请帮助我,我真的很努力解决这个问题......
作者可以写帖子,作者可以喜欢其他作者的帖子……
所以帖子属于作者(当作者写它们时),但属于 habtm 作者(当作者喜欢它们时)。
例如,我想获取按帖子爱好者数量排序并在过去 24 小时内创建的帖子。这是我的模型和连接表:
表:lovedposts_postloves id:INTlovepost_id:INT postlover_id:INT
后模型
<?php
class Post extends AppModel {
var $name = 'Post';
var $belongsTo = 'Author';
var $hasAndBelongsToMany = array(
'Postlover' =>
array(
'className' => 'Author',
'joinTable' => 'lovedposts_postlovers',
'foreignKey' => 'lovedpost_id',
'associationForeignKey' => 'postlover_id',
'unique' => true
)
);
var $displayField = 'title';
}
?>
作者模型
<?php
class Author extends AppModel {
var $name = 'Author';
var $hasMany = 'Post';
var $hasAndBelongsToMany = array(
'Lovedpost' =>
array(
'className' => 'Post',
'joinTable' => 'lovedposts_postlovers',
'foreignKey' => 'postlover_id',
'associationForeignKey' => 'lovedpost_id',
'unique' => true
)
);
var $displayField = 'username';
}
?>