Shopware 6数据抽象层:比较两个订单字段/属性。

huangapple go评论61阅读模式
英文:

Shopware 6 Data Abstraction Layer: Compare two order fields/properties with each other

问题

以下是您要翻译的内容:

"代码的目标是获取具有两个不同字段/属性中相同值的所有订单实体,这种情况下是 createdAtupdatedAt

到目前为止,尚未找到这种用例的解决方案,唯一的结果是 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&#39;t do this with just Shopware&#39;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-&gt;connection-&gt;fetchFirstColumn(
    &#39;SELECT LOWER(HEX(`id`)) FROM `order` WHERE `created_at` = `updated_at`&#39;
);

$criteria = new Criteria($ids);
$orders = $this-&gt;orderRepository-&gt;search($criteria, $context)-&gt;getEntities();

huangapple
  • 本文由 发表于 2023年7月17日 18:11:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/76703433.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定