本文整理匯總了PHP中yii\base\Model::children方法的典型用法代碼示例。如果您正苦於以下問題:PHP Model::children方法的具體用法?PHP Model::children怎麽用?PHP Model::children使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類yii\base\Model
的用法示例。
在下文中一共展示了Model::children方法的1個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的PHP代碼示例。
示例1: tree
/**
* Returns a tree data as array
*
* @param bool|Model|NestedSetBehavior $root Parent item for a tree data (false, if from root item)
* @param bool|integer $maxLevel Max level for a tree data (false for no limit)
* @param string $keyAttribute Attribute of model for array key
* @param string[] $extraAttributes Additional attribute names for include to tree
* @return array
*/
public function tree($root = false, $maxLevel = false, $keyAttribute = 'id', array $extraAttributes = [])
{
/** @var NestedSetBehavior|Model $items */
$tree = [];
if ($root === false) {
$ownerClass = $this->owner->modelClass;
$items = $ownerClass::find()->roots()->all();
} else {
if (!$maxLevel || $root->level <= $maxLevel) {
$items = $root->children()->all();
} else {
return $tree;
}
}
foreach ($items as $item) {
$tree[$item->{$keyAttribute}] = ['id' => $item->id, 'name' => $item->{$item->titleAttribute}, 'children' => !$maxLevel || $item->level < $maxLevel ? $this->tree($item, $maxLevel, $keyAttribute, $extraAttributes) : null];
if (count($extraAttributes) > 0) {
$tree[$item->{$keyAttribute}] = ArrayHelper::merge($tree[$item->{$keyAttribute}], $item->toArray($extraAttributes));
}
}
return $tree;
}