Cakephp多表搜索容器行为

时间:2017-08-23 08:16:07

标签: mysql cakephp cakephp-2.0

你好我想使用用户输入的关键字从多个表中获取结果。我遇到的问题是我不知道如何在该表上搜索contain

这是我的查询

public function getGigPostBasedOnSearch($keyword){
        $this->Behaviors->attach('Containable');
        return $this->find('all', array(

            'contain' => array(
              'User','UserInfo', 'GigPostAndCategory.Category','Location','GigPostAndCalender'

            ),
              'conditions' => array(
                  'AND' => array( 
                      'GigPost.active' => 1,
               'OR' => array(

            array('GigPost.title LIKE' => '%'.$keyword.'%'),
            array('GigPost.description LIKE' => '%'.$keyword.'%'),
            array('Location.location_string LIKE' => '%'.$keyword.'%'),


        )
)
                 //'OrderGigPost.request' => 0
            ),
            'order' => 'GigPost.gig_post_id DESC',

            'recursive' => 0
        ));
    }

此查询正常。我也想在类别表中搜索关键字。我想做类似这样的事情

array('Category.cat_name LIKE' => '%'.$keyword.'%')

请帮助我如何在分类表中搜索<​​/ p>

谢谢

模型GigPost.php

<?php



class GigPost extends AppModel
{
    public $useTable = 'gig_post';
    public $primaryKey = 'gig_post_id';

public $hasAndBelongsToMany = array(
'Category' =>
array(
'className' => 'Category',
'joinTable' => 'gigpost_category',
'foreignKey' => 'gig_post_id',
'associationForeignKey' => 'cat_id',
'unique' => true,
)
);
    public $hasOne = array(
        'Location' => array(
            'className'     => 'Location',
            'foreignKey'    => 'gig_post_id',
            'conditions' => array('GigPost.gig_post_id = Location.gig_post_id')
        ));


    public $belongsTo = array(
        'User' => array(
            'className' => 'User',
            'foreignKey' => 'user_id',
            'fields' => array('User.user_id','User.email','User.active')

        ),
        'UserInfo' => array(
            'className' => 'UserInfo',
            'foreignKey' => 'user_id',


        )

    );







    public function getGigPostBasedOnSearch($keyword){
        $this->Behaviors->attach('Containable');
        return $this->find('all', array(

            'contain' => array(
                'User','UserInfo', 'GigPostAndCategory.Category'=>array(
                    'conditions'=>array(

                        'OR' => array(

                            array('Category.cat_name' => '%'.$keyword.'%'),
                        ),


                    )
                ),'Location','GigPostAndCalender'

            ),
            'conditions' => array(
                'AND' => array(
                    'GigPost.active' => 1,
                    'OR' => array(


                        array('GigPost.title LIKE' => '%'.$keyword.'%'),
            array('GigPost.description LIKE' => '%'.$keyword.'%'),
            array('Location.location_string LIKE' => '%'.$keyword.'%'),
           // array('Category.cat_name LIKE' => '%'.$keyword.'%'),


        )
)
                 //'OrderGigPost.request' => 0
            ),
            'order' => 'GigPost.gig_post_id DESC',

            'recursive' => 0
        ));
    }




}

1 个答案:

答案 0 :(得分:1)

您的问题是GigPostCategory之间的关系是HABTM,因此当您使用contain时,Cake必须执行单独的查询以检索相关类别,因为没有一对一协会。为了解决此问题,您需要手动执行连接而不是contain类别: -

$this->find('all', array(
    'contain' => array(
        'User', 'UserInfo', 'GigPostAndCategory.Category', 'Location', 'GigPostAndCalender'
    ),
    'joins' => array(
        array(
            'table' => 'gigpost_category',
            'alias' => 'GigPostCategory',
            'type' => 'INNER',
            'conditions' => 'GigPost.id = GigPostCategory.gig_post_id'
        ),
        array(
            'table' => 'categories',
            'alias' => 'Category',
            'type' => 'INNER',
            'conditions' => 'Category.id = GigPostCategory.cat_id'
        )
    ),
    'conditions' => array(
        'AND' => array( 
            'GigPost.active' => 1,
            'OR' => array(
                array('GigPost.title LIKE' => '%'.$keyword.'%'),
                array('GigPost.description LIKE' => '%'.$keyword.'%'),
                array('Location.location_string LIKE' => '%'.$keyword.'%'),
                array('Category.cat_name LIKE' => '%'.$keyword.'%'),
            )
        )
    ),
    'order' => 'GigPost.gig_post_id DESC',
    'group' => 'GigPost.id',
    'recursive' => 0
));

这可确保类别将包含在主查询返回的数据中。

您可能需要稍微修改joins数组,因为您似乎没有使用表的Cake命名约定,因此我不确定您的名称是什么。但是,这应该会让你走上正轨。