How to JOIN without a relationship in Doctrine?(如何在没有教义关系的情况下加入?)
问题描述
我有一个实体 Log
(表 log
),其中包含成员 resourceType
和 resourceId
(带有列 resource_log
和 resource_id
).resourceType
可以例如Order
(用于订单操作,例如状态更改)或 Whatever
(用于 Whatever
相关操作).Log
和资源"实体/表之间没有任何关系(Doctrine 和数据库中都没有).
I have an entity Log
(table log
) with members resourceType
and resourceId
(with columns resource_log
and resource_id
). The resourceType
can e.g. Order
(for actions on orders, e.g. status changes ) or Whatever
(for Whatever
related actions). There is no relationships (neither in Doctrine, nor in the database) between the Log
and the "resource" entities/tables.
现在我只想选择 Log
,它们与 myOrderProperty = "someValue"
的 Order
相关.这意味着:
Now I want to select only the Log
s, that are related to Order
s with myOrderProperty = "someValue"
. That means:
SELECT
*
FROM
`log`
JOIN
`order` ON `order`.`id` = `log`.`resource_id` AND `log`.`resource_type` = 'order'
WHERE
`order`.`my_order_property` LIKE '%my_order_property_value%'
但是代码
$queryBuilder = $this->entityManager->createQueryBuilder();
$queryBuilder->select('l')->from(Log::class, 'l');
$queryBuilder->join('l.order', 'o');
...
$queryBuilder
->where('o.myOrderProperty = :myOrderProperty')
->setParameter('myOrderProperty', $myOrderProperty);
不起作用,因为实体 Log
与 Order
没有任何关系(尽管它有一个属性 order
):
doesn't work, since the entity Log
doesn't have any relationship with the Order
(though it has a property order
):
[Semantical Error] line 0, col 102 near 'o WHERE o.myOrderProperty': Error: Class MyNamespaceLog has no association named order
如何在两个实体之间没有定义关系的情况下JOIN
?
我知道,我可以使用 继承.但从语义上讲,这不是继承案例.那么有没有其他方法可以解决这个问题呢?
I know, I could use inheritance. But semantically it's not an inheritance case. So is there another way for solving the problem?
推荐答案
没有关系的JOIN
可以这样实现:
The JOIN
without relationship can be implemented like this:
$queryBuilder = $this->entityManager->createQueryBuilder();
$queryBuilder->select('l')->from(Log::class, 'l');
$queryBuilder->join(
Order::class,
'o',
DoctrineORMQueryExprJoin::WITH,
'o.id = l.resourceId'
);
...
$queryBuilder
->where('o.myOrderProperty = :myOrderProperty')
->setParameter('myOrderProperty', $myOrderProperty);
唯一的问题是,连接的实体没有添加到主实体,所以 $myLog->getOrder(...)
返回 null
.
The only problem is, that the joined entities are not added to the main entity, so that $myLog->getOrder(...)
returns null
.
这篇关于如何在没有教义关系的情况下加入?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何在没有教义关系的情况下加入?
基础教程推荐
- XAMPP 服务器不加载 CSS 文件 2022-01-01
- 在 PHP 中强制下载文件 - 在 Joomla 框架内 2022-01-01
- 超薄框架REST服务两次获得输出 2022-01-01
- mysqli_insert_id 是否有可能在高流量应用程序中返回 2021-01-01
- Libpuzzle 索引数百万张图片? 2022-01-01
- 在 Woocommerce 中根据运输方式和付款方式添加费用 2021-01-01
- WooCommerce 中选定产品类别的自定义产品价格后缀 2021-01-01
- 如何在 PHP 中的请求之间持久化对象 2022-01-01
- 通过 PHP SoapClient 请求发送原始 XML 2021-01-01
- 在多维数组中查找最大值 2021-01-01