Symfony2 oneToMany 问题!
Jun 06, 2016 pm 08:19 PM
现在有两张表,category 和 article。
category:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
|
article:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
|
我的问题是, $category->getArticles() 方法可以获取指定分类下的 articles,但是,如果我只想获取 status="published" 时怎么办呢?我看过文档,oneToMany 中不能设定条件
同时,在 twig 中使用 category.articles|length 来显示指定分类下的文章数,也只能返回全部的文章,这种场景怎么写呢?。
另外,我现在在 CategoryRepository 中使用 JOIN 加上条件查询出来,但在 twig 中显示出来的结果就还是忽略了 status 条件:
1 2 3 4 5 6 |
|
回复内容:
现在有两张表,category 和 article。
category:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
|
article:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
|
我的问题是, $category->getArticles() 方法可以获取指定分类下的 articles,但是,如果我只想获取 status="published" 时怎么办呢?我看过文档,oneToMany 中不能设定条件
同时,在 twig 中使用 category.articles|length 来显示指定分类下的文章数,也只能返回全部的文章,这种场景怎么写呢?。
另外,我现在在 CategoryRepository 中使用 JOIN 加上条件查询出来,但在 twig 中显示出来的结果就还是忽略了 status 条件:
1 2 3 4 5 6 |
|
class Category
{
...
public function getPublishedArticles()
{
1 2 3 4 5 6 7 8 9 |
|
}
从articles的角度解决问题。
在controller里面
1 2 3 4 5 6 7 |
|
然后view里面就可以用articles|length了。

Heißer Artikel

Hot-Tools-Tags

Heißer Artikel

Heiße Artikel -Tags

Notepad++7.3.1
Einfach zu bedienender und kostenloser Code-Editor

SublimeText3 chinesische Version
Chinesische Version, sehr einfach zu bedienen

Senden Sie Studio 13.0.1
Leistungsstarke integrierte PHP-Entwicklungsumgebung

Dreamweaver CS6
Visuelle Webentwicklungstools

SublimeText3 Mac-Version
Codebearbeitungssoftware auf Gottesniveau (SublimeText3)

Heiße Themen

PHP 8.4 Installations- und Upgrade-Anleitung für Ubuntu und Debian

So richten Sie Visual Studio-Code (VS-Code) für die PHP-Entwicklung ein
