cakephp cakedc search plugin - 多表/多查询搜索

时间:2011-10-23 22:59:10

标签: php cakephp search plugins cakedc

我在从多个表中搜索时遇到了一些问题。我在卡片模型上有一个关键字搜索设置工作正常,我也希望关键字查看具有card_id的forign键的联系人模型。我似乎无法弄清楚如何解决这个问题。

要查看/有问题的要点是findByContacts功能和:

array('Contact.street_suburb' => 'contacts', 
      'type' => 'subquery', 
      'method' => 'findByContacts', 
      'field' => 'Card.id'),

我最初试图让郊区进行搜索,但理想情况下我希望联系人模型中的任何字段都可以进行搜索。

谢谢!

我在卡片模型中的代码如下:

public $filterArgs = array(
    array('name' => 'keyword', 'type' => 'query', 'method' => 'filterQuery'),
);

public $hasAndBelongsToMany = array('Contact' => array('with' => 'Contact'));

public function filterQuery($data = array()) {
    if(empty($data['keyword'])) { // keyword is the name of my search field
        return array();
    }

    $query = '%'.$data['keyword'].'%';
    return array(
        'OR' => array(
            array('Card.name LIKE' => $query),
            array('Property.name LIKE' => $query),
            array('Building.name LIKE' => $query),
            array('Stage.name LIKE' => $query),
            array('Contact.street_suburb' => 'contacts', 'type' => 'subquery', 'method' => 'findByContacts', 'field' => 'Card.id'),
        )
    );
} // END SEARCH 

// FIND BY CONACTS - PART OF SEARCH
// ------------------------------------------------------------------------------------>
public function findByContacts($data = array()) {
        $this->Contact->Behaviors->attach('Containable', array('autoFields' => false));
        $this->Contact->Behaviors->attach('Search.Searchable');
        $query = $this->Contact->getQuery('all', array(
            'conditions' => array('Contact.street_suburb'  => $data['contacts']),
            'fields' => array('foreign_key'),
            'contain' => array('Contact')
        ));
        return $query;
    }

1 个答案:

答案 0 :(得分:0)

我遇到了类似的麻烦。事实证明我必须在HABTM模型上手动定义'with',即使我(我认为)我的字段都遵循标准的CakePHP约定。另外,我没有看到你在filterArgs中调用findByContacts但也许我错过了它。

所以我的代码就这样工作了:

(宝藏模型,相关代码)

public $filterArgs = array(array('name' => 'makers', 'type' => 'subquery', 'method' => 'findByMaker', 'field' => 'Treasure.id'),);

public function findByMaker($data = array()) {
$this->MakersTreasure->Behaviors->attach('Containable', array('autoFields' => false));
$this->MakersTreasure->Behaviors->attach('Search.Searchable');
    $query = $this->MakersTreasure->getQuery('all', array(
        'conditions' => array("Maker.name LIKE '%" . $data['makers'] ."%'"),
        'fields' => array('treasure_id'),
        'contain' => array('Maker'),
    //the limit does not work, but it does make the timeout query halt so you can see what's going on
    //'limit'=>1
    ));
    return $query;
}

然后,同样在我必须为Makers设置的模型上:

public $hasAndBelongsToMany = array(
    'Maker' => array(
        'className' => 'Maker',
        'joinTable' => 'makers_treasures',
        'foreignKey' => 'treasure_id',
        'associationForeignKey' => 'maker_id',
        'unique' => 'keepExisting',
        'with' => 'MakersTreasure'
    ),
            ...

因此,在您的函数中,在运行查询时或在“with”中,您似乎不是引用了联结模型。您的数据库中是否有HABTM的联结表?从它的声音来看,如果Contact有一个FK card_id,那么它不是HABTM关系,而是OnetoMany或其他东西。