1

我有一个产品实体,它可以有多个属性,如应用程序。其中“应用程序”也可能具有多个值。

所以该产品有一个“应用程序”的值数组,我怎样才能用 Doctrine 保存它,以便以后可以执行 findBy('applications' => 'whatever') 或类似操作?

Product 1
   application1
   application3

Product 2
   application1
   application2

所以后来的 findBy('applications' => 'application1') 应该找到这两个产品。

4

2 回答 2

3

假设您在实体之间配置了一对多关系,如下所示:

产品实体:

/**
* @ORM\OneToMany(targetEntity="Aplication", mappedBy="product", cascade={"persist"})
*/
protected $aplications;

申请实体:

* @ORM\ManyToOne(targetEntity="Product", inversedBy="aplications")
* @ORM\JoinColumns({
*   @ORM\JoinColumn(name="product_id", referencedColumnName="id")
* })
    protected $product;

您可以在产品存储库中创建这样的 DQL:

$qb = $this->getEntityManager()->createQueryBuilder();
$qb->select('p')
   ->from('bundleNamespace:Product', 'p')
   ->leftJoin('p.applications', 'a')
   ->andWhere($qb->expr()->in('a.id', ':aplication'))
                    ->setParameter('aplication', $aplication);

return $qb->getQuery()->getResult();
于 2013-04-13T11:50:14.587 回答
0

尝试这个:

public function findProductByApplication($app) {
    $query = $this->em->createQuery('SELECT DISTINCT p FROM Product p JOIN p.applications app WHERE app = :app');
    $query->setParameter('app', $app);
    return $query->getResult();
}
于 2013-04-11T13:42:53.803 回答