查询生成器自定义 on 子句

Query Builder custom on clause

我想将以下 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.