如何在 Doctrine 2 中为 1:1 关系指定多个连接条件

2022-01-16 00:00:00 php doctrine doctrine-orm

文档状态:

class Cart
{
    // ...

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

    // ...
}

这个注解代表这样的sql:

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>

有什么解决办法吗?

UPD:

我现在真正需要的附加条件是 <const>c.f1 和 c.f2 之间

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

推荐答案

你可以使用 WITH 关键字来指定额外的连接条件,你可以在一些 例子.

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

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

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)

还要注意对 CURRENT_TIMESTAMP() 的调用,它转换为 MySQL 的 NOW().查看其他非常有用的聚合函数和表达式的列表 这里

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

相关文章