0

我正在尝试为我的数据库构建过滤器。基于几个表单输入的值,我需要构造适当的查询。这是代码:

    $qb = $em->createQueryBuilder();
            $qb->select('c.title')
            ->from('Baza\BlogBundle\Entity\Conferences', 'c');         
    $conditions=array(
        $qb->expr()->gt('c.title',$enquiry->getNaziv()) );//First condition 

    if ($enquiry->getKontakt()!=null) {
         $conditions[] = $qb->expr()->lt('c.contactemail',$enquiry->getKontakt())}//Adding second, optional condition

    $conditions = call_user_func_array(array($qb, 'andX'), $conditions);
    $qb->where($conditions);
    $query = $qb->getQuery();

当我运行它时,我得到以下异常:

警告:call_user_func_array() 期望参数 1 是一个有效的回调,类 'Doctrine\ORM\QueryBuilder' 没有方法 'andX'

*编辑 call_user_func_array() 修改后,我收到以下错误:

QueryException: SELECT c.title FROM Baza\BlogBu​​ndle\Entity\Conferences c WHERE c.title > ITRO AND c.contactemail < somemail@yahoo.com

显然,查询的格式不正确。我在关注这篇文章:Doctrine DQL 条件查询

有什么想法吗?

4

4 回答 4

2

我使用以下代码构建了动态查询:

$em = $this->getDoctrine()->getEntityManager();

            $naziv = $enquiry->getNaziv();
            $drzava=$enquiry->getDrzava();
            $oblast=$enquiry->getOblast();
            $grad=$enquiry->getGrad();
            $provincija=$enquiry->getProvincija();
            $kontakt=$enquiry->getKontakt();
            $sajt=$enquiry->getSajt();

            $testquery = "SELECT c.title FROM Baza\BlogBundle\Entity\Conferences c JOIN c.topicid t JOIN c.locationid l JOIN l.stateid s WHERE c.lh = :lh";
            $testparam ['lh'] = $lh;

            if ($kontakt != '') {
                    $testquery .= " and c.contactemail = :kontakt";
                    $testparam['kontakt'] = $kontakt;
            }
            if ($drzava != '') {
                    $testquery .= " and s.name = :drzava";
                    $testparam['drzava'] = $drzava;
            }

            if ($provincija != '') {
                    $testquery .= " and l.province = :provincija";
                    $testparam['provincija'] = $provincija;
            }

             if ($grad != '') {
                    $testquery .= " and l.city = :grad";
                    $testparam['grad'] = $grad;
            }

             if ($sajt != '') {
                    $testquery .= " and c.siteurl = :sajt";
                    $testparam['sajt'] = $sajt;
            }

             if ($oblast != '') {
                    $testquery .= " and t.name = :oblast";
                    $testparam['oblast'] = $oblast;
            }

            $query = $em->createQuery($testquery)->setParameters($testparam);
            $result = $query->getResult();
于 2013-01-08T12:21:54.523 回答
0
  1. andx 都是小写
  2. andx 是 expr() 的函数,而不是查询生成器的函数:

    $conditions = call_user_func_array(array($qb->expr(), 'andx'), $conditions);

于 2013-01-07T23:43:57.117 回答
0

关于您的编辑:

$qb = $em->createQueryBuilder();
            $qb->select('c')
            ->from('Baza\BlogBundle\Entity\Conferences', 'c');

使用学说(作为 ORM)选择整个对象。您仍然可以在字段上使用 where 子句。此外 gt 和 lt 搜索字符串并不是真正正确的 sql 实践。如果字母大小写不正确,它们可能会产生奇怪的结果。

于 2013-01-08T00:35:19.450 回答
0

多年后,这就是我的做法:

    if (array_key_exists('phone', $filters)) {
        $qb->andWhere('p.phone LIKE :phone')
        ->setParameter(':phone', '%' . $filters['phone'] . '%');
    }

希望这对某人有帮助

于 2020-05-06T02:44:35.740 回答