How to specify several join conditions for 1:1 relationship in Doctrine 2(如何在 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 关系指定多个连接条件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何在 Doctrine 2 中为 1:1 关系指定多个连接条件
基础教程推荐
- 学说 dbal querybuilder 作为准备好的语句 2022-01-01
- PHP 类:全局变量作为类中的属性 2021-01-01
- 在PHP中根据W3C规范Unicode 2022-01-01
- 有什么方法可以用编码 UTF-8 而不是 Unicode 返回 PHP`json_encode`? 2021-01-01
- 如何在 Laravel 中使用 React Router? 2022-01-01
- PHP PDO MySQL 查询 LIKE ->多个关键词 2021-01-01
- YouTube API v3 点赞视频,但计数器不增加 2022-01-01
- Cron Jobs 调用带有变量的 PHP 脚本 2022-01-01
- 如何在 Laravel 5.3 注册中添加动态下拉列表列? 2021-01-01
- 如何替换eregi() 2022-01-01
