2017-07-17 15 views
0

私は適切な関係を作成したいと思います。だから私がleaseテーブルからデータを取得すると、priceテーブルデータも取得できるようになります。価格表はleaseの複数のIDを持つことができます。cakephpで適切な関連付けを作成する3

テーブルである

リーステーブル

enter image description here

料金表

enter image description here

id Oの構造価格表のlease_idに関連する表f lease

私は次のコードで試してみましたが、データを取得できませんでした。あなたが説明

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

関連する問題