CakePHP 3 中的子查询?
Subqueries in CakePHP 3?
根据 CakePHP BelongsToMany 约定,我有两个 table products
和 product_categories
通过第三个 table、products_categories_products
关联(编辑: 这些协会建立在 ProductsTable.php
和 ProductCategoriesTable.php
)。我想生成一个产品类别列表,使用最畅销产品的图像来代表每个类别。
我可以使用以下函数实现我想要的结果:
public function findImages(Query $query, array $options) {
$query->select([
'ProductCategories.id',
'ProductCategories.name',
'ProductCategories__image' => '(SELECT Products.image
FROM product_categories_products AS ProductCategoriesProducts
LEFT JOIN products AS Products
ON ProductCategoriesProducts.product_id = Products.id
WHERE ProductCategoriesProducts.product_category_id = ProductCategories.id
ORDER BY Products.turnover DESC
LIMIT 1)'
])->where([
'ProductCategories.name <>' => 'Unsorted'
])->order([
'ProductCategories.name' => 'asc'
]);
return $query;
}
这是接受table还是有更简单的方法来实现我的目标?我真的找不到任何关于在 CakePHP 3 中制作子查询的主题。经过几个小时的挫折,我只是偶然发现了上述解决方案。任何建议表示赞赏!
感谢用户 ndm_yesterday 的 link,我生成了以下解决方案:
public function findImages(Query $query, array $options) {
$this->hasMany('ProductCategoriesProducts');
$subquery = $this->ProductCategoriesProducts->find('all')->select([
'Products.image'
])->join([
[
'table' => 'products',
'alias' => 'Products',
'type' => 'LEFT',
'conditions'=> [
'ProductCategoriesProducts.product_id = Products.id'
]
]
])->where([
'ProductCategoriesProducts.product_category_id = ProductCategories.id'
])->order([
'Products.turnover' => 'DESC'
])->limit(1);
$query->select([
'ProductCategories.id',
'ProductCategories.name',
'ProductCategories__image' => $subquery
])->where([
'ProductCategories.name <>' => 'Unsorted'
])->order([
'ProductCategories.name' => 'asc'
]);
return $query;
}
根据 CakePHP BelongsToMany 约定,我有两个 table products
和 product_categories
通过第三个 table、products_categories_products
关联(编辑: 这些协会建立在 ProductsTable.php
和 ProductCategoriesTable.php
)。我想生成一个产品类别列表,使用最畅销产品的图像来代表每个类别。
我可以使用以下函数实现我想要的结果:
public function findImages(Query $query, array $options) {
$query->select([
'ProductCategories.id',
'ProductCategories.name',
'ProductCategories__image' => '(SELECT Products.image
FROM product_categories_products AS ProductCategoriesProducts
LEFT JOIN products AS Products
ON ProductCategoriesProducts.product_id = Products.id
WHERE ProductCategoriesProducts.product_category_id = ProductCategories.id
ORDER BY Products.turnover DESC
LIMIT 1)'
])->where([
'ProductCategories.name <>' => 'Unsorted'
])->order([
'ProductCategories.name' => 'asc'
]);
return $query;
}
这是接受table还是有更简单的方法来实现我的目标?我真的找不到任何关于在 CakePHP 3 中制作子查询的主题。经过几个小时的挫折,我只是偶然发现了上述解决方案。任何建议表示赞赏!
感谢用户 ndm_yesterday 的 link,我生成了以下解决方案:
public function findImages(Query $query, array $options) {
$this->hasMany('ProductCategoriesProducts');
$subquery = $this->ProductCategoriesProducts->find('all')->select([
'Products.image'
])->join([
[
'table' => 'products',
'alias' => 'Products',
'type' => 'LEFT',
'conditions'=> [
'ProductCategoriesProducts.product_id = Products.id'
]
]
])->where([
'ProductCategoriesProducts.product_category_id = ProductCategories.id'
])->order([
'Products.turnover' => 'DESC'
])->limit(1);
$query->select([
'ProductCategories.id',
'ProductCategories.name',
'ProductCategories__image' => $subquery
])->where([
'ProductCategories.name <>' => 'Unsorted'
])->order([
'ProductCategories.name' => 'asc'
]);
return $query;
}