kl83 / yii2-sortable-behaviour

Yii2 行为,用于在邻接表中排序和选择记录

v1.1.14 2019-04-06 15:23 UTC

This package is auto-updated.

Last update: 2024-09-07 05:15:41 UTC


README

Latest Stable Version Total Downloads License

提供在邻接表层次结构中对模型实例进行排序和移动的功能。

安装

安装此扩展的首选方式是通过 composer

运行以下命令之一:

php composer.phar require kl83/yii2-sortable-behaviour ~1.1.0

或添加

"kl83/yii2-sortable-behaviour": "~1.1.0"

到您的 composer.json 文件的要求部分。

使用方法

将行为添加到您的模型中。

public function behaviors()
{
    return [
        [
            'class' => 'kl83\behaviours\SortableBehaviour',
            'sortField' => 'sort',
            'parentIdField' => 'parent_id',
        ],
    ];
}

行为配置

行为函数

null moveFirst(int $parentId = false)

将模型实例移动到 $parentId 域的开头。如果未设置 $parentId,则模型实例将是其当前域中的第一个。

null moveLast(int $parentId = false)

将模型实例移动到 $parentId 域的末尾。如果未设置 $parentId,则模型实例将是其当前域中的最后一个。

null moveAfter(Model|int $model)

将模型实例移动到 $model 之后。如果 $model 为空,则将模型实例移动到其域的开头。

null moveBefore(Model|int $model)

将模型实例移动到 $model 之前。

int[] getChildrenId(bool $includeSelf = true, bool $all = true)

返回子模型实例的实例 ID 数组。如果 $all 为 true,则还包括所有子级别。

\yii\db\ActiveQueryInterface getChildren()

返回获取子模型的 ActiveQuery。

\yii\db\ActiveQueryInterface getAllChildren(bool $includeSelf = false)

返回获取子模型实例的 ActiveQuery,包括所有子级别。

Model getParent()

返回父模型的一个实例。

int[] getParentsId(bool $includeSelf = false)

返回父模型实例的实例 ID 数组。按深度级别排序。

\yii\db\ActiveQueryInterface getParents(bool $includeSelf = false)

返回获取父模型实例的 ActiveQuery。

Model getRoot()

返回根模型的一个实例。

string getFullPath(string $delimeter = " → ", callable $callable = false)

返回模型实例的完整路径。$callable 是将模型字符串化为输出路径元素的函数。简单的 $callable 示例

function($model, $isRoot, $isLeaf){
  return $model->title;
}
Model|false getJointParent(Model $model, bool $includeSelf = false)

返回当前模型和 $model 的联合父模型的实例。

null sortAlphabetically(int $parentId = false, int $direction = SORT_ASC)

按字母顺序排序模型实例。

int[] getTreeIds(bool $includeSelf = true, bool $includeParents = false)

返回子模型实例的标识符,包括所有子级别。如果 $includeParents 为 true,还包括父模型的实例 ID。

\yii\db\ActiveQueryInterface getTree(bool $includeSelf = true, bool $includeParents = false)

返回获取子模型实例的 ActiveQuery,包括所有子级别。如果 $includeParents 为 true,还包括父模型的实例。

PHPDoc 模型类标题

 * @method null moveFirst(integer $parentId = false)
 * Moves the model instance to the beginning of $parentId domain.
 * If $parentId is not set, then the model instance will be the first in its current domain.
 *
 * @method null moveLast(integer $parentId = false)
 * Moves the model instance to the end of $parentId domain.
 * If $parentId is not set, then the model instance will be the last in its current domain.
 *
 * @method null moveAfter(self|integer $model)
 * Moves the model instance after $model.
 * If $model is empty, then moves the model instance to the beginning of his domain.
 *
 * @method null moveBefore(self|integer $model)
 * Moves the model instance before $model.
 *
 * @method integer[] getChildrenId(boolean $includeSelf = true, boolean $all = true)
 * Returns an array of instance IDs of child models. If $all is true, it also includes all sublevels.
 *
 * @method \yii\db\ActiveQueryInterface getChildren()
 * Returns ActiveQuery to get child models.
 *
 * @method \yii\db\ActiveQueryInterface getAllChildren(boolean $includeSelf = false)
 * Returns ActiveQuery to get instances of child models, including all sublevels.
 *
 * @method self getParent()
 * Returns an instance of the parent model.
 *
 * @method integer[] getParentsId(boolean $includeSelf = false)
 * Returns an array of instance IDs of parent models. Ordered by depth level.
 *
 * @method \yii\db\ActiveQueryInterface getParents(boolean $includeSelf = false)
 * Returns ActiveQuery to get instances of parent models.
 *
 * @method self getRoot()
 * Returns an instance of the root model.
 *
 * @method string getFullPath(string $delimeter = " → ", callable $callable = false)
 * Returns the full path of the model instance.
 * $callable is function to stringify model for output as path element.
 * Simple $callable example:
 * function($model, $isRoot, $isLeaf){
 *    return $model->title;
 * }
 *
 * @method self|false getJointParent(self $model, boolean $includeSelf = false)
 * Returns an instance of the joint parent of the current model and $model.
 *
 * @method null sortAlphabetically(integer $parentId = false, integer $direction = SORT_ASC)
 * Sorting model instances in alphabetical order.
 *
 * @method integer[] getTreeIds(boolean $includeSelf = true, boolean $includeParents = false)
 * Returns the identifiers of the child model instances, including all sublevels.
 * Also include instance IDs of the parent models, if $includeParents is true.
 *
 * @method \yii\db\ActiveQueryInterface getTree(boolean $includeSelf = true, boolean $includeParents = false)
 * Returns ActiveQuery to get child model instances, including all sublevels.
 * And instances of the parent models, if $includeParents is true.

许可

MIT 许可证