CakePHP 获得协会的协会
CakePHP get associations of assotiations
我有一个属于产品 table 的报价 table。产品 table 属于标记 table(带外键:markup_id)。
现在,当我尝试这段代码时:
$offersObject = $this->paginate($this->Offers->find('all')
->contain('Products');
我得到结果:
Array
(
[0] => App\Model\Entity\Offer Object
(
[id] => 1
[store_id] => 1
[product_id] => 1
[price] => 150
[new_price] => 0
[status] => 1
[twid] => 70261
[magkod] => MAG
[product] => App\Model\Entity\Product Object
(
[id] => 1
[name] => BOSCH S3 41Ah 360A S3001
[markup_id] => 3
[created] => Cake\I18n\FrozenTime Object
(
[time] => 2018-01-08T14:30:38+00:00
[timezone] => UTC
[fixedNowTime] =>
)
[modified] => Cake\I18n\FrozenTime Object
(
[time] => 2018-01-08T14:30:38+00:00
[timezone] => UTC
[fixedNowTime] =>
)
[[repository]] => Products
)
)
问题是我确实得到了 markup_id,我需要的是 Products table.
的关联行
来自我的 OffersTable.php:
$this->belongsTo('Products', [
'foreignKey' => 'product_id',
'joinType' => 'INNER'
]);
ProductsTable.php
$this->hasMany('Offers');
$this->belongsTo('Markups', [
'foreignKey' => 'markup_id',
'joinType' => 'INNER'
]);
请指教。或要求任何说明。
您可以使用嵌套数组加载嵌套关联:
$offersObject = $this->paginate($this->Offers->find('all')
->contain([
'Products' => [
'Markups'
]
]);
或者,您可以使用点符号表示嵌套关联:
$offersObject = $this->paginate($this->Offers->find('all')
->contain([
'Products.Markups'
]);
我有一个属于产品 table 的报价 table。产品 table 属于标记 table(带外键:markup_id)。
现在,当我尝试这段代码时:
$offersObject = $this->paginate($this->Offers->find('all')
->contain('Products');
我得到结果:
Array
(
[0] => App\Model\Entity\Offer Object
(
[id] => 1
[store_id] => 1
[product_id] => 1
[price] => 150
[new_price] => 0
[status] => 1
[twid] => 70261
[magkod] => MAG
[product] => App\Model\Entity\Product Object
(
[id] => 1
[name] => BOSCH S3 41Ah 360A S3001
[markup_id] => 3
[created] => Cake\I18n\FrozenTime Object
(
[time] => 2018-01-08T14:30:38+00:00
[timezone] => UTC
[fixedNowTime] =>
)
[modified] => Cake\I18n\FrozenTime Object
(
[time] => 2018-01-08T14:30:38+00:00
[timezone] => UTC
[fixedNowTime] =>
)
[[repository]] => Products
)
)
问题是我确实得到了 markup_id,我需要的是 Products table.
的关联行来自我的 OffersTable.php:
$this->belongsTo('Products', [
'foreignKey' => 'product_id',
'joinType' => 'INNER'
]);
ProductsTable.php
$this->hasMany('Offers');
$this->belongsTo('Markups', [
'foreignKey' => 'markup_id',
'joinType' => 'INNER'
]);
请指教。或要求任何说明。
您可以使用嵌套数组加载嵌套关联:
$offersObject = $this->paginate($this->Offers->find('all')
->contain([
'Products' => [
'Markups'
]
]);
或者,您可以使用点符号表示嵌套关联:
$offersObject = $this->paginate($this->Offers->find('all')
->contain([
'Products.Markups'
]);