在 cakephp 3 中创建适当的关联
create proper association in cakephp 3
我想建立适当的关系 b/w 两个 table。因此,当我从 lease
table 获取数据时,我也将能够获取 price
table 数据。价格 table 可以有多个 ID lease
table.
table的结构是
租赁Table
价格Table
其中 lease
的 id
table 与价格 table 的 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',
]);
}
您描述的协会是租赁 有许多 价格 - 因此您应该在 LeasesTable.php 中使用以下代码:
$this->hasMany("Prices", [
"foreignKey" => "lease_id"
]);
更多信息:HasMany Associations
我想建立适当的关系 b/w 两个 table。因此,当我从 lease
table 获取数据时,我也将能够获取 price
table 数据。价格 table 可以有多个 ID lease
table.
table的结构是
租赁Table
价格Table
其中 lease
的 id
table 与价格 table 的 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',
]);
}
您描述的协会是租赁 有许多 价格 - 因此您应该在 LeasesTable.php 中使用以下代码:
$this->hasMany("Prices", [
"foreignKey" => "lease_id"
]);
更多信息:HasMany Associations