2013-04-11 47 views
1

我有一个Product Entity,它可以具有多个属性,如应用程序。 “应用程序”也可能具有多重值。在Doctrine中的多个值中搜索

因此,该产品有一个“应用程序”值的数组,我怎样才能保存与学说,所以我可以做一个findBy('应用程序'=>'不管')或类似的以后?

Product 1 
    application1 
    application3 

Product 2 
    application1 
    application2 

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

+0

你需要使用DQL。 – mpm 2013-04-11 11:10:11

+0

而你必须创建一个新的实体调用应用程序。然后以OneToMany关系加入产品和应用程序。 在此之后,您可以使用Doctrine的DQL进行自定义请求 – Pierrickouw 2013-04-11 13:21:35

回答

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(); 
} 
3

假设你已经配置了实体之间的一对多的关系,就像这样:

产品实体:

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

Aplication实体:

* @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();