I have 3 tables, categories
, products
and seller_products
and their associations are like
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'
]);
Now, In the view
of categories
(site.com/categories/view/2
), I have to select all products data from products
and sellerProducts
where products belongs to category id and also exists in sellerProducts.
ie.,
products.category_id = $id AND sellerProducts.product_id = Products.id
Is there any easy way in CakePHP 3 to get the result ?
Edit 2
This is what I'm trying. In view()
action of 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);
}
on debug
it gives only the data from Products
table but not from SellerProducts