我在从多个表中搜索时遇到了一些问题。我在卡片模型上有一个关键字搜索设置,效果很好,我还希望关键字查看具有 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;
}