post habtm postlover :如何按 postlovers 的数量对帖子进行排序?
请帮助我,我真的很挣扎......
作者可以写帖子,作者可以喜欢其他作者的帖子......
所以帖子属于作者(当作者写它们时),但帖子habtm 作者(当作者喜欢他们时)。
例如,我想获取按帖子爱好者数量排序的帖子, 过去 24 小时内创建。 这是我的模型和连接表:
表:lovedposts_postlovers 编号:INT lovepost_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';
}
?>
please help me, i'm really struggling with this...
Authors can write post, and authors can love other authors' posts...
So posts belong to author (when authors write them), but posts habtm
authors (when authors love them).
For example i'd like to get posts ordered by number of postlovers and
created in the last 24 hours. Here's my models and join table:
TABLE: lovedposts_postlovers
id: INT
lovedpost_id: INT
postlover_id: INT
POST MODEL
<?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';
}
?>
AUTHOR MODEL
<?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';
}
?>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
最好的选择是查询 joinModel。 通常情况下是:
但是由于您没有遵守表的 Cake 命名约定,因此可能会有所不同,也可能不会有所不同。 顺便说一句,joinModel 是自动创建的,但您也可以在 HABTM 声明中显式指定它。
对于查找选项,您可以执行任何您需要的操作,
'group' => 'post_id'
和'order' => 'COUNT(post_id)'
或类似的内容。 您正在寻找的是获取正确的“post_ids”集。由于从 joinModel 的角度来看,Author 和 Post 模型都是属于关系,Cake 会相应地找到相关结果,您可以使用常用的
'contain'
选项等来过滤结果。希望有帮助。
Your best option is to query on the joinModel. Usually that would be:
But since you're not sticking to the Cake naming conventions for the table that may or may not be different. The joinModel is automatically created BTW, but you can also explicitly specify it in the HABTM declaration.
For the find options you can do whatever you need,
'group' => 'post_id'
and'order' => 'COUNT(post_id)'
or something to that extend. What you're looking for is getting the right set of 'post_ids' back.Since from the point of view of the joinModel the Author and Post models are both belongsTo relationships, Cake will find related results accordingly and you can use the usual
'contain'
options etc to filter results.Hope that helps.
我认为你应该退一步并尝试阅读文档(book.cakephp.org)。 尝试使用他们的示例制作演示。
lovedposts_postlovers 应该包含字段author_id、post_id
//帖子模型
var $hasAndBelongsToMany = 数组(
'作者' =>
大批(
'类名' => '作者',
'加入表' => 'lovedposts_postlovers',
'外键' => '作者id',
'associationForeignKey' =>; '帖子 ID',
'独特' => 真的
)
);
例如,我想获取按帖子爱好者数量排序并在过去 24 小时内创建的帖子。 这是我的模型和连接表:
基本上,您想要执行选择计数查询并按 post_id 进行分组,并且此代码应该让您走上正确的轨道。 注意:我没有测试这段代码。 您还需要在该查找操作中使用 order 子句,但我会将其留给您。
I think you should take a step back and try to read the documentation (book.cakephp.org). Try to make a demo using their examples.
lovedposts_postlovers should have the fields author_id, post_id
//POST MODEL
var $hasAndBelongsToMany = array(
'Author' =>
array(
'className' => 'Author',
'joinTable' => 'lovedposts_postlovers',
'foreignKey' => 'author_id',
'associationForeignKey' => 'post_id',
'unique' => true
)
);
For example i'd like to get posts ordered by number of postlovers and created in the last 24 hours. Here's my models and join table:
Basically you want to do a select count query and group by the post_id and this code should get you on the right track. Note: I didn't test this code. You also need an order clause in that find operation but I will leave that to you.