Symfony2 QueryBuilder加入ON和WITH的区别 [英] Symfony2 QueryBuilder join ON and WITH difference

查看:155
本文介绍了Symfony2 QueryBuilder加入ON和WITH的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Symfony2的新成员,我通过QueryBuilder和Doctrine 2成功完成了我的第一次加入。
可能这是一个愚蠢的问题,但是在线和通过Symfony2的方法我没有找到任何理解连接条款WITH和ON之间的区别。



例如,这是我的加入代码:

   - > leftJoin('EcommerceProductBundle:ProductData','pdata','WITH','prod.id = IDENTITY(pdata.product)')

它的效果很好,但如果我把 ON 而不是 WITH 我收到以下错误:


[语法错误]行0,col 200:错误:预期
Doctrine\ORM\Query\Lexer :: T_WITH,得到'ON'


为什么?我在对象中看到了T_ON和T_WITH都像连接子句一样,但是它们的使用差异呢?他们的用途如何?



非常感谢你。

解决方案

@florian给了你正确的答案,但让我尝试解释一下例子:



在sql中,连接完成如下:

  SELECT * FROM category 
LEFT JOIN product ON product.category_id = category.id

(或类似的东西)



现在在Doctrine中,您不需要使用 ON 子句,因为doctrine知道你的实体中的关系注释。所以上面的例子是:

  // CategoryRepository.php 
public function getCategoriesAndJoinProducts()
{
return $ this-> createQueryBuilder(o)
- > leftJoin(o.products,p) - > addSelect(p)
- > getQuery() - > getResult();
}

两者都将获取所有类别并加入与其相关联的产品。



现在来到 WITH 子句。如果您只想加入价格大于50的产品,您可以在SQL中执行此操作:

  SELECT * FROM category 
LEFT JOIN product ON product.category_id = category.id AND product.price> 50

在原则:

  // CategoryRepository.php 
public function getCategoriesAndJoinProductsWithPriceBiggerThan($ price)
{
return $ this-> createQueryBuilder(o)
- > leftJoin(o.products,p,WITH,p.price>:price)
- > ; setParameter(price,price) - > addSelect(p)
- > getQuery() - > getResult();
}

所以,在现实中你永远不应该使用 ON 如果您使用的是Doctrine。如果你需要这样的东西,那么你几乎可以确定你搞砸了别的东西。


I'm new with Symfony2 and i built successfully my first join through QueryBuilder and Doctrine 2. Probably this is a stupid question but both on-line and through the Symfony2's methods i didn't find nothing for understand the difference between the join clauses "WITH" and "ON".

For example this is my join code:

->leftJoin('EcommerceProductBundle:ProductData', 'pdata', 'WITH', 'prod.id = IDENTITY(pdata.product)')

It works good but if i put ON instead of WITH i get the following error:

[Syntax Error] line 0, col 200: Error: Expected Doctrine\ORM\Query\Lexer::T_WITH, got 'ON'

Why? I've seen among the objects that there are both the T_ON and T_WITH like join clauses, but which is their usage difference? What is their uses like?

Thank you very much.

解决方案

@florian gave you the correct answer but let me try to explain it on example:

In sql, joins are done like this:

SELECT * FROM category
    LEFT JOIN product ON product.category_id = category.id

(or something like this)

Now in Doctrine, you don't need to use ON clause because doctrine knows that from relations annotations in your entities. So above example would be:

// CategoryRepository.php
public function getCategoriesAndJoinProducts() 
{
    return $this->createQueryBuilder("o")
        ->leftJoin("o.products", "p")->addSelect("p") 
        ->getQuery()->getResult() ;
}

Both would fetch all categories and join products associated with them.

Now comes the WITH clause. If you want to join only products with price bigger than 50, you would do this in SQL:

SELECT * FROM category
    LEFT JOIN product ON product.category_id = category.id AND product.price>50

In Doctrine:

// CategoryRepository.php
public function getCategoriesAndJoinProductsWithPriceBiggerThan($price) 
{
    return $this->createQueryBuilder("o")
        ->leftJoin("o.products", "p", "WITH", "p.price>:price")
            ->setParameter("price", price)->addSelect("p") 
        ->getQuery()->getResult() ;
}

So, in reality you should never, ever use ON if you are using Doctrine. If you have a need for something like that, you can be almost sure that you screwed something else.

这篇关于Symfony2 QueryBuilder加入ON和WITH的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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