【问题标题】:How to recursively get an array of child ids in Modx Revolution without using getChildIds如何在不使用 getChildIds 的情况下递归获取 Modx Revolution 中的一组子 ID
【发布时间】:2015-02-08 01:27:31
【问题描述】:

如果针对大量资源运行 getChildIds 可能会非常慢,因此我正在尝试编写查询和一些代码来更快地获取所有子 ID。

但是,我从 getChildIds 和我的脚本中得到了不同的结果。

谁能明白为什么这些会产生不同的结果?

使用 getChildIDs 的方法:

$parentDepth = isset($scriptProperties['parentDepth']) ? $scriptProperties['parentDepth'] : 5;

$parents = explode(',', $parents);

$children = array();

foreach ($parents as $parent){

    $ids = $modx->getChildIds($parent,10,array('context' => 'web'));

    foreach($ids as $id){

        $children[] = $id;

    }


}

echo ' number of children = ' . count($children);

使用查询和循环的方法:

$comma_separated = implode(",", $parents);

$sql = "SELECT `id` from modx_site_content where `parent` IN  (".$comma_separated.") and  published = 1 and isfolder = 0 and deleted = 0 and hidemenu = 0;";

$results = $modx->query($sql);

$mychildren = array();

while ($row = $results->fetch(PDO::FETCH_ASSOC)) {


    $mychildren[] = $row['id'];

}


for($i=0; $i <= 10; $i++){

  $comma_separated = implode(",", $mychildren);

  $sql = "SELECT `id` from modx_site_content where `parent` IN  (".$comma_separated.") and  published = 1 and isfolder = 0 and deleted = 0 and hidemenu = 0;";

  $results = $modx->query($sql);

  while ($row = $results->fetch(PDO::FETCH_ASSOC)) {


      $mychildren[] = $row['id'];

  }
}

echo ' number of children = ' . count($mychildren);

getChildIDs 方法需要将近 1.5 秒的时间来运行并提供大约 1100 个结果

SQL/脚本方法在 0.1 秒内运行,并给出 1700 个结果。

要么我没有将子 ID 正确地附加到数组中 ~ 或者 ~ 也许 getChildIDs 正在过滤掉一些其他结果?

有人知道这里会发生什么吗?

【问题讨论】:

    标签: mysql modx-revolution


    【解决方案1】:

    您可以尝试使用built-in method of pdoFetch。

    $pdo = $modx->getService('pdoFetch');
    $ids = $pdo->getChildIds('modResource', 0);
    print_r($ids);
    

    它也是递归的,但在某些情况下可能会更好。 当然,您需要先从存储库中install pdoTools。

    【讨论】:

      【解决方案2】:

      再次查看代码,结果的差异现在非常明显。我将结果附加到子 id 数组中,它插入重复项,因为一个父级可以有多个子级。

      避免重复的解决方案:

      $mychildren[$row['id']] = $row['id'];
      

      【讨论】:

        【解决方案3】:

        getChildIds - 是递归的,所以默认比较慢。

        【讨论】:

        • @ vasis & bezumkin 这里的整个想法是不要使用 modx 臃肿的方法——上面的循环查询运行时间不到 getChildIds 的 1/10。我们正在处理 6000 多个资源的这个特定实例,例如 getResources、getChildIds 等,由于需要解析这么多数据,速度太慢了。
        猜你喜欢
        • 2019-01-02
        • 1970-01-01
        • 2020-08-08
        • 1970-01-01
        • 2022-01-16
        • 1970-01-01
        • 1970-01-01
        • 2013-04-18
        相关资源
        最近更新 更多