原则:将 createQuery 查询转换为 createQueryBuilder

发布于 2024-12-21 03:51:01 字数 807 浏览 4 评论 0原文

这是我当前的查询,它工作正常:

$q = $this->_em->createQuery("SELECT s FROM app\models\Sub s 
                              LEFT JOIN s.case c
                              WHERE s.type LIKE '%$type'
                              AND c.id = '$id'");


foreach ($q->getResult() as $row) {
   $results[$row->getId()] = $row;
}

我想将其转换为 QueryBuilder API 结构,这是我到目前为止所拥有的,但它无法正常工作。 (我需要与上面相同格式的结果)。

 $q = $this->_em->createQueryBuilder();

 $q->add('select', 's')
   ->add('from', 'app\models\Sub s')
   ->add('leftJoin', 's.case c')
   ->add('where', 's.type LIKE :type')
   ->add('where', 'c.id = :case');
   ->setParameter('type', "%$type");
   ->setParameter('case', $id);

再说一次,这不能正常工作,而且我不知道如何以与上面相同的格式获得结果。谢谢!

This is the current query that I have, and it works fine:

$q = $this->_em->createQuery("SELECT s FROM app\models\Sub s 
                              LEFT JOIN s.case c
                              WHERE s.type LIKE '%$type'
                              AND c.id = '$id'");


foreach ($q->getResult() as $row) {
   $results[$row->getId()] = $row;
}

I want to convert this to the QueryBuilder API structure, this is what I have so far, but it's not working correctly. (And I need the results as the same format as above).

 $q = $this->_em->createQueryBuilder();

 $q->add('select', 's')
   ->add('from', 'app\models\Sub s')
   ->add('leftJoin', 's.case c')
   ->add('where', 's.type LIKE :type')
   ->add('where', 'c.id = :case');
   ->setParameter('type', "%$type");
   ->setParameter('case', $id);

And again, this isn't working properly, and I don't know how to get the results in the same format as above. Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

祁梦 2024-12-28 03:51:01

接近,但没有雪茄。您需要学习如何使用 Doctrine 附带的 Expr 类来创建正确的 WHERE 子句。

http://www.doctrine -project.org/docs/orm/2.1/en/reference/query-builder.html#expr-classes

在您提供的示例中,您实际上的目标是这样的(未经测试)

$q->add('select', 's')
  ->add('from', 'app\model\Sub s')
  ->add('leftJoin', 's.case c')
  ->add('where', $q->expr()->andX($q->expr()->like('s.type', ':type'), $q->expr()->eq('case', ':case')))
  ->setParameter('type', "%$type")
  ->setParameter('case', $id);

:条款得到更多复杂,表达式的嵌套也会变得复杂。

Close, but no cigar. You'll need to learn how to use the Expr classes that comes with Doctrine in order to create the correct WHERE clause.

http://www.doctrine-project.org/docs/orm/2.1/en/reference/query-builder.html#expr-classes

In the example you provided, you are actually aiming for something like this (not tested):

$q->add('select', 's')
  ->add('from', 'app\model\Sub s')
  ->add('leftJoin', 's.case c')
  ->add('where', $q->expr()->andX($q->expr()->like('s.type', ':type'), $q->expr()->eq('case', ':case')))
  ->setParameter('type', "%$type")
  ->setParameter('case', $id);

As your WHERE clause gets more complicated, so will the nesting of expressions.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文