简体   繁体   English

向分页查询中的联接表添加条件(CakePHP 2.0.4)

[英]Add condition to join table in pagination query (CakePHP 2.0.4)

I am new to cakephp. 我是Cakephp的新手。 I have to do some improvements in the cakephp based application. 我必须在基于cakephp的应用程序中做一些改进。 In that, there are categories and images models. 在那,有类别和图像模型。 In the images table has lots of images. 在图像表中有很多图像。 Default category images and other category related images details are maintaining in that table. 该表中保留了默认类别图像和其他类别相关图像的详细信息。 It will display following queries in the 2nd page (pagination) of list categories. 它将在列表类别的第二页(分页)中显示以下查询。

SELECT `Category`.`id`, `Category`.`name`, `Category`.`slug`, `Category`.`meta_description`, `Category`.`meta_keywords`, `Category`.`sort`, `Category`.`color`, `Category`.`bgcolor`, `Category`.`modified`, `Category`.`created` FROM `categories` AS `Category`   WHERE 1 = 1   ORDER BY `sort` ASC  LIMIT 20, 20

SELECT `Image`.`id`, `Image`.`path`, `Image`.`size`, `Image`.`tags`, `Image`.`default`, `Image`.`category_id`, `Image`.`modified`, `Image`.`created` FROM `images` AS `Image` WHERE `Image`.`category_id` IN (60, 17, 9, 33, 71, 18, 73, 30, 58, 54, 3, 44, 64, 66, 67, 11, 53, 16, 23, 68)

But actually, the second query needs to be like: 但实际上,第二个查询需要像这样:

SELECT `Image`.`id`, `Image`.`path`, `Image`.`size`, `Image`.`tags`, `Image`.`default`, `Image`.`category_id`, `Image`.`modified`, `Image`.`created` FROM `images` AS `Image` WHERE `Image`.`default` = 1 AND `Image`.`category_id` IN (60, 17, 9, 33, 71, 18, 73, 30, 58, 54, 3, 44, 64, 66, 67, 11, 53, 16, 23, 68)

There should be 应该有

Image . Image default = 1 in WHERE clause. WHERE子句中的default = 1。

My CategoriesController.php index action is: 我的CategoriesController.php索引操作是:

public function index() {
    $this->Category->recursive = 1;
    $this->Category->Behaviors->attach('Containable');
    $this->paginate = array("contain"=>array("Image"));
    $search="";
    if(isset($this->params['named']['search'])){
        $search = str_replace("%20"," ", $this->params['named']['search']);
        $conditions = array(
            'OR' => array(
                array('Category.name LIKE' => '%'.$search.'%')
            )
        );

        $this->set('categories', $this->paginate($conditions));
    } else {
            $this->set('categories', $this->paginate());
    }
    $success = $this-> Session->read('success');
    $this->set(compact('search', 'success'));   
}

And my Category model has : 我的类别模型有:

public $hasMany = array(
    'Panel' => array(...),
    'Image' => array(
        'className' => 'Image',
        'foreignKey' => 'category_id',
        'dependent' => false,
        'conditions' => array('Image.default' => '1'),
        'fields' => '',
        'order' => '',
        'limit' => '',
        'offset' => '',
        'exclusive' => '',
        'finderQuery' => '',
        'counterQuery' => ''    
    )   
);

So, I need to filter the join table images.default set to '1' 因此,我需要过滤联接表images.default设置为'1'

In your categories controller change this line: 在类别控制器中,更改以下行:

$this->paginate = array("contain"=>array("Image"));

to: 至:

$this->paginate = array("contain"=>array("Image.default = 1"));

More info here: http://book.cakephp.org/2.0/en/core-libraries/behaviors/containable.html#containing-deeper-associations 此处提供更多信息: http : //book.cakephp.org/2.0/en/core-libraries/behaviors/containable.html# contains-deeper-associations

Note: default recursive level is 1 注意:默认递归级别为1

Combine both condition into 1 将两个条件合并为1

$this->paginate = array("contain"=>array("Image.default = 1"),"recursive" => 1);

or make multiple conditions 或使多个条件

$this->paginate = array('contain'=>array('Image'=>array('conditions'=>array('Image.default'=>1))))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM