Home >Backend Development >PHP Tutorial >Symfony2 oneToMany 问题!

Symfony2 oneToMany 问题!

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOriginal
2016-06-06 20:19:181306browse

现在有两张表,category 和 article。

category:

<code class="php">/**
 * @Mapping\Entity
 * @Mapping\Table(name="category")
 */
class Category
{
    /**
     * @Mapping\Id
     * @Mapping\Column(type="integer")
     * @Mapping\GeneratedValue(strategy="AUTO")
     */
    protected $id;

    /**
     * @Mapping\OneToMany(targetEntity="Article", mappedBy="category")
     */
    protected $articles;

    // ...
}</code>

article:

<code>/**
 * @Mapping\Entity
 * @Mapping\Table(name="article")
 */
class Article
{
    /**
     * @Mapping\Id
     * @Mapping\Column(type="integer")
     * @Mapping\GeneratedValue(strategy="AUTO")
     */
    protected $id;

    /**
     * @Mapping\ManyToOne(targetEntity="Category", inversedBy="articles")
     * @Mapping\JoinColumn(name="categoryid", referencedColumnName="id")
     */
    protected $category;
    
    /**
     * @Mapping\Column(type="string", columnDefinition="enum('published', 'draft')")
     */
    protected $status;

    // ...
}</code>

我的问题是, $category->getArticles() 方法可以获取指定分类下的 articles,但是,如果我只想获取 status="published" 时怎么办呢?我看过文档,oneToMany 中不能设定条件

同时,在 twig 中使用 category.articles|length 来显示指定分类下的文章数,也只能返回全部的文章,这种场景怎么写呢?。

另外,我现在在 CategoryRepository 中使用 JOIN 加上条件查询出来,但在 twig 中显示出来的结果就还是忽略了 status 条件:

<code class="php">return $this->createQueryBuilder('c')
     ->leftJoin('c.articles', 'a', 'WITH', 'a.status = :status')
     ->groupBy('c.id')
     ->setParameter('status', 'published')
     ->getQuery()
     ->getResult();</code>

回复内容:

现在有两张表,category 和 article。

category:

<code class="php">/**
 * @Mapping\Entity
 * @Mapping\Table(name="category")
 */
class Category
{
    /**
     * @Mapping\Id
     * @Mapping\Column(type="integer")
     * @Mapping\GeneratedValue(strategy="AUTO")
     */
    protected $id;

    /**
     * @Mapping\OneToMany(targetEntity="Article", mappedBy="category")
     */
    protected $articles;

    // ...
}</code>

article:

<code>/**
 * @Mapping\Entity
 * @Mapping\Table(name="article")
 */
class Article
{
    /**
     * @Mapping\Id
     * @Mapping\Column(type="integer")
     * @Mapping\GeneratedValue(strategy="AUTO")
     */
    protected $id;

    /**
     * @Mapping\ManyToOne(targetEntity="Category", inversedBy="articles")
     * @Mapping\JoinColumn(name="categoryid", referencedColumnName="id")
     */
    protected $category;
    
    /**
     * @Mapping\Column(type="string", columnDefinition="enum('published', 'draft')")
     */
    protected $status;

    // ...
}</code>

我的问题是, $category->getArticles() 方法可以获取指定分类下的 articles,但是,如果我只想获取 status="published" 时怎么办呢?我看过文档,oneToMany 中不能设定条件

同时,在 twig 中使用 category.articles|length 来显示指定分类下的文章数,也只能返回全部的文章,这种场景怎么写呢?。

另外,我现在在 CategoryRepository 中使用 JOIN 加上条件查询出来,但在 twig 中显示出来的结果就还是忽略了 status 条件:

<code class="php">return $this->createQueryBuilder('c')
     ->leftJoin('c.articles', 'a', 'WITH', 'a.status = :status')
     ->groupBy('c.id')
     ->setParameter('status', 'published')
     ->getQuery()
     ->getResult();</code>

class Category
{
...
public function getPublishedArticles()
{

<code>$result = new ArrayCollection();

foreach ($this->articles as $article) {
    if ($article->getStatus() == 'published') {
        $result[] = $article;
    }
}

return $result;</code>

}

从articles的角度解决问题。

在controller里面

<code>$articles = $this
    ->getDoctrine()
    ->getRepository('Article')
    ->findBy([
        'category' => $category,
        'status'   => 'published',    
    ]);</code>

然后view里面就可以用articles|length了。

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn