欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  后端开发

Symfony2 oneToMany 问题!

程序员文章站 2024-01-08 10:14:22
...
现在有两张表,category 和 article。

category:

/**
 * @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;

    // ...
}

article:

/**
 * @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;

    // ...
}

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

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

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

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

回复内容:

现在有两张表,category 和 article。

category:

/**
 * @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;

    // ...
}

article:

/**
 * @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;

    // ...
}

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

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

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

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

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

$result = new ArrayCollection();

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

return $result;

}

从articles的角度解决问题。

在controller里面

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

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