开发者

One table two model

i have posts table

id

type enum(A,Q)

title

content

and i need controllers questions_controller and answer_controller to use

please lead the right way to

1-开发者_运维知识库

make 2 models (question and answer) and use 'posts' as model's table

2-

use post model in questions_controller and answer_controller


For me, it should be one table has one model

So better use $uses = array('Post'); in your controllers.

Update: As far I understood you want to filter the data depending from the type column. Actually your db model is wrong, because except if they are totally unrelated your answers need to belong to the question. This mean you need an extra column parent_id which will determine what is the parent node (question) and child nodes (answers).

If we have this assumption, then you can set a relation in the model like:

class Post extends AppModel {
   ...
   var $belongsTo = array(
      'Parent' => array('className' => 'Post','foreignKey' => 'parent_id', 'conditions' => 'Parent.parent_id IS NOT NULL')
   );
   ...
}

So you automatically have Post and Post->Parent. But I think that it would be better to have 2 tables - one which holds the questions and another the answers.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜