0

我在从多个表中搜索时遇到了一些问题。我在卡片模型上有一个关键字搜索设置,效果很好,我还希望关键字查看具有 card_id 外键的联系人模型。我似乎无法弄清楚如何去做。

要查看/有问题的要点是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;
    }
4

1 回答 1

0

我有类似的麻烦。事实证明,即使我(认为)我的字段都遵循标准的 CakePHP 约定,我也必须在 HABTM 模型上手动定义“with”。另外,我没有看到您在 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 或其他东西。

于 2014-02-01T16:51:10.793 回答