2017-07-17 81 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