post habtm postlover:如何按照后期人数订购帖子?

时间:2021-11-18 22:46:01

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).

所以帖子属于作者(当作者写作时),但是帖子habtm作者(当作者喜欢它们时)。

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:

例如,我想在过去的24小时内获得按照后期数量排序的帖子。这是我的模型和连接表:

TABLE: lovedposts_postlovers id: INT lovedpost_id: INT postlover_id: INT

表: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';

}

?> 

2 个解决方案

#1


Your best option is to query on the joinModel. Usually that would be:

您最好的选择是查询joinModel。通常会是:

$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.

但是,因为你没有坚持表的Cake命名约定,这些约定可能会有所不同。 joinModel是自动创建的BTW,但您也可以在HABTM声明中明确指定它。

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.

对于查找选项,您可以执行任何所需操作,'group'=>'post_id'和'order'=>'COUNT(post_id)'或其他内容。您正在寻找的是获得正确的'post_ids'。

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.

从joinModel的角度来看,Author和Post模型都是belongsTo关系,Cake会相应地找到相关结果,你可以使用通常的'contains'选项等来过滤结果。

Hope that helps.

希望有所帮助。

#2


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.

我想你应该退后一步,尝试阅读文档(book.cakephp.org)。尝试使用他们的示例进行演示。

  • 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表非常混乱,也许应该被称为其他东西,也许是authors_posts甚至是收藏夹。好吧,只要你在'joinTable'中指定它,它就可以是任何东西。

  • lovedposts_postlovers should have the fields author_id, post_id

    lovedposts_postlovers应该有字段author_id,post_id

    //POST MODEL var $hasAndBelongsToMany = array( 'Author' => array( 'className' => 'Author', 'joinTable' => 'lovedposts_postlovers', 'foreignKey' => 'author_id', 'associationForeignKey' => 'post_id', 'unique' => true ) );

    // 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:

例如,我想在过去的24小时内获得按照后期数量排序的帖子。这是我的模型和连接表:

$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.

基本上你想要通过post_id进行选择计数查询和分组,这段代码可以让你走上正确的轨道。注意:我没有测试此代码。您还需要在该查找操作中使用订单子句,但我会将其留给您。

#1


Your best option is to query on the joinModel. Usually that would be:

您最好的选择是查询joinModel。通常会是:

$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.

但是,因为你没有坚持表的Cake命名约定,这些约定可能会有所不同。 joinModel是自动创建的BTW,但您也可以在HABTM声明中明确指定它。

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.

对于查找选项,您可以执行任何所需操作,'group'=>'post_id'和'order'=>'COUNT(post_id)'或其他内容。您正在寻找的是获得正确的'post_ids'。

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.

从joinModel的角度来看,Author和Post模型都是belongsTo关系,Cake会相应地找到相关结果,你可以使用通常的'contains'选项等来过滤结果。

Hope that helps.

希望有所帮助。

#2


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.

我想你应该退后一步,尝试阅读文档(book.cakephp.org)。尝试使用他们的示例进行演示。

  • 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表非常混乱,也许应该被称为其他东西,也许是authors_posts甚至是收藏夹。好吧,只要你在'joinTable'中指定它,它就可以是任何东西。

  • lovedposts_postlovers should have the fields author_id, post_id

    lovedposts_postlovers应该有字段author_id,post_id

    //POST MODEL var $hasAndBelongsToMany = array( 'Author' => array( 'className' => 'Author', 'joinTable' => 'lovedposts_postlovers', 'foreignKey' => 'author_id', 'associationForeignKey' => 'post_id', 'unique' => true ) );

    // 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:

例如,我想在过去的24小时内获得按照后期数量排序的帖子。这是我的模型和连接表:

$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.

基本上你想要通过post_id进行选择计数查询和分组,这段代码可以让你走上正确的轨道。注意:我没有测试此代码。您还需要在该查找操作中使用订单子句,但我会将其留给您。