2016-05-17 2 views
0

Doctrine DQLでは、逆マッピングを使用しているエンティティなしで多対多の関連付けからカウントを取得しようとしています。 。逆マッピングなしでDoctrineの逆オブジェクトから多くのオブジェクトへの関連の数を取得する

$this->getEntityManager()->createQueryBuilder() 
    ->select('a AS attribute') 
    ->addSelect('COUNT(p) AS num_products') 
    ->from('VendorAttributeBundle:Attribute', 'a') 
    ->leftJoin('VendorProductBundle:Product', 'p', 'WITH', 'a MEMBER OF p.attributes') 
    ->groupBy('a.id'); 

以上実行されますがnum_productsフィールドの0返します。

+1

私はbiderectional多対多エンティティに対してクエリを実行し、うまく機能します。マッピングでエラーを見つけようとします。私の例を見てください。https://gist.github.com/Infernosquad/cc6237e7c782892e70ba33dd36976234 –

答えて

0

私はちょうど先に進み、逆マッピングを作成しました。私はそれなしで仕事を得ることができませんでした。

参考

Attributeエンティティにおいて:

/** 
* @var ArrayCollection 
* 
* @ORM\ManyToMany(targetEntity="Vendor\ProductBundle\Entity\Product", mappedBy="attributes") 
*/ 
private $products; 

Productエンティティにおいて:

/** 
* @var ArrayCollection 
* 
* @ORM\ManyToMany(targetEntity="Vendor\AttributeBundle\Entity\Attribute", inversedBy="products") 
*/ 
private $attributes; 

そして働い以下:

$this->getEntityManager()->createQueryBuilder() 
    ->select('a AS attribute') 
    ->addSelect('COUNT(p) AS num_products') 
    ->from('VendorAttributeBundle:Attribute', 'a') 
    ->leftJoin('VendorProductBundle:Product', 'p') 
    ->groupBy('a.id'); 
関連する問題