2012-01-31 3 views
0

좋아, 이건 조금 복잡 하네. (어쨌든). 하나의 결과를보기 위해 두 개의 레코드에서 이미지를 가져오고 싶습니다.cakephp는 각 결과에 대해 동일한 테이블에서 하나 이상의 레코드를 가져옵니다.

나는 컴플렉스에 속하는 단위가 있습니다. 각 유닛과 각 컴플렉스는 이미지 테이블에 레코드를 가질 수 있습니다.

특정 단위에 대한 결과로 이미지 단위의 모든 이미지와 그 컴플렉스 (이미지 테이블에도 저장 됨)의 일부 이미지를 표시하고 싶습니다. 여기 내 모델은 다음과 같습니다

$this->paginate['Unit']=array(
     'limit'=>9, 
     'order' => 'RAND()', 
     'contain'=>array(
       'User'=>array('email'), 
       'Complex'=>array('location_id'), 
       'Location', 
       'Image' 
       ), 
     'conditions'=>array(

       'Unit.active'=>1) 
    ); 
$data = $this->paginate('Unit'); 
$this->set('allaccommodations', $data); 

당신은 디버그 here:의 결과를 볼 수 있습니다

class Image extends AppModel { 
    public $name='Image'; 
    var $belongsTo=array(
    'Unit'=>array (
     'className'=>'Unit', 
     'foreignKey'=>'unit_id' 
     ), 
    'Complex'=>array (
     'className'=>'Complex', 
     'foreignKey'=>'complex_id' 
     ), 
    'Restaurant'=>array (
     'className'=>'Restaurant', 
     'foreignKey'=>'restaurant_id' 
     ) 
    ); 


} 

class Complex extends AppModel { 
    public $name='Complex'; 
    public $hasMany=array('Unit'); 
    public $hasOne=array('Image'); 
    public $belongsTo=array(
     'Location'=>array(
      'className'=>'Location', 
      'foreignKey'=>'location_id' 
      ) 
    ); 






} 

class Unit extends AppModel { 
public $name='Unit'; 

public $belongsTo=array(
'User'=>array(
    'className'=>'User', 
    'foreignKey'=>'user' 
), 
'Complex'=>array(
    'className'=>'Complex', 
    'foreignKey'=>'complex_id' 
), 

'Location'=>array (
     'className'=>'Location', 
     'foreignKey'=>'location_id' 
     ), 
); 

public $hasOne=array('Image'); 

} 

가 여기 내 컨트롤러 로직입니다. 유닛에 대한 이미지 데이터를 가져 오지만, unit_id와 complex_id를 가진 Image 배열을 가져와야합니다. 이것이 가능한가?

답변

2

이러한 경우 연관 배열은 동일한 키로 두 값을 가질 수 없으므로 이미지 모델에 대해 다른 별칭을 사용해야합니다.

class Complex extends AppModel { 
    //... 
    public $hasOne=array(
     'ComplexImage' => array(
      'className'=>'Image', 
      'foreignKey'=>'complex_id' 
     ) 
    ); 
} 

class Unit extends AppModel { 
    //... 
    public $hasOne=array(
     'UnitImage' => array(
      'className'=>'Image', 
      'foreignKey'=>'unit_id' 
     ) 
    ); 
} 

그런 다음 쿼리 배열이 변경 것 :

$this->paginate['Unit'] = array(
    'limit' => 9, 
    'order' => 'RAND()', 
    'contain' => array(
     'User' => array('email'), 
     'Complex' => array(
      'fields' => array('id', 'location_id'), 
      'ComplexImage' 
     ), 
     'Location', 
     'UnitImage' 
    ), 
    'conditions' => array(
     'Unit.active' => 1) 
); 

이 또한 모델에 액세스하는 방식에 영향을 미친다. 'ComplexImage'하지

$this->Unit->UnitImage->id;

+0

흠, 난 것을 시도하고, 디버깅 나를 보여주고는 'UnitImage'를 가져옵니다,하지만. Cake : 경고 (512) : 모델 "Unit"은 "ComplexImage"[CORE/Cake/Model/Behavior/ContainableBehavior.php, line 339] 모델과 관련이 없습니다. 코드를 복사하여 붙여 넣었고 이미지 테이블에 complex_id와 unit_id가 모두 있는지 확인했습니다. – huzzah

+0

** 업데이트 ** 단위 모델에 두 별칭을 추가하면 더 이상 관련되지 않은 ComplexImage에 대한 경고가 표시되지 않지만 여전히 UnitImage의 채우기 배열과 ComplexImage의 빈 배열을 반환합니다. – huzzah

+0

페이지 매김 설정을 업데이트했습니다. 'ComplexImage'는 'Unit'과 직접 관련이 없었기 때문에 이동해야했습니다. – Wylie

관련 문제