CakePHP - Post type without dB table
What's the best way of doing this in cakephp:
I have a Posts model+view+controller with a "Post type" field in my database - linking to my post type id.
The post type ids are: - Preview - Review - News
What开发者_StackOverflow中文版 I'm asking is: What's the best way (natively) of retrieving the post type name, without creating a table Post_types and linking it with a post_id.
I tried creating an array in the config lists, it worked but I think it can be better then this.
You could use the afterFind
method of your Post
model and add there a post_type
field to your results. See also the cookbook.
From what I understand your post_type
is a enum field with values preview, review, news
or a varchar in which you know only add these 3 types, right?
You could try the following query
SELECT DISTINCT(post_type) FROM posts;
This will return all post_types
that are being used in your posts
table.
In Cake you could do this either with the find
or query
method.
# Using find
$this->Post->find('all', array(
'fields' => array('DISTINCT(post_type)')
);
# Or using the query directly (maybe easier in this case)
$this->Post->query("SELECT DISTINCT(post_type) FROM posts;");
associate your table posts with the table post_types with $hasOne. With a e.g. $this->Post->find('all'); you would get the post and the post_type in one array.
精彩评论