2017-07-17 78 views
0

我想创建适当的关系b/w两个表。因此,当我从lease表中获取数据时,我也可以获得price表数据。价格表可以具有lease表的多个ID。在cakephp中创建适当的关联3

表是

表租赁

enter image description here

价格一览

enter image description here

id邻的结构f lease表格与lease_id价格表相关。

我已经尝试使用下面的代码,但无法获取数据。你描述

class LeasesTable extends Table { 

    /** 
    * Initialize method 
    * 
    * @param array $config The configuration for the Table. 
    * @return void 
    */ 
    public function initialize(array $config) { 
     parent::initialize($config); 

     $this->table('leases'); 

     $this->primaryKey('id'); 

     $this->addBehavior('Timestamp'); 

     $this->belongsTo('Offices', [ 
      'foreignKey' => 'office_type', 
      'joinType' => 'INNER' 
     ]); 
     $this->belongsTo('Countries', [ 
      'foreignKey' => 'country_id', 
      'joinType' => 'INNER' 
     ]); 
     $this->belongsTo('Areas', [ 
      'foreignKey' => 'area_id', 
      'joinType' => 'INNER' 
     ]); 
     $this->belongsToMany('id', [ 
      'foreignKey' => 'lease_id', 
      'joinType' => 'INNER', 
      'joinTable' => 'Prices', 
     ]); 
    } 

回答

2

协会是租赁有许多价格 - 所以你应该LeasesTable.php使用下面的代码:

$this->hasMany("Prices", [ 
    "foreignKey" => "lease_id" 
]); 

更多信息:HasMany Associations