在doctrine 2实体的存储库中如何使用复杂的标准? [英] How do I use a complex criteria inside a doctrine 2 entity's repository?

查看:99
本文介绍了在doctrine 2实体的存储库中如何使用复杂的标准?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

说我有一张表,可以保存有关节日的信息。

每个节日都有一个开始和结束日期。

Lets say I have a table that holds information about festivals.
Each festival has a start and end date.

我想选择所有在给定日期的活动(发生)的节日。

意思是,我想选择他们的开始日期在给定日期之前或之后的所有节日,并且他们的结束日期是在或者在相同的给定日期。

I want to select all the festivals that are live (that happen) on a given date.
Meaning, I want to select all the festivals that their start date is before or on a given date, and that their end date is after or on a the same given date.

所以我继续到节日实体的存储库类,并创建了一个方法来做到这一点。

但是标准参数findByexpects是一个数组,所有的例子只能作为一个简单的标准(例如array('name'=>'billy'))将选择所有具有值billy的行

So I went on to the repository class of the festival entity, and created a method to do just that.
But the criteria argument "findBy" expects is an array, which all the examples only treat as a simple criteria (eg. "array('name' => 'billy')" will select all the rows that have the value billy in their name column), which uses only the comparison operator.

如何使用其他运算符,如

How can I use other operators such as

>, <, !=, IN, NOT IN, LIKE    

等?

谢谢

推荐答案

您将需要(可能使用DQL),如果你想要的东西,具体到写自己的查询。我相信,建立在findBy方法更只是抓住对象很快,如果你有较少的具体标准。我不知道你的实体名称或他们的存储位置。可能会像您的节日存储库中的一样。

You'll need to write your own query (probably using DQL) if you want something that specific. I believe the built in "findBy" methods are more for just grabbing objects quickly if you have less specific criteria. I don't know your entity names or where they are stored. Could be something like this as a function in your Festival Repository.

public function findActiveFestivals($start, $end)
{
    $qb = $this->_em->createQueryBuilder();
    $qb->select('f')
        ->from('Festival', 'f')
        ->where('f.start >= :start')
        ->andWhere('f.end <= :end')
        ->setParameters(array('start' => $start, 'end' => $end));

    return $qb->getQuery()->getArrayResult();
}

这篇关于在doctrine 2实体的存储库中如何使用复杂的标准?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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