具有似乎使用混合参数查询的学说标准,除非首先急切地加载关联。
设置非常简单。我有四个实体:User,Business,UserBusiness和UserBusinessMeta。
每个用户都可以属于一个或多个企业。每个UserBusiness还可以具有给定企业的Meta键/值列表。所以,
用户:
商务:
UserBusiness:
UserBusinessMeta(在db中对user_id,business_id,key也有唯一约束):
在我的UserBusiness实体中,我有一个名为getFoo的方法,其标准如下所示:
$criteria = Criteria::create()
->where(Criteria::expr()->eq('key', 'foo'))
->setMaxResults(1);
return $this->meta->matching($criteria)->first();
出于某种原因,当为此条件创建SQL时,它会在设置params时反转business_id和user_id值,以便它正在查找business_id为3且user_id为2!
SELECT
t0.user_id AS user_id_1,
t0.business_id AS business_id_2,
t0.`key` AS key_3,
t0.`value` AS value_4,
t0.id AS id_5,
t0.user_id AS user_id_6,
t0.business_id AS business_id_7
FROM user_business_meta t0
WHERE (t0.`key` = ? AND t0.user_id = ? AND t0.business_id = ?) LIMIT 1
array (size=3)
0 => string 'foo' (length=11)
1 => int 2
2 => int 3
array (size=3)
0 => string 'string' (length=6)
1 => string 'integer' (length=7)
2 => string 'integer' (length=7)
但是,如果我将UserBusinessMeta关联提取设置为EAGER,则将变量正确加载到内存中,上述条件 - 不必使用SQL - 返回正确的结果。
我使用XML映射,其中没有什么特别之处。列和字段的名称以及关联是正确的:
商业 - >一对多UserBusiness UserBusiness - >一对多UserBusinessMeta
此处关联的映射是:
<one-to-many target-entity="UserBusinessMeta" mapped-by="UserBusiness" field="meta" orphan-removal="true">
<cascade>
<cascade-persist/>
<cascade-remove/>
</cascade>
</one-to-many>
并映射了user_id和business_id列:
<id name="businessId" column="business_id" type="integer" />
<id name="userId" column="user_id" type="integer" />
在UserBusinessMeta中,返回UserBusiness的多对一关联定义如下:
<many-to-one target-entity="UserBusiness" field="UserBusiness" inversed-by="meta">
<join-columns>
<join-column name="user_id" referenced-column-name="user_id" />
<join-column name="business_id" referenced-column-name="business_id" />
</join-columns>
</many-to-one>
最后,UserBusinessMeta实体还有两列business_id和user_id映射为:
<field name="userId" column="user_id" type="integer" />
<field name="businessId" type="integer" column="business_id" />
答案 0 :(得分:0)
事实证明,问题是由于UserBusiness实体中有两个ID,其中一个ID需要标记为关键密钥。以下更新修复了它:
<id name="business" column="business_id" type="integer" association-key="true" />