开发者

Doctrine Custom Query Builder Filters

开发者 https://www.devze.com 2023-03-01 05:15 出处:网络
This is the situation (the code below does not working): if(is_array($filters)) { $f = array(); foreach($filters as $filter)

This is the situation (the code below does not working):

if(is_array($filters))
     {
        $f = array();

        foreach($filters as $filter)
        {
            $f[] = $qb->expr()->like("p.tags","'%" . $filter . "%'");
        }

        $qb->andWhere($qb->expr()->orx($f)开发者_运维知识库);
     }

I need to pass custom/multiple expressions to orx function but i don't know how!!

The orx function sistaxe is:

$qb->expr()->andx($cond1 [, $condN])

Fixed example (Extracted from doctrine doc's):

$qb->add('select', $qb->expr()->select('u'))
->add('from', $qb->expr()->from('User', 'u'))
->add('where', $qb->expr()->orx(
   $qb->expr()->eq('u.id', '?1'),
   $qb->expr()->like('u.nickname', '?2')
))

Help please!!


(answered in a question edit. Converted to a community wiki answer. See What is the appropriate action when the answer to a question is added to the question itself? )

The OP wrote:

Solved,Solution:

 if(is_array($filters))
 {
    foreach($filters as $filter)
    {
        $temp[] = $qb->expr()->like("p.tags","'%" . $filter . "%'");
    }
    $qb->andWhere(call_user_func_array(array($qb->expr(),'orx'), $temp));
 }


Neater solution :)

if(is_array($filters))
{
    $f = array();

    $orCondition = $qb->expr()->orX();
    foreach($filters as $filter)
    {
        $orCondition->add($qb->expr()->like("p.tags", "'%" . $filter . "%'"));
    }

    $qb->andWhere($orCondition);
}

Or you can do:

$orCondition->addMultiple(<conditions_array>);
0

精彩评论

暂无评论...
验证码 换一张
取 消