主义实体找到许多到许多 [英] Doctrine Entity findBy Many to Many
问题描述
我想要做的是通过颜色查找产品。
eg)
$ colors = $ em-> getRepository('Xxxxx\XxxxxBundle\\ \\ Entity\Colour') - > findBy(array('name'=>'red');
$ products = $ em-> getRepository('Xxxxx\XxxxxBundle\Entity\Product ') - > findBy(array('colors'=> $ colors));
是我的Yaml配置:
Xxxxx\XxxxxBundle\Entity\Product:
type:entity
manyToMany:
颜色:
targetEntity:颜色
joinTable:
名称:Product_Colour
joinColumns:
product_id:
referencedColumnName:id
inverseJoinColumns:
colour_id:
referencedColumnName:id
。 >
Xxxxx\XxxxxBundle\Entity\Colour:
类型:实体
id:
id:
类型:整数
生成器:
策略:AUTO
字段:
十六进制:
类型:字符串
长度:320
名称:
类型:字符串
长度:320
我得到的错误信息是:
注意:未定义的索引:joinColumns在/home/xxx/public_html/products/vendor/doctrine/lib/Doctrine/ORM/Persisters/BasicEntityPersister.php行1217
有人能够清楚了解为什么这不起作用。
我知道这是一个古老的问题,但是如果有人通过Google到达这里(像我这样做),我不得不避开findBy并在存储库中使用DQL:
$ products = $ em-> getRepository('Vendor\Bundle\Entity\Product') - > findByColours($色);
在存储库中:
public function findByColours($ colors)
{
$ qb = $ this-> getEntityManager() - > createQueryBuilder();
$ qb - > select(array('p'))
- > from('VendorBundle:Product','p')
- > join('p.colours ','c','WITH',$ qb-> expr() - > in('c.id',$ colors));
$ result = $ qb-> getQuery() - > execute();
return $ result;
}
您可能需要根据什么$颜色更改连接是。这是假设它是一个颜色ID数组。如果它是一个字符串,您可以放弃()中的,或者如果它是一个字符串数组,则需要将字符串绑定为参数(请参阅以下链接)。澄清expr()等等,在 Doctrine docs
我不知道为什么未定义的索引:joinColumns
但这是一种完全相反的方法。希望有人可以澄清错误,因为我的解决方案为多对多关系增添了额外的工作。
I have a many to many relationship between products and colours.
What I am trying to do is find products by their colours.
eg)
$colours = $em->getRepository('Xxxxx\XxxxxBundle\Entity\Colour')->findBy(array('name'=>'red');
$products = $em->getRepository('Xxxxx\XxxxxBundle\Entity\Product')->findBy(array('colours'=>$colours));
This is my Yaml config:
Xxxxx\XxxxxBundle\Entity\Product:
type: entity
manyToMany:
colours:
targetEntity: Colour
joinTable:
name: Product_Colour
joinColumns:
product_id:
referencedColumnName: id
inverseJoinColumns:
colour_id:
referencedColumnName: id
.
Xxxxx\XxxxxBundle\Entity\Colour:
type: entity
id:
id:
type: integer
generator:
strategy: AUTO
fields:
hex:
type: string
length: 320
name:
type: string
length: 320
The error message I am getting is:
Notice: Undefined index: joinColumns in /home/xxx/public_html/products/vendor/doctrine/lib/Doctrine/ORM/Persisters/BasicEntityPersister.php line 1217
Would someone be able to shine some light on why this is not working.
I know this is an old question, but if anyone else arrives here via Google (like I did), I had to eschew the findBy and use DQL in the repository:
$products = $em->getRepository('Vendor\Bundle\Entity\Product')->findByColours($colours);
And in the repository:
public function findByColours($colours)
{
$qb = $this->getEntityManager()->createQueryBuilder();
$qb ->select(array('p'))
->from('VendorBundle:Product', 'p')
->join('p.colours', 'c', 'WITH', $qb->expr()->in('c.id', $colours));
$result = $qb->getQuery()->execute();
return $result;
}
You may need to change the join based on what $colours is. This is assuming it's an array of colour IDs. If it's a string you can forgo the in()
or if it's an array of strings you'll need to bind the strings as parameters (see the following link). Clarification on expr() and such is in the Doctrine docs
I don't know why Undefined index: joinColumns
occurs, but this is a method to side-step it altogether. Hopefully someone can clarify as to the error, as my solution adds extra work to the Many to Many relationship.
这篇关于主义实体找到许多到许多的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!