我有3个表,categories
,products
和seller_products
及其关联就像 CategoriesTable.phpCakePHP的3:多级联想
$this->hasMany('Products', [
'foreignKey' => 'category_id'
]);
ProductsTable.php
$this->hasMany('SellerProducts', [
'foreignKey' => 'product_id'
]);
$this->belongsTo('Categories', [
'foreignKey' => 'category_id',
'joinType' => 'INNER'
]);
SellerProductsTable.php
$this->belongsTo('Products', [
'foreignKey' => 'product_id',
'joinType' => 'INNER'
]);
现在,在categories
的view
(site.com/categories/view/2
),我不得不从products
和sellerProducts
选择所有产品数据,其中产品所属类别ID,也存在于sellerProducts。 即,
products.category_id = $id AND sellerProducts.product_id = Products.id
有CakePHP中3任何简单的方法来获得结果呢?
编辑2
这就是我正在努力。在CategoriesController.php
$this->loadModel('Products');
$sellerProducts = $this->Products->find('all', [
'conditions' => [
'category_id' => $id
],
'joins' => [
'table' => 'SellerProducts',
'alias' => 'SellerProducts',
'type' => 'INNER',
'conditions' => [
'SellerProducts.product_id' => 'Products.id',
'stock >' => 0
]
]
]);
debug($sellerProducts);
foreach($sellerProducts as $a) {
debug($a);
}
上debug
view()
行动也只给出从Products
表,但没有从SellerProducts
似乎是一个非常简单的任务,你到底尝试了什么? – arilia
@arilia参见'edit 2' –