英文:
Shopware 6 Data Abstraction Layer: Compare two order fields/properties with each other
问题
以下是您要翻译的内容:
"代码的目标是获取具有两个不同字段/属性中相同值的所有订单实体,这种情况下是 createdAt
和 updatedAt
。
到目前为止,尚未找到这种用例的解决方案,唯一的结果是 https://forum.shopware.com/t/dal-range-filter-mit-spalte/72770,可惜没有解决。
我尝试了这段代码,但没有成功:
// 从一个类中摘录
use Shopware\Core\Framework\Context;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
$criteria = new Criteria();
$criteria->addFilter(
new EqualsFilter('updatedAt', 'createdAt')
);
// $this->orderRepository 在构造函数中设置
$result = $this->orderRepository->search($criteria, Context::createDefaultContext())->getEntities();
$result
应该包含在 EntityCollection
中的所有订单,但在这种情况下,该代码会引发异常,这是预期的,因为 'createdAt' 被视为值,而不是字段路径:
在 PDOStatement.php 第 117 行:
[PDOException (HY000)] SQLSTATE[HY000]: General error: 1525 Incorrect DATETIME value: 'createdAt'
如何在 DAL 中比较实体的字段/属性?"
英文:
The goal of the code is to get all order entities that have the same value in two different fields/properties, in this case createdAt
and updatedAt
.
So far, it was not possible to find a solution for this use case, the only result was https://forum.shopware.com/t/dal-range-filter-mit-spalte/72770, which was sadly not solved.
I have tried this code, but was not successful:
// excerpt from a class
use Shopware\Core\Framework\Context;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
$criteria = new Criteria();
$criteria->addFilter(
new EqualsFilter('updatedAt', 'createdAt')
);
// $this->orderRepository is set in the constructor
$result = $this->orderRepository->search($criteria, Context::createDefaultContext())->getEntities();
$result
should include all orders in an EntityCollection
but in this case the code throws an exception which is expected since 'createdAt' is treated as a value, not a field path:
> In PDOStatement.php line 117:
>
> [PDOException (HY000)] SQLSTATE[HY000]: General error: 1525 Incorrect DATETIME value: 'createdAt'
How is it possible to compare fields/properties for entities inside the DAL?
答案1
得分: 1
这是翻译好的部分:
"在此时,您不能仅使用Shopware自己的数据抽象层来完成这个操作。您可以注入Doctrine\DBAL\Connection
并使用纯SQL来预过滤id,然后再使用抽象层来获取完整的数据集。
$ids = $this->connection->fetchFirstColumn(
'SELECT LOWER(HEX(`id`)) FROM `order` WHERE `created_at` = `updated_at`'
);
$criteria = new Criteria($ids);
$orders = $this->orderRepository->search($criteria, $context)->getEntities();
```"
<details>
<summary>英文:</summary>
At this time, you can't do this with just Shopware's own data abstraction layer. You could inject `Doctrine\DBAL\Connection` and use plain SQL to pre-filter ids and then use the abstraction layer to fetch full data sets.
```php
$ids = $this->connection->fetchFirstColumn(
'SELECT LOWER(HEX(`id`)) FROM `order` WHERE `created_at` = `updated_at`'
);
$criteria = new Criteria($ids);
$orders = $this->orderRepository->search($criteria, $context)->getEntities();
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论