我有 3 个表,categories
它们products
的seller_products
关联就像
CategoriesTable.php
$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'
]);
现在,在view
( categories
)site.com/categories/view/2
中,我必须选择所有产品数据products
,sellerProducts
其中产品属于类别 id 并且也存在于卖方产品中。IE。,
products.category_id = $id AND sellerProducts.product_id = Products.id
CakePHP 3 中是否有任何简单的方法来获得结果?
编辑 2
这就是我正在尝试的。在view()
行动中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
它只给出表中的数据,而Products
不是来自SellerProducts