2012-12-04 3 views
0

나는 FilesController에 있는데 그 명령이 현재 사용자에게 속한 조건을 기반으로 파일을 가져 오려고합니다.CakePHP - 어떻게 조인 된 테이블의 테이블에 가입합니까?

FilesController

// Check the file exists and that it belongs to the user 

$this->File->find('first', array(
    'conditions' => array(
     'File.id' => $id, 
     'Order.Customer.id' => $this->Auth->user('id') 
    ), 
    'recursive' => 2 
)); 

그때 files 위에 orders에 가입 왼쪽에있는 SQL을 얻으려고 노력하고있어

Unknown column 'Order.Customer.id' in 'where clause' 

케이크 SQL 오류 orderscustomers 가입 왼쪽,하지만 난 전에 고객 테이블에 참여하는 방법을 알아낼 수는 없지만, 전에 해봤 다. 나는 조건으로 생각할 수있는 모든 것을 시도했고, contains을 사용했다. 여기

내 모델 관계이다 :

고객 모델

class Customer extends AppModel { 

    public $hasMany = array('Order'); 

} 

주문 모델

class Order extends AppModel { 

    public $belongsTo = array('Customer'); 

    public $hasMany = array('File'); 

} 

파일 모델

class File extends AppModel { 

    public $belongsTo = array('Order'); 

} 

답변

0

내가 실제로이 경우에 고객 테이블에 가입 할 필요가 없습니다 실현 때문에, 같은

0

'joins'매개 변수를 사용하여 테이블을 결합 해보십시오. 때로는 '포함'이 작동하지 않으며이 문제를 다시 해결해야합니다.

$this->File->find('first', array(
    'conditions' => array(
     'File.id' => $id, 
     'Order.Customer_id' => $this->Auth->user('id') 
    ), 
    'joins' => array(
     array(
      'table' => 'orders', 
      'alias' => 'Order', 
      'type' => 'LEFT', 
      'conditions' => array('File.orders_id = Order.id') 
     ), 
     array(
      'table' => 'customers', 
      'alias' => 'Customer', 
      'type' => 'LEFT', 
      'conditions' => array('Customer.orders_id = Order.id') 
     ) 
    ) 
)); 
0

당신은 가장 쉬운 해결책은 그대로 함유 성 (http://book.cakephp.org/2.0/en/core-libraries/behaviors/containable.html)를 사용할 수 있습니다. Cake는 그런 조건을 중첩하지 않으므로 Order.Customer.id를 사용할 수 없습니다. 수동 조인도 가능합니다. 로

pr($customer['Order']); 

파일을 액세스 할 수 있습니다 :

pr($customer['File']); 
+0

예제가 있습니까? 나는 '포함하다'와 함께 일할 수있는 어떤 것도 얻을 수 없었다. – BadHorsie

0
$this->loadModel('Customer'); 
$customer = $this->Customer->findById($this->Auth->user('id'), array(
     'conditions' => array(
      'File.id' => $id 
     ), 
     'recursive' => 2 
    )); 

주문은 액세스 할 수 있습니다 orders 테이블에는 이미 customer_id가 있습니다.

$this->File->find('first', array(
    'conditions' => array(
     'File.id' => $id, 
     'Order.customer_id' => $this->Auth->user('id') 
    ) 
)); 
관련 문제