Skip to content

Instantly share code, notes, and snippets.

@antk25
Created September 5, 2016 15:06
Show Gist options
  • Save antk25/615f3f1d9d39a126922ea60c1adc2c05 to your computer and use it in GitHub Desktop.
Save antk25/615f3f1d9d39a126922ea60c1adc2c05 to your computer and use it in GitHub Desktop.
Сниппет, который строит дерево ресурсов глубиной в 2 уровня, от указанного родителя.
<?php
$parent = 3;
$exclude_parents = array(100500,123456);
$template = 4;
$ids = $modx->getChildIds($parent));
$q = $modx->newQuery('modResource', array('parent:IN' => $ids, 'OR:id:IN' => $ids));
$q->andCondition(array('id:NOT IN' => $exclude_parents, 'template' => $template));
$q->select('id,pagetitle,parent');
$resources = array();
if ($q->prepare() && $q->stmt->execute()) {
while ($row = $q->stmt->fetch(PDO::FETCH_ASSOC)) {
if ($row['parent'] == $parent) {
if (isset($resources[$row['id']])) {
$resources[$row['id']] = array_merge($resources[$row['id']], $row);
}
else {
$resources[$row['id']] = $row;
$resources[$row['id']]['children'] = array();
}
}
else {
$resources[$row['parent']]['children'][$row['id']] = $row;
}
}
}
echo'<pre>';print_r($resources);die;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment