【发布时间】:2017-12-21 21:53:08
【问题描述】:
我想创建适当的 b/w 两个表的关系。这样当我从lease 表中获取数据时,我也将能够获取price 表数据。价格表可以有多个 id 为lease 表。
表的结构是
租用表
价格表
其中id 的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',
]);
}
【问题讨论】:
标签: php mysql cakephp model cakephp-3.0