У меня есть 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'
]);
ПродавецProductsTable.php
$this->belongsTo('Products', [
'foreignKey' => 'product_id',
'joinType' => 'INNER'
]);
Теперь, в view
из categories
(site.com/categories/view/2
), я должен выбрать все данные продукты из products
и sellerProducts
где продукция относится к категории идентификатору, а также существует в sellerProducts. т.е..,
products.category_id = $id AND sellerProducts.product_id = Products.id
Есть ли простой способ CakePHP 3, чтобы получить результат?
Edit 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
Кажется очень простой задачей, что вы делали до сих пор? – arilia
@arilia см. 'Edit 2' –