如何在Doctrine 2中指定1:1关系的几个连接条件 [英] How to specify several join conditions for 1:1 relationship in Doctrine 2

查看:93
本文介绍了如何在Doctrine 2中指定1:1关系的几个连接条件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

文件说明:

  class Cart 
{
// ...

/ **
* @OneToOne(targetEntity =Customer,inversedBy =cart)
* @JoinColumn(name =customer_id,referencedColumnName =id)
* /
private $ customer;

// ...
}

此注释表示这样的sql:

 加入客户c ON c.id = cart.customer_id 
/ pre>

而且问题是我需要添加额外的比较,如:

  JOIN Customer c ON c.id = cart.customer_id AND c.anotherField =< constant> 

任何解决方案?



UPD



现在我需要的真正的附加条件是< const>

解决方案

可以使用 WITH 关键字来指定其他连接条件,您可以在某些示例



我认为这应该得到你去:

  SELECT l,c FROM location 
INNER JOIN客户c
WITH CURRENT_TIMESTAMP()BETWEEN c.f1 AND c.f2
WHERE CURRENT_TIMESTAMP()BETWEEN l.f1和l.f2

我删除了 ON 子句,因为我认为没有必要明确指定连接的 ON 字段,除非它们不是标准(每个实体的ID)



还注意到 CURRENT_TIMESTAMP()的调用,转换为MySQL的 NOW()。查看其他非常有用的聚合函数和表达式的列表 here


Documentation states:

class Cart
{
    // ...

    /**
     * @OneToOne(targetEntity="Customer", inversedBy="cart")
     * @JoinColumn(name="customer_id", referencedColumnName="id")
     */
    private $customer;

    // ...
}

This annotation represents such sql:

JOIN Customer c ON c.id = cart.customer_id

And the issue is that I need to add additional comparison there, like:

JOIN Customer c ON c.id = cart.customer_id AND c.anotherField = <constant>

Any solutions for that?

UPD:

the real additional condition I need for now is <const> BETWEEN c.f1 AND c.f2

解决方案

you can use the WITH keyword to specify additional join conditions, as you can see in some of the examples.

i think this should get you going:

SELECT l, c FROM location
INNER JOIN Customer c
WITH CURRENT_TIMESTAMP() BETWEEN c.f1 AND c.f2
WHERE CURRENT_TIMESTAMP() BETWEEN l.f1 AND l.f2

i removed the ON clause because i think there's no need to explicitly specify the join's ON fields unless they are not the "standard" ones (id of each entity)

also notice the call to CURRENT_TIMESTAMP() which translates into MySQL's NOW(). check out a list of other pretty useful aggregate functions and expresions here

这篇关于如何在Doctrine 2中指定1:1关系的几个连接条件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆