简体   繁体   English

如何使用querybuilder排除不具有多对多关系的实体,以Symfony2形式创建字段?

[英]How can I make a field in a Symfony2 form, using querybuilder, excluding entities without a many-to-many relation?

In my Symfony2 app, people can write texts that will be published on a webpage. 在我的Symfony2应用程序中,人们可以编写将发布在网页上的文本。 There is an option to write a new text, and you have to select the webpage it will belong to. 有一个选项可以写一个新文本,您必须选择它将属于的网页。

What I want to achieve, is that this list of webpages shows webpages that do not yet have a text only. 我要实现的是,此网页列表显示的还不是纯文本网页。 Otherwise you will end up replacing an existing text. 否则,您最终将替换现有文本。

So I would like to write something like this in my form type : 所以我想在我的表单类型中写这样的东西:

$qb->select('wp')
->from('MyBundle:Webpage', 'wp')
->where('wp.webtexts is null')
->orderBy('wp.id');

The problem arises around the "wp.webtexts is null" statement. 问题出现在“ wp.webtexts为空”语句周围。 This is a (fully functioning) many-to-many relationship, and I would like to test whether there are no relations here. 这是一个(运作良好的)多对多关系,我想测试一下这里是否没有关系。 The error I receive, is: 我收到的错误是:

[Semantical Error] line 0, col 70 near 'webtexts is null': Error: Invalid PathExpression. [语义错误]第0行,“ webtexts为null”附近的第70列:错误:无效的PathExpression。 StateFieldPathExpression or SingleValuedAssociationField expected. 预期为StateFieldPathExpression或SingleValuedAssociationField。

How can I query for webpages, with no relations to any webtexts? 如何查询与任何文本无关的网页?

Added: 添加:

How could I count the amount of relations? 我如何计算关系数量? This notation: 该符号:

 $qb->where($qb->expr()->count('wp.webtexts < 1'))

...gives me: ...给我:

[Syntax Error] line 0, col 85: Error: Expected Doctrine\\ORM\\Query\\Lexer::T_CLOSE_PARENTHESIS, got '<' [语法错误]行0,列85:错误:预期的Doctrine \\ ORM \\ Query \\ Lexer :: T_CLOSE_PARENTHESIS,得到了“ <”

If you are trying to check against the webtexts object you should query the webtexts id field rather than the webtexts object. 如果您要尝试检查webtexts对象,则应查询webtexts id字段而不是webtexts对象。 Doctrine is still trying to write SQL at the end of the day and in SQL you would have to query a field: 在今天结束时,Doctrine仍在尝试编写SQL,而在SQL中,您必须查询一个字段:

$qb->select('wp')
    ->from('MyBundle:Webpage', 'wp')
    ->leftJoin('wp.webtexts', 'wt')
    ->where('wt.id IS NULL')
    ->orderBy('wp.id');

Like this: 像这样:

$em->createQuery('SELECT wp FROM MyBundle:Webpage wp LEFT JOIN wp.webtexts wt WHERE wt.id IS NULL ORDER BY wp.id');

It takes Webpages that are joined or not with texts and then eliminates thoses which have a relation with text... So you've got webpages without relation ! 它需要将网页与文本连接在一起或不将其与文本连接,然后消除那些与文本有关系的页面...因此,您的网页没有关系!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM