2016-08-09 16 views
0

現在私は以下の3つのテーブルを持っています。カテゴリーエンティティでDoctrine many to many [Semantical Error]

articles_categories

+-------------+---------+------+-----+---------+-------+ 
| Field  | Type | Null | Key | Default | Extra | 
+-------------+---------+------+-----+---------+-------+ 
| article_id | int(11) | NO | PRI | NULL |  | 
| category_id | int(11) | NO | PRI | NULL |  | 
+-------------+---------+------+-----+---------+-------+ 

カテゴリ

+-----------------+--------------+------+-----+---------+----------------+ 
| Field   | Type   | Null | Key | Default | Extra   | 
+-----------------+--------------+------+-----+---------+----------------+ 
| id    | int(11)  | NO | PRI | NULL | auto_increment | 
| title   | varchar(255) | NO |  | NULL |    | 
+-----------------+--------------+------+-----+---------+----------------+ 

記事

+-----------------+--------------+------+-----+---------+----------------+ 
| Field   | Type   | Null | Key | Default | Extra   | 
+-----------------+--------------+------+-----+---------+----------------+ 
| id    | int(11)  | NO | PRI | NULL | auto_increment | 
| title   | varchar(255) | NO |  | NULL |    | 
+-----------------+--------------+------+-----+---------+----------------+ 

/** 
    * @ORM\ManyToMany(targetEntity="Article", mappedBy="categories") 
    * @ORM\OrderBy({"createdAt" = "DESC"}) 
    */ 
    protected $articles; 

記事の内容

/** 
    * All categories this article belongs to. 
    * 
    * @ORM\ManyToMany(targetEntity="Category", inversedBy="articles", cascade={"persist"}) 
    * @ORM\JoinTable(name="articles_categories") 
    */ 
    protected $categories; 

ほとんどのクエリが正常に動作します。しかし、私はカテゴリに属する​​記事を入手したい。またはそれは特定のカテゴリを持っているか、カテゴリに基づいて記事をフィルタリングしたいですか?

私は次のクエリを書いています。

$em = $this->getEntityManager(); 
     $qb = $em->createQueryBuilder('c'); 
     $qb->select('1') 
      ->from('articles_categories', 'a_c') 
      ->leftJoin('\\Chip\\Entity\\Article', 'a', 'WITH', 'a.id = a_c.article_id') 
      ->leftJoin('\\Chip\\Entity\\Category', 'c', 'WITH', 'c.id = a_c.category_id') 
     ; 

     $result = $qb->getQuery()->getResult(); 

ただし、次のエラーが発生します。

[Semantical Error] line 0, col 14 near 'articles_categories': Error: Class 'articles_categories' is not defined. 
500 Internal Server Error - QueryException 
1 linked Exception: QueryException » 

質問を書くためのヒントやその他の方法があれば、それは素晴らしいものです。

ありがとうございました。

+0

'articles_categories'はDoctrineマップクラスではなく、ORMクラスではありません。必要に応じて、エンティティ間の関係テーブルを指定する必要はなく、それらの間の既存の関係のみを指定する必要があります。 – Matteo

+0

いくつかの例でこの答えをチェックhttp://stackoverflow.com/a/19710312/2270041 – Matteo

+1

@Matteoありがとう、ありがとう。あなたは問題を解決しました。 – bharatesh

答えて

1

クエリビルダではテーブル名を使用しないでください。代わりにクラス名を使用する必要があります。

$em = $this->getEntityManager(); 
$qb = $em->createQueryBuilder('c'); 
$qb->select('c', 'a') 
    ->from('Chip\Entity\Category', 'c') // this line is not necessary when performing this query in your category repository 
    ->leftJoin('c.articles, a') 
    ->where('c.id = 1'); 
$result = $qb->getQuery()->getResult(); 
+0

はい。ヒントありがとうございます。私はソリューションがリンク内にあると思うstackoverflow.com/a/19710312/2270041 – bharatesh