Mysql 容器行为中的Cakephp多表搜索

Mysql 容器行为中的Cakephp多表搜索,mysql,cakephp,cakephp-2.0,Mysql,Cakephp,Cakephp 2.0,您好,我想使用用户键入的关键字从多个表中获取结果。我遇到的问题是,我不知道如何在正在通过的表上进行搜索 这是我的问题 public function getGigPostBasedOnSearch($keyword){ $this->Behaviors->attach('Containable'); return $this->find('all', array( 'contain' => array(

您好,我想使用用户键入的关键字从多个表中获取结果。我遇到的问题是,我不知道如何在正在通过的表上进行搜索

这是我的问题

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.'%')
请帮助我如何在类别表中搜索

多谢各位

模型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
        ));
    }




}

您的问题是GigPost和Category之间的关系是HABTM,因此当您使用contain Cake时,必须执行单独的查询来检索相关的类别,因为没有一对一的关联。要解决此问题,您需要手动执行联接,而不是包含类别:-

$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数组,因为您似乎没有为表使用蛋糕命名约定,所以我不确定您命名了什么。不过,这应该会让您走上正确的道路。

您为您的模型定义了哪些关系?@drmonkeynija我将更新我的question@drmonkeyninja您想让我添加与当前GigPost相关的其他型号的详细信息吗?非常感谢@drmonkeynija。你太棒了。你太棒了。我现在欠你的债。再次感谢你