[英]CakePHP HABTM condition
我想對我的HABTM屬性做一個條件,我在CakePHP 2.x中具有以下HABTM關系:
Practise.php
public $hasAndBelongsToMany = array(
'Attribute' => array(
'className' => 'Attribute',
'joinTable' => 'practises_attributes',
'foreignKey' => 'practise_id',
'associationForeignKey' => 'attribute_id',
'unique' => true,
'conditions' => '',
'fields' => '',
'order' => '',
'limit' => '',
'offset' => '',
'finderQuery' => '',
)
);
Attribute.php
public $hasAndBelongsToMany = array(
'Practise' => array(
'className' => 'Practise',
'joinTable' => 'practises_attributes',
'foreignKey' => 'attribute_id',
'associationForeignKey' => 'practise_id',
'unique' => true,
'conditions' => '',
'fields' => '',
'order' => '',
'limit' => '',
'offset' => '',
'finderQuery' => '',
)
);
現在,我想在我的PractiseController.php中找到所有包含“屬性”條件的條件
PractiseController.php
$cond['Attribute.id'] = array(1,2,3);
$this->Practise->find('all', array('conditions' => $cond));
然后我得到以下錯誤:
錯誤:SQLSTATE [42S22]:找不到列:1054“ where子句”中的未知列“ Attribute.id”
SQL查詢:
選擇Practise
。 id
, Practise
。 title
, Practise
。 body
,來自db
。 practises
AS Practise
WHERE Attribute
。 id
IN(1、2、3)
如何使CakePHP也可以將HABTM表加入查找查詢?
您可以使用包含為例
$this->Practise->find('all', array('contain' => 'Attribute.id = whatever'));
您也可以手動加入:
$options['joins'] = array( array('table' => 'practises_attributes', 'alias' => 'PractiseAttribute', 'type' => 'INNER', 'conditions' => array( 'Attribute.id = whatever', ) ) );
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.