我正在创建一个包含以下表格的电子商务网站:
articles
sizes
colors
orders
articles_colors(article_id,color_id)
articles_sizes(article_id,size_id)
articles_orders(article_id,order_id,size_id,color_id,quantity)
我的表格文件:
ArticlesTable.php
$this->belongsToMany('Colors', [
'foreignKey' => 'article_id',
'targetForeignKey' => 'color_id',
'joinTable' => 'articles_colors'
]);
$this->belongsToMany('Orders', [
'through' => 'ArticlesOrders'
]);
$this->belongsToMany('Sizes', [
'foreignKey' => 'article_id',
'targetForeignKey' => 'size_id',
'joinTable' => 'articles_sizes'
]);
OrdersTable.php
$this->belongsToMany('Articles', [
'through'=>'ArticlesOrders'
]);
ArticlesOrdersTable.php
$this->belongsTo('Articles', [
'foreignKey' => 'article_id',
'joinType' => 'INNER'
]);
$this->belongsTo('Orders', [
'foreignKey' => 'order_id',
'joinType' => 'INNER'
]);
$this->belongsTo('Sizes', [
'foreignKey' => 'size_id',
'joinType' => 'INNER'
]);
$this->belongsTo('Colors', [
'foreignKey' => 'color_id',
'joinType' => 'INNER'
]);
问题在于,当我收到订单时,我不知道如何在Article对象中获取joinData的大小和颜色。 例如:
$order = $this->Orders->get($id, [
'contain' => ['Articles']
]);
结果:
articles: [
'_joinData' => object(App\Model\Entity\ArticlesOrder) {
'article_id' => (int) 2,
'id' => (int) 1,
'order_id' => (int) 57,
'price' => (float) 100,
'color_id' => (int) 1,
'size_id' => (int) 2,
//I would like to have the size and the color object
]
感谢您的帮助:)