【发布时间】:2017-12-09 22:51:47
【问题描述】:
我正在使用 symfony 3.4.1,以及学说/orm 2.5.13。我有 2 张桌子。 store 和 store_product。
在Store 实体中我有:
/**
* @ORM\OneToMany(targetEntity="AppBundle\Entity\Store\Product", mappedBy="store")
* @ORM\JoinColumn(name="store_id")
*/
private $products;
在Store\Product 实体中,我有 (store_id,product_id) 的复合索引。我有:
/**
* @ORM\ManyToOne(targetEntity="AppBundle\Entity\Store", inversedBy="products")
* @ORM\JoinColumn(name="store_id",referencedColumnName="id",nullable=false,onDelete="CASCADE")
* @ORM\Id()
* @ORM\GeneratedValue("NONE")
* @var $store \AppBundle\Entity\Store
*/
protected $store;
/**
* @ORM\ManyToOne(targetEntity="AppBundle\Entity\Product", inversedBy="stores")
* @ORM\JoinColumn(name="product_id",referencedColumnName="id",nullable=false,onDelete="CASCADE")
* @ORM\Id()
* @ORM\GeneratedValue("NONE")
* @var $product \AppBundle\Entity\Product
*/
protected $product;
之前我使用guide 生成了一个本机查询。它只从store 表中获取条目,并且运行良好。
现在我正在尝试加入store_product 表,但进展并不顺利。我正在使用以下查询,它返回 1 个结果。
SELECT st.id, st.name, stp.store_id, stp.product_id, stp.price FROM store st LEFT JOIN store_product stp ON st.id = stp.store_id WHERE st.id=1 LIMIT 1;
返回类似:
id | name | store_id | product_id | price
----+------------+----------+------------+-------
1 | Store Name | 1 | 1234567890 | 129
我设置结果集映射如下:
$rsm = new ResultSetMapping();
$rsm->addEntityResult('AppBundle\Entity\Store', 'st');
$rsm->addFieldResult('st', 'id', 'id');
$rsm->addFieldResult('st', 'name', 'name');
$rsm->addJoinedEntityResult('AppBundle\Entity\Store\Product', 'stp',
'st', 'products');
$rsm->addFieldResult('stp','store_id', 'store');
$rsm->addFieldResult('stp','product_id','product');
$rsm->addFieldResult('stp','price','price');
我收到错误:Notice: Undefined index: store 任何人都可以看到错误的原因吗?
【问题讨论】:
标签: php symfony join doctrine-orm doctrine