2012-01-30 20 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