【问题标题】:Combining two QueryResults in a Extbase Repository在 Extbase 存储库中组合两个 QueryResult
【发布时间】:2013-03-23 01:28:17
【问题描述】:

我正在编写一个 TYPO3 - 网站扩展程序。因为我使用的是 Extbase 框架,所以我有一个存储库类 (Tx_Extbase_Persistence_Repository),我在其中连续执行两个 sql 查询:

$query1 = $this->createQuery();
$query1->statement($sql1);
$res1 = $query1->execute();

$query2 = $this->createQuery();
$query2->statement($sql2);
$res1 = $query2->execute();

$res1 和$res2 都包含Tx_Extbase_Persistence_QueryResult。现在我想返回组合结果,但我不知道这是如何完成的。返回原始数组不是一个选项,因为我依赖于QueryResult 类的函数,而且我想避免组合 sql(UNION, JOIN)。我已经试过了:

$myResult = $this->objectManager->create('Tx_Extbase_Persistence_ObjectStorage')
foreach($res1 as $obj) {
    $myResult->attach($obj);
}
//foreach $res2

..但这会引发错误 ("could not determine the child object type")

那么如何正确组合两个Tx_Extbase_Persistence_QueryResult

编辑:

我的意思是合并不是两个单独的QueryResults,我只想要一个包含$query1$query2 的结果。不幸的是,SQL-UNION 或 JOIN 不是一个选项。

【问题讨论】:

    标签: php typo3 extbase typo3-flow


    【解决方案1】:

    QueryResult 实现了 QueryResultInterface,它扩展了 ArrayAccess 等。 这样就可以使用 offsetSet 方法了。

    foreach ($res2->toArray() as $result) {
      $res1->offsetSet(($res1->count()), $result);
    }
    

    QueryResult $res1 现在也包含来自 $res2 的对象。

    【讨论】:

    • 这可行,但有一个关键缺陷。它指示错误的计数。它显示第一个 QueryResult 的计数。所以如果你插入一个有 10 个项目和一个有 20 个项目,合并的 Result 将包含所有 30 个项目,但 $res1->count() 将只显示 10 个!
    【解决方案2】:

    如果您不需要 QueryResult-Object,您可以使用 array_merge 来执行此操作。

    $res1 = $this->createQuery()->execute();
    $res2 = $this->createQuery()->execute();
    $res = array_merge($res1->toArray(), $res2->toArray());
    

    【讨论】:

      【解决方案3】:

      通过编写组合结果很难猜出你的意思,但是,我只是猜测,你应该使用自定义$query->statement($sql),你应该使用 SQL 的连接获得单个 result等等

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多