2012-01-30 18 views
0

我有一個'複雜'模型,其中有許多單位。我的模型關聯是正確的,一切正常,只是我無法正確地獲得不同的模型/關聯,並且讓我懷疑是否需要以不同方式設置此模型。CakePHP單元屬於複雜,但不是所有單元都有複雜嗎?

看,並非所有的單位將有一個複雜的關聯它。有些單位是房屋,有些單位是酒店房間,有些單位只是租賃公司。

因爲配合物有許多單位我很想離開它,但我想知道如果我不應該重新定義作爲複雜歸屬單位的關係。我試圖不在數據庫中重複自己,在同一個複合體的多個實例中添加。

這是我當前機組型號:

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

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

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

} 

這裏是我當前複雜的模型:

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






} 

順便說一句,「定位」是我遇到問題的模型(當我在我的視圖中調用我的單元/複雜信息時,我的數組的位置部分當前返回空,所以我需要讓我的關聯正確)。
如果單位是一個複雜的公寓,我希望它返回複雜的位置。如果是房子或酒店或租賃公司,我希望它返回單元的位置。我必須做幾個這些關聯(圖像,位置等等),所以我想讓所有事情都直接在前面。

單元和複雜度之間的當前關係是否正確?還是需要以不同方式定義一切?

UPDATE這是我的控制器邏輯:

$this->paginate['Unit']=array(
     'limit'=>9, 
     'order' => 'RAND()', 
     'contain'=>array(
       'User'=>array(
        'email'), 
       'Complex'=>array(
        'id','complex_name', 'realname', 'photo1','photo2','photo3','photo4','photo5'), 
       'Location'=>array(
        'complex_id','address', 'city','state','zip','area_code','exchange','sln','lat','lon','website') 
       ), 
     'conditions'=>array(
       'Unit.type'=>array('condo', 'rentalco'), 
       'Unit.active'=>1) 
    ); 
$data = $this->paginate('Unit'); 
$this->set('allcondos', $data); 


} 

而且你可以看到結果,我得到here:

我不是目前顯示的所有物的觀點感興趣,但索引頁邏輯確實有ContainableBehavior只限於複雜(原本我將有各種複雜的意見):

public function index() { 
    $this->Complex->Behaviors->attach('Containable'); 
    $this->Complex->contain(); 
    $c=$this->Complex->find('all'); 
    $this->set('complexes', $c); 
    } 

更新我現在就開始工作,對於遇到此問題的任何人。我必須將位置更改爲根,並使複雜屬於位置。這裏是我的模型更新:

class Location extends AppModel { 
    public $name='Location'; 
    public $hasMany=array('Complex', 'Unit'); 
    var $belongsTo=array(


    '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'); 

} 

非常感謝Wylie的幫助,這是一個很簡單!

+0

你的應用程序中的哪個位置是'find()'調用將「Location」留空? – Wylie 2012-01-30 18:06:31

+0

$這 - >分頁[ '單元'] =陣列( \t \t \t '限制'=> 9, \t \t \t '順序'=> 'RAND()', \t \t \t '包含'=>數組( \t \t \t \t \t '用戶'=>數組( \t \t \t \t \t \t '電子郵件'), \t \t \t \t \t '複雜的'=>數組( \t \t \t \t \t \t 'ID', 'complex_name', '真實姓名', '照片1', '照片2', '照片3', '照片4', '照片5'), \t \t \t \t \t '位置'=>數組( \t \t \t \t \t \t 'complex_id', '地址', '城市', '狀態', '拉鍊', 'AREA_CODE', '交換', 'SLN', 'lat','lon','website') \t \t \t \t \t), \t \t \t '條件'=>數組( \t \t \t \t \t 'Unit.type'=>數組( '公寓', 'rentalco'), \t \t \t \t \t 'Unit.active'=> 1) \t \t \t); \t $ data = $ this-> paginate('Unit'); \t $ this-> set('allcondos',$ data); – huzzah 2012-01-30 18:59:43

+0

所有這些模型都有ContainableBehavior嗎?你可以編輯OP併發布查找結果嗎? – Wylie 2012-01-30 20:31:33

回答

1

http://book.cakephp.org/2.0/en/core-libraries/behaviors/containable.html?containing-deeper-associations#containing-deeper-associations

When using ‘fields’ and ‘contain’ options - be careful to include all foreign keys that your query directly or indirectly requires. Please also note that because Containable must to be attached to all models used in containment, you may consider attaching it to your AppModel.

你可以在你的AppModel這樣的:public $actsAs = array('Containable'); 您不必使用包含您在不想所以應該不會礙事。

當模型A具有模型B時,可以使用模型B中的外鍵。因此,位置上的外鍵應該是complex_id。儘管如此,不應該如此,因爲並非所有單位都有複雜的情況。

在我看來,你應該讓位置成爲「根」。所以,Complex屬於Location和Unit屬於Complex和Location。然後位置有一個/許多複雜的等等。或者只是把地址列放在Unit/Complex表中。

+0

好的,在改變了我的關聯和混搭後,我開始工作,發佈我在OP中做的事情。感謝您的幫助! – huzzah 2012-01-30 22:55:46