[英]Doctrine addJoinedEntityResult gives error with oneToMany/manyToOne
I am using symfony 3.4.1, with doctrine/orm 2.5.13.我正在使用 symfony 3.4.1,以及学说/orm 2.5.13。 I have 2 tables.
我有2张桌子。
store
and store_product
. store
和store_product
。
In Store
entity I have:在
Store
实体中,我有:
/**
* @ORM\OneToMany(targetEntity="AppBundle\Entity\Store\Product", mappedBy="store")
* @ORM\JoinColumn(name="store_id")
*/
private $products;
and in Store\\Product
entity I have composite index with (store_id,product_id).在
Store\\Product
实体中,我有 (store_id,product_id) 的复合索引。 I have:我有:
/**
* @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;
Earlier I produced a native query using the guide .早些时候,我使用指南生成了一个本机查询。 It was only fetching entries from
store
table and it worked perfectly.它只是从
store
表中获取条目,并且运行良好。
Now I am trying to join store_product
table and it is not going so well.现在我正在尝试加入
store_product
表,但进展并不顺利。 I am using the following query which returns 1 result.我正在使用以下查询返回 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;
returns something like:返回类似:
id | name | store_id | product_id | price
----+------------+----------+------------+-------
1 | Store Name | 1 | 1234567890 | 129
I setup the result set mapping as follows:我设置结果集映射如下:
$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');
I am getting error: Notice: Undefined index: store
Can anybody see the reason of the error?我收到错误:
Notice: Undefined index: store
任何人都可以看到错误的原因吗?
I managed to get this working at last after 3 months. 3 个月后,我终于设法让这个工作了。
Apparently I made a mistake of trusting documentation when I added foreign keys with addFieldResult()
for joined table.显然,当我使用
addFieldResult()
为连接表添加外键时,我犯了信任文档的错误。
I needed to remove addFieldResult()
lines for store_id
and product_id` and add them like:我需要删除
addFieldResult()
为线store_id
和product_id`并将它们添加,如:
$rsm->addMetaResult('stp', 'store_id', 'store_id', true);
$rsm->addMetaResult('stp', 'product_id', 'product_id', true);
It wasn't enough to just add them with addMetaResult()
I had to set 3rd parameter true also.仅使用
addMetaResult()
添加它们是不够的,我还必须将第三个参数设置为 true。
Even though documentation says Consequently, associations that are fetch-joined do not require the foreign keys to be present in the SQL result set, only associations that are lazy.
即使文档说
Consequently, associations that are fetch-joined do not require the foreign keys to be present in the SQL result set, only associations that are lazy.
What worked for me was using addJoinedEntityFromClassMetadata
from the ResultSetMappingBuilder
instead of addJoinedEntityResult
.对我
addJoinedEntityFromClassMetadata
是使用ResultSetMappingBuilder
的addJoinedEntityResult
而不是addJoinedEntityResult
。 Using the example from the documentation :使用文档中的示例:
/**
* @param string $class The class name of the joined entity.
* @param string $alias The unique alias to use for the joined entity.
* @param string $parentAlias The alias of the entity result that is the parent of this joined result.
* @param string $relation The association field that connects the parent entity result
* with the joined entity result.
*/
$rsm->addJoinedEntityFromClassMetadata(Address::class, 'a', 'u', 'address');
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.