我想将以下SQL语句与学说映射
select
address.*,
(CASE WHEN (customer.default_billing_address_id = address.id) THEN 1 ELSE 0 END) as isDefaultBillingAddress,
(CASE WHEN (customer.default_shipping_address_id = address.id) THEN 1 ELSE 0 END) as isDefaultShippingAddress
from
s_user customer
join
s_user_attributes customerattributes on customerattributes.customer_number = customer.customernumber
join
s_user_addresses address on address.user_id = customer.id
where customerattributes.userID = 214
但是当我使用查询生成器编写原则查询时,我总是得到一些不同的查询:
[...]
FROM s_user_addresses s1_
[...]
INNER JOIN s_user_attributes s5_ ON s0_.id = s5_.userID
INNER JOIN s_user s0_ ON s0_.id = s5_.userID
AND (s5_.customer_number = s0_.customernumber)
WHERE s5_.userID = 214
ORDER BY sclr_0 DESC
,sclr_1 DESC
最后,这是使用查询生成器的代码:
$builder->from(Address::class, 'address')
->andWhere('customerAttribute.customerId = :userId')
->setParameter('userId', $userId)
->join('address.customer', 'customer')
->join('customer.attribute', 'customerAttribute', Join::WITH, 'customerAttribute.customerNumber = customer.number')
->addSelect([
'(CASE WHEN (customer.defaultBillingAddress = address.id) THEN 1 ELSE 0 END) as HIDDEN isDefaultBillingAddress',
'(CASE WHEN (customer.defaultShippingAddress = address.id) THEN 1 ELSE 0 END) as HIDDEN isDefaultShippingAddress',
])
->addOrderBy('isDefaultBillingAddress', 'DESC')
->addOrderBy('isDefaultShippingAddress', 'DESC');
问题是,JOIN
子句得到一个AND
,我只希望customer_number
被连接而不是id的
这两个条件都是在按关系连接时添加的 - 这将添加映射关系的默认条件。
只需用实体的全名更改'customer.attribute'
,我猜是CustomerAttribute::class
.