gpt4 book ai didi

php - 使用 Doctrine (DQL) 加入 Table On Self

转载 作者:行者123 更新时间:2023-11-29 03:15:19 25 4
gpt4 key购买 nike

我正在尝试使用 doctrine 的 DQL 连接一个表。场景是:

我有一个产品属性值表,它通过引用表链接到产品表。产品属性值将用作产品的过滤器,因此,产品属性值表需要重新连接到自身以过滤多个属性。

我的方法如下:

public static function getProductsByCategoryCode($productCategoryCode, $currencyCode, Zend_Session_Namespace $filters = null)
{
$products = Doctrine_Query::create()
->from('Products p')
->leftJoin('p.ProductCategories pc')
->leftJoin('p.ProductPrices pp')
->leftJoin('pp.Currencies c')
->where('pc.product_category_code = '. $productCategoryCode)
->addWhere('c.currency_code = "'. $currencyCode . '"');

if($filters !== null)
{
$i = 0;

foreach($filters as $index => $value)
{
$alias = 'pav_' . (string)$i;

$products
->leftJoin('p.ProductAttributeValues ' . $alias)
->addWhere($alias . '.product_attribute_code = "'. $index . '"')
->addWhere($alias . '.value = "'. $value . '"');

$i++;
}
}

die($products->getSql());

return $products->execute();
}

问题是从 getSql() 调用中获得的结果 SQL 具有产品属性值链接表的重复别名(MySQL 错误:#1066 - 不是唯一的表/别名: 'p6').据推测,这是由于产品和产品属性值之间的引用关系(产品属性值链接)被重用所致。

生成的 SQL 是:

SELECT 
p.product_code AS p__product_code,
p.name AS p__name,
p.description AS p__description,
p2.product_category_code AS p2__product_category_code,
p2.name AS p2__name,
p2.category_route AS p2__category_route,
p2.parent_product_category_code AS p2__parent_product_category_code,
p2.lft AS p2__lft,
p2.rgt AS p2__rgt,
p2.level AS p2__level,
p4.currency_code AS p4__currency_code,
p4.product_code AS p4__product_code,
p4.net_value AS p4__net_value,
c.currency_code AS c__currency_code,
p5.product_attribute_value_id AS p5__product_attribute_value_id,
p5.product_attribute_code AS p5__product_attribute_code,
p5.value AS p5__value,
p7.product_attribute_value_id AS p7__product_attribute_value_id,
p7.product_attribute_code AS p7__product_attribute_code,
p7.value AS p7__value
FROM
products p
LEFT JOIN
product_category_links p3 ON p.product_code = p3.product_code
LEFT JOIN
product_categories p2 ON p2.product_category_code = p3.product_category_code
LEFT JOIN
product_prices p4 ON p.product_code = p4.product_code
LEFT JOIN
currencies c ON p4.currency_code = c.currency_code
LEFT JOIN
product_attribute_value_links p6 ON p.product_code = p6.product_code
LEFT JOIN
product_attribute_values p5 ON
p5.product_attribute_value_id = p6.product_attribute_value_id
LEFT JOIN
product_attribute_value_links p6 ON p.product_code = p6.product_code
LEFT JOIN
product_attribute_values p7 ON
p7.product_attribute_value_id = p6.product_attribute_value_id
WHERE
p2.product_category_code = 10100
AND
c.currency_code = "GBP"
AND
p5.product_attribute_code = "BRAND"
AND
p5.value = "Advanced Hair Gear"
AND
p7.product_attribute_code = "SIZE" AND p7.value = "238 ml"

如您所见,P6 作为别名出现了两次。

有人知道使用 DQL 实现此目的的正确方法吗?

非常感谢。

最佳答案

我不知道是否有更简单的解决方案,但您应该能够自己指定连接:

        ->leftJoin('p.ProductAttributeValuesLinks ' . $alias1)
->leftJoin($alias1 '.ProductAttributeValue ' . $alias2)

如果您在链接表中设置如下内容:

  relations:
Product:
class: Products
foreignType: many
ProductAttributeValue:
class: ProductAttributeValues
foreignType: many

关于php - 使用 Doctrine (DQL) 加入 Table On Self,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/380776/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com