2011-10-23 137 views
0

我有一些問題從多個表中搜索。我有一個卡片模型的關鍵字搜索設置工作正常,我還希望關鍵字查看具有card_id的外鍵的聯繫人模型。我似乎無法解決如何解決這個問題。cakephp cakedc搜索插件 - 多表/多個查詢搜索

的點一下/有問題的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; 
    } 
+0

我想你錯過了幾件事,但你可以定義不工作?什麼是不工作?它是否正確地搜索卡片?它是否正確地搜索聯繫人?它至少做了一個查詢嗎?或根本就沒有做任何事情? – api55

+1

謝謝,我在上面提到的錯誤是: 警告(512):SQL錯誤:1054:'where子句'中的未知列'Contact.street_suburb'[CORE/cake/libs/model/datasources/dbo_source .php,line 684] 我想這意味着聯繫人和卡模型沒有正確鏈接... – sluggerdog

+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; 
} 

,然後又在模型上我與莊家這樣設置:

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

所以在你的函數中,它看起來不像你在運行查詢時或在'with'中引用junction模型。你的數據庫中是否有HABTM的聯結表?從它的聲音來看,如果聯繫人有FK card_id,那麼它不是HABTM關係,而是OnetoMany或其他。