1

我的类别模型:

class Category extends AppModel {
    public $displayField = 'name';

//    public $actsAs = array('Containable');

    public $hasAndBelongsToMany = array(
        'Post' => array(
            'className' => 'Post',
            'joinTable' => 'categories_postss',
            'foreignKey' => 'category_id',
            'associationForeignKey' => 'post_id',
            'unique' => 'keepExisting'
        )
    );
}

$params['contain'] = array('Post' => array(
            'limit'=> 3));
        pr($this->Category->find('first',$params)); exit;

它正在获取所有帖子,无论限制如何。我想做的事:

我有这个页面,我可以在其中列出所有类别和与之相关的最新 5 篇文章。我想将关联模型限制为仅 5 行。

有任何想法吗?

4

2 回答 2

2

未使用可包含行为

此问题最可能的原因是根本没有使用可包含的行为。

比较,对于以下代码示例:

$results = $this->Category->find('first', array(
    'contain' => array(
        'Post' => array(
            'limit' => 3
        )   
    )   
));

如果没有可包含的行为,它将生成以下查询:

SELECT ... FROM `crud`.`categories` AS `Category` WHERE 1 = 1 LIMIT
SELECT ... FROM `crud`.`posts` AS `Post` 
    JOIN `crud`.`categories_posts` AS `CategoriesPost` ON (...)

通过可包含的行为,它将生成以下查询:

SELECT ... FROM `crud`.`categories` AS `Category` WHERE 1 = 1 LIMIT
SELECT ... FROM `crud`.`posts` AS `Post` 
    JOIN `crud`.`categories_posts` AS `CategoriesPost` ON (...) LIMIT 3

鉴于此(以及问题中的代码),请检查 AppModel 是否具有可包含的行为$actsAs

<?php
// app/Model/AppModel.php
class AppModel extends Model {
    public $actsAs = array('Containable');
}

总是需要限制?

或者,或者可能另外,您可能更喜欢在关联定义中设置限制- 为此只需定义“限制”键:

class Category extends AppModel {

    public $hasAndBelongsToMany = array(
        'Post' => array(
            'limit' => 100, // default to a high but usable number of results
        )
    );
}
于 2013-08-23T15:09:24.907 回答
1

the hasAndBelongsToMany relationship seems unnecessary to me. I think you only need Category hasMany Post and Post belongsTo Category relationships. Add category_id to the posts table. Make both models actAs containable.

Post Model

class Post extends AppModel {
     public $actsAs = array('Containable');

    var $belongsTo = array(
        'Category' => array(
            'className' => 'Category',
            'foreignKey' => 'category_id'
        ),
        // ... more relationships
    );

Category Model

class Category extends AppModel {
     public $actsAs = array('Containable');

    var $hasMany = array(
        'Post' => array(
            'className' => 'Post',
            'foreignKey' => 'category_id'
        ),
        // ... more relationships
    );

Categories Controller

class CategoriesController extends AppController {

    public $paginate = array(
        'Category' => array(
            'contain' => array(
                 'Post' => array(
                     'limit' => 3
                 ), // end Post
             ) // end Category contain
         ) // end Category pagination
     ); // end pagination

     public function index() {

         // for paginated results
         $this->set('categories', $this->paginate());

         // for find results
         $this->Category->contain(array(
             'Post' => array(
                 'limit' => 3
             )
         ));

     $this->set('categories', $this->Category->find('all'));

     } 
于 2013-08-22T14:12:55.657 回答