post habtm postlover :如何按 postlovers 的数量对帖子进行排序?

发布于 2024-07-27 15:43:56 字数 1411 浏览 4 评论 0原文

请帮助我,我真的很挣扎......

作者可以写帖子,作者可以喜欢其他作者的帖子......

所以帖子属于作者(当作者写它们时),但帖子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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

紫竹語嫣☆ 2024-08-03 15:43:56

最好的选择是查询 joinModel。 通常情况下是:

$this->Author->AuthorsLovedpost->find(...);

但是由于您没有遵守表的 Cake 命名约定,因此可能会有所不同,也可能不会有所不同。 顺便说一句,joinModel 是自动创建的,但您也可以在 HABTM 声明中显式指定它。

var $hasAndBelongsToMany = array(
    'Postlover' => array(
         ...,
         'with' => 'joinModelName'
    )
);

对于查找选项,您可以执行任何您需要的操作,'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:

$this->Author->AuthorsLovedpost->find(...);

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.

var $hasAndBelongsToMany = array(
    'Postlover' => array(
         ...,
         'with' => 'joinModelName'
    )
);

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.

↙温凉少女 2024-08-03 15:43:56

我认为你应该退一步并尝试阅读文档(book.cakephp.org)。 尝试使用他们的示例制作演示。

  • loveposts_postlovers 表非常混乱,也许应该称为其他名称,也许是authors_posts 甚至是favorites。 好吧,它可以是任何东西,只要你在“joinTable”中指定它即可。
  • lovedposts_postlovers 应该包含字段author_id、post_id

    //帖子模型
    var $hasAndBelongsToMany = 数组(
    '作者' =>
    大批(
    '类名' => '作者',
    '加入表' => 'lovedposts_postlovers',
    '外键' => '作者id',
    'associationForeignKey' =>; '帖子 ID',
    '独特' => 真的

    );

例如,我想获取按帖子爱好者数量排序并在过去 24 小时内创建的帖子。 这是我的模型和连接表:

$this->Post->LovedPosts->find('all', array('fields'=>array('Post.title', 'count(LovedPosts.*) as favorited'), 'group'=>'LovedPosts.post_id');

基本上,您想要执行选择计数查询并按 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 table is very confusing and maybe should be called something else, maybe authors_posts or even favorites. well, it can be anything as long as you specify it in 'joinTable'.
  • 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:

$this->Post->LovedPosts->find('all', array('fields'=>array('Post.title', 'count(LovedPosts.*) as favorited'), 'group'=>'LovedPosts.post_id');

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文