dpbrrczhlwbv849228 2017-04-04 12:56
浏览 44
已采纳

Query Builder自定义on子句

I want to map the following SQL statement with doctrine

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

But when I write my doctrine query with the query builder I always get the query a little different:

[...]
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

And finally this is the code that uses the query builder:

$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');

The problem is, that the JOIN clause gets an AND, I just want that the customer_number gets joined not the id's

  • 写回答

1条回答 默认 最新

  • douyanguo7964 2017-04-04 20:24
    关注

    Both conditions are added as you join by relation - this adds default condition by which relation is mapped.

    Just change 'customer.attribute' with full name of the entity, I guess something like CustomerAttribute::class.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100