0

我想用学说映射下面的 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 的

4

1 回答 1

1

当您通过关系加入时,这两个条件都会添加 - 这会添加映射关系的默认条件。

只需更改'customer.attribute'实体的全名,我猜类似CustomerAttribute::class.

于 2017-04-04T20:24:36.450 回答