Your comment database table should look like this:
id | belongs_to | foreign_id | .... ------------------------------------- | | |
belongs_to either enum or varchar
Then in your model you can link the comment model, as it is in the comment model:
var $belongsTo = array( 'News' => array( 'className' => 'News', 'foreignKey' => 'foreign_id', 'conditions' => array('Comment.belongs_to'=>'News'), 'fields' => '', 'order' => '' ), 'Post' => array( 'className' => 'Post', 'foreignKey' => 'foreign_id', 'conditions' => array('Comment.belongs_to'=>'Post'), 'fields' => '', 'order' => '' ) );
And in the Post-Model:
var $hasMany = array( 'Comment' => array( 'className' => 'Comment', 'foreignKey' => 'foreign_id', 'dependent' => true, 'conditions' => array('Comment.belongs_to' => 'Post'), 'fields' => '', 'order' => 'Comment.created DESC', 'limit' => '', 'offset' => '', 'exclusive' => '', 'finderQuery' => '', 'counterQuery' => '' ) );
source share