文档说明:
class Cart
{
// ...
/**
* @OneToOne(targetEntity="Customer", inversedBy="cart")
* @JoinColumn(name="customer_id", referencedColumnName="id")
*/
private $customer;
// ...
}
这个注释代表了这样的sql:
JOIN Customer c ON c.id = cart.customer_id
问题是我需要在那里添加额外的比较,例如:
JOIN Customer c ON c.id = cart.customer_id AND c.anotherField = <constant>
任何解决方案?
UPD :
我现在需要的真正附加条件是<const> BETWEEN c.f1 AND c.f2
答案 0 :(得分:0)
您可以使用WITH
关键字指定其他加入条件,您可以在examples中查看。
我认为这应该让你前进:
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)
还会注意到CURRENT_TIMESTAMP()
的调用,该调用会转换为MySQL的NOW()
。查看其他非常有用的聚合函数和表达式列表here
答案 1 :(得分:0)
似乎没有任何解决方案可以解决你的问题,即学说可以自动神奇地进行。
由于@ficuscr已经为您提供了查询解决方案,因此只需要处理一件事 - 检查您的其他条件并在成功时返回getter中的Customer实例,并在未能满足其他条件时返回NULL。
class Cart
{
const VALUE = '<some_constant_value>';
/**
* @OneToOne(targetEntity="Customer", inversedBy="cart")
* @JoinColumn(name="customer_id", referencedColumnName="id")
*/
private $customer;
// ...
/**
* @return Customer|null
*/
public function getCustomer()
{
if ($this->customer->getField1() <= self::VALUE
&& $this->customer->getField2() >= self::VALUE
) {
return $this->customer;
}
return null;
}
}
如果这是一对多关系,可以使用Collection Filtering API(a.k.a. Criteria)来过滤映射创建的集合:
use Doctrine\Common\Collections\Criteria;
class Cart
{
const VALUE = '<some_constant_value>';
/**
* @OneToMany(targetEntity="Customer", mappedBy="cart")
*/
private $customers;
// ...
/**
* @return Customer[]|ArrayCollection
*/
public function getCustomers()
{
$expr = Criteria::expr();
$criteria = Criteria::create()
->where($expr->andX(
$expr->lte('field1', self::VALUE),
$expr->gte('field2', self::VALUE)
));
return $this->patientProblems->matching($criteria);
}
}