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

Symfony2 oneToMany 问题!

WBOY
Release: 2016-06-06 20:19:18
Original
1275 people have browsed it

现在有两张表,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>
Copy after login
Copy after login

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>
Copy after login
Copy after login

我的问题是, $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>
Copy after login
Copy after login

回复内容:

现在有两张表,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>
Copy after login
Copy after login

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>
Copy after login
Copy after login

我的问题是, $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>
Copy after login
Copy after login

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

<code>$result = new ArrayCollection();

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

return $result;</code>
Copy after login

}

从articles的角度解决问题。

在controller里面

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

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

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template