Query Builder自定义on子句(Query Builder custom on clause)
我想用doctrine映射以下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
但是当我用查询构建器编写我的doctrine查询时,我总是得到一个不同的查询:
[...] 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
加入而不是idI 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 anAND
, I just want that thecustomer_number
gets joined not the id's
原文:https://stackoverflow.com/questions/43207925
更新时间:2022-10-07 15:10
最满意答案
无边落木萧萧下 不尽长江滚滚来
相关问答
更多-
我会用词语组成一句话。 1.画画 电脑 老师 我们 教 用 &nb...[2022-10-19]
1.老师用计算机教我们画画。 2.小马和小牛爱吃草。 3.叶子一片片从树上落下来。 4.我们的小学堂最漂亮。 -
青苹果叶子黄了(专业爱花人士请指点)[2023-09-23]
如果你是北方城市,叶面喷水这一项就要省去,北方天气不如南方,南方湿热,叶面的水很快会蒸发掉,北方则不同,进入深秋入冬时节,温度过低,叶面重复积水很快会发黄,干枯。还有一点秋冬季节交替时青苹果在北方城市易发生受冻现象。此时更不能叶面喷水。叶面喷水可以选择温度较高的季节。 -
无边落木萧萧下 不尽长江滚滚来
-
万物生 歌词表达的什么意思?[2024-04-29]
万物生梵文版的歌词是经咒“金刚萨埵百字明”。 “空手把锄头,步行骑水牛,人从桥上过,桥流水不流。”如果你能够明白傅大士的这首偈语,也就能明白汉文版歌词的意思了。 类似的歌曲有萨顶顶的《神香》、《陀罗尼》、《妈妈天哪》、齐豫的《大悲咒》、《心经》、《大吉祥天女咒》、朱哲琴的《阿姐鼓》。 -
问题表明S是图中顶点V的子集。 可能存在非叶节点。 但是,您必须确保这些内部节点不在S中。如果S等于V,那么您就是对的。 The question states that S is a subset of the vertices V in the graph. There may be non-leaf nodes. However, you have to make sure that these internal nodes are not in S. If S would be equal to V ...
-
你的解释基本上是归纳的证明,所以是的,我会说它是“合法的”。 关于我们何时拥有单个根的第一条评论是基本案例。 然后给定属性所在的二叉搜索树,您可以解释在通过添加一个节点修改树之后,该属性仍然成立。 这就是归纳步骤。 Your explanation is basically a proof by induction, so yes, I would say that it is 'legit'. Your first comment about when we have a single root is t ...
-
有多少叶子有四叉树?(How many leaves has a quadtree?)[2022-04-25]
考虑通过细分叶节点来扩展四叉树。 该叶节点成为内部节点(将叶数减少一个),并添加四个叶节点。 如果先前的内部节点数是N,则新的内部节点数是N + 1,并且叶的数量是1 + 3 * N-1 + 4 = 1 + 3 *(N + 1)。 一般声明遵循归纳。 Consider expanding a quadtree by subdividing a leaf node. That leaf node becomes an internal node (decreasing the leaf count by on ... -
一种选择是找到两个节点中最不共同的祖先,以及从每个节点到达该祖先的节点序列。 这是算法的草图: 从每个节点开始,向后走到该节点的父节点,直到到达根节点。 计算从每个节点到根的路径上的节点数。 设第一节点的高度为h 1 ,第二节点的高度为h 2 。 设h = min(h 1 ,h 2 )。 这是两个节点中较高者的高度。 从每个节点开始,继续跟随节点的父指针,直到两个节点都处于高度h。 记录在此步骤中您遵循的节点。 此时,两个节点都处于相同的高度。 在找到公共节点之前,请继续从每个节点向上行进到其父节点。 最终 ...
-
递归树遍历,为树的每个叶子返回一个变量(Recursive Tree Traversal, returning a variable for each leaf of the tree)[2022-04-02]
“将这些字符串保存在树外的数组中。” 评论:你确定你必须存储字符串吗? 如果只是存储整数并在递归完成后创建字符串,那就更清晰了。 好的,无论如何(并且没有给出源代码)你只是: 在开始递归之前创建一个足够大的(*)数组 创建一个指针,用于写入数组的不同部分,将该指针初始化为数组的开头。 将指针指向您的递归作为新的/附加函数参数。 每次在递归中到达一片叶子你 写入你在叶子上找到的指针 增加指针(你可以这样做,因为你有指针指针 "save these strings in an array outside of ... -
如何找到树的叶子(how to find leaves of a tree)[2022-07-27]
编辑:我刚看到你可能有一棵有根的树。 您可以创建树的DFS,并且从当前节点开始,您没有边缘到与父节点不同的节点,那么您已经找到了一个叶子。 引用维基百科 : 图中顶点的度数是入射到它的边的数量。 孤立的顶点是零度的顶点; 也就是说,一个不是任何边缘端点的顶点。 叶顶点(也是悬垂顶点)是一度的顶点。 只计算每个节点的程度。 如果它是1那么你有一片叶子。 Edit: I have just seen you probably have a rooted tree. You can make a DFS of t ...