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

How to specify several join conditions for 1:1 relationship in Doctrine 2(如何在 Doctrine 2 中为 1:1 关系指定多个连接条件)
本文介绍了如何在 Doctrine 2 中为 1:1 关系指定多个连接条件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

文档状态:

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

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

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Convert JSON integers and floats to strings(将JSON整数和浮点数转换为字符串)
in php how do I use preg replace to turn a url into a tinyurl(在php中,如何使用preg替换将URL转换为TinyURL)
all day appointment for ics calendar file wont work(ICS日历文件的全天约会不起作用)
trim function is giving unexpected values php(Trim函数提供了意外的值php)
Basic PDO connection to MySQL(到MySQL的基本PDO连接)
PHP number_format returns 1.00(Php number_Format返回1.00)