2014-12-14 1 views
0

내 HABTM에 조건이 속성 만들고 싶어 I CakePHP의 2.x에서의 다음 HABTM 관계가 있습니다CakePHP의 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' => '', 
    ) 
); 

이제 Attribu에 대한 조건을 모두 찾고 싶습니다.

오류 : SQLSTATE [42S22] : 발견되지 칼럼 : 1054 알 수없는 열 '특성 TES 내 PractiseController.php

PractiseController.php에

$cond['Attribute.id'] = array(1,2,3); 
$this->Practise->find('all', array('conditions' => $cond)); 

그 때 나는 다음과 같은 오류가 발생합니다. '의'ID는 where 절 '

SQL 쿼리 :

는 012을 선택. id, Practise. title, Practise. body, 부터 db. practisesPractise 어디에서 Attribute. id IN (1, 2, 3)

어떻게 CakePHP를 만들 수 있습니까? HABTM 테이블을 찾기 쿼리에 가입시킬 수 있습니까?

답변

0

당신은 예를

수동으로도 가입 할 수 있습니다
$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', 
 
     ) 
 
    ));

+0

이 작동하지 않습니다. 결과 SQL 쿼리에는 Attribute에 대한 조인이 없으며 Attribute.id에 다른 조건도 없습니다. – lockdoc

+0

아마도 도움이됩니다. http://book.cakephp.org/2.0/en/models/associations-linking-models-together.html# 조인 테이블 –