我寫了2天前詢問和條件,它似乎我不明白的想法,但事實是,兩天後,我堅持下一步使用CakeDC:CakeDC搜索插件在與HABTM查詢中使用複雜條件
如何在CakeQuery搜索插件的「查詢」方法中實現複雜的HABTM條件?
我有發售HABTM特徵(表:報價,功能,features_offers),並在控制器中使用時,下面的工作得很好:
debug($this->Offer->find('all', array('contain' => array(
'Feature' => array(
'conditions' => array(
'Feature.id in (8, 10)',
)
)
)
)
)
);
問題是當我想要使用相同的條件搜索:
public $filterArgs = array(
array('name' => 'feature_id', 'type' => 'query', 'method' => 'findByFeatures'),
);
........
public function findByFeatures($data = array()) {
$conditions = '';
$featureID = $data['feature_id'];
if (isset($data['feature_id'])) {
$conditions = array('contain' => array(
'Feature' => array(
'conditions' => array(
'Feature.id' => $data['feature_id'],
)
)
)
);
}
return $conditions;
}
我得到一個錯誤:
Error: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'contain' in 'where clause'
這使得我認爲我無法在搜索中執行此搜索和/或使用可容忍的行爲。
如果有人在該領域有更多的經驗,請告訴我,如果我錯過了某些東西或指向我到哪裏找到解決方案 - 也許是食譜中的一個部分?
編輯:也試過連接。該控制器工作完全正常,返回我需要的所有數據:
$options['joins'] = array(
array('table' => 'features_offers',
'alias' => 'FeaturesOffers',
'type' => 'inner',
'conditions' => array(
'Offer.id = FeaturesOffers.offer_id'
),
array('table' => 'features',
'alias' => 'F',
'type' => 'inner',
'conditions' => array(
'F.id = FeaturesOffers.feature_id'
),
)
),
);
$options['conditions'] = array(
'feature_id in (13)' //. $data['feature_id']
);
debug($this->Offer->find('all', $options));
...當我試圖把搜索方法,我只在SQL的where子句獲取返回的條件
WHERE ((joins = (Array)) AND (conditions = ('feature_id in Array')))
...導致錯誤:
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'joins' in 'where clause'
編輯:也許我是愚蠢的,抱歉地說,但該插件的文檔吸一噸。
我雙人,三人和四人檢查(順便說一句,已經失去了至少1提交搜索表單捂臉 30小時),從文檔愚蠢findByTags仍然沒有任何意義,我。
public function findByTags($data = array()) {
$this->Tagged->Behaviors->attach('Containable', array('autoFields' => false));
$this->Tagged->Behaviors->attach('Search.Searchable');
$query = $this->Tagged->getQuery('all', array(
'conditions' => array('Tag.name' => $data['tags']),
'fields' => array('foreign_key'),
'contain' => array('Tag')
));
return $query;
}
據我瞭解
$this->Tagged
應該是HABTM協會的模型的名稱。
這是很遠CakePHP的標準,但:http://book.cakephp.org/2.0/en/models/associations-linking-models-together.html#hasandbelongstomany-habtm
它是這裏所描述的樣子,說,你並不需要另一種模式,而是你,如下圖所示副食譜與成分:
class Recipe extends AppModel {
public $hasAndBelongsToMany = array(
'Ingredient' =>
array(
'className' => 'Ingredient',
'joinTable' => 'ingredients_recipes',
'foreignKey' => 'recipe_id',
'associationForeignKey' => 'ingredient_id',
'unique' => true,
'conditions' => '',
'fields' => '',
'order' => '',
'limit' => '',
'offset' => '',
'finderQuery' => '',
'deleteQuery' => '',
'insertQuery' => ''
)
);
}
這意味着您可以從配方中訪問HABTM關聯表數據,而無需定義模型「IngredientRecipe」。
而且根據cakeDC文檔,你需要的型號是IngredientRecipe和未表示爲CakePHP的文檔中的一些強制性的。即使這個模型被創建,HABTM assoc也無法正常工作 - 我也試過這個。
現在我需要重新寫在我的方式搜索功能,只使用CakePHP的,即使我已經花了30個小時就......如此不開心。 :(
IngredientRecipe錯誤的連接模型名稱應該是IngredientsRecipe,注意複數的成分。這本書也描述了這一點。 HABTM數組中有「with」鍵,可讓您指定模型。這本書在你粘貼在這裏的書的示例代碼之後。此外,這仍然不是插件或文檔的問題,您只需構建您的關聯和查詢錯誤。插件本身只是實現了PRG模式。歡迎您提交顯示插件中問題的單元測試。 – burzum