add:新增Collection toTree方法
This commit is contained in:
12
extend/catcher/CatchModelCollection.php
Normal file
12
extend/catcher/CatchModelCollection.php
Normal file
@@ -0,0 +1,12 @@
|
||||
<?php
|
||||
namespace catcher;
|
||||
|
||||
use think\model\Collection;
|
||||
|
||||
class CatchModelCollection extends Collection
|
||||
{
|
||||
public function toTree($pid = 0, $pidField = 'parent_id', $children = 'children')
|
||||
{
|
||||
return Tree::done($this->items, $pid, $pidField, $children);
|
||||
}
|
||||
}
|
@@ -2,7 +2,9 @@
|
||||
|
||||
namespace catcher\traits\db;
|
||||
|
||||
use catcher\CatchModelCollection;
|
||||
use catcher\Utils;
|
||||
use think\Collection;
|
||||
|
||||
trait BaseOptionsTrait
|
||||
{
|
||||
@@ -186,4 +188,25 @@ trait BaseOptionsTrait
|
||||
|
||||
return $model->save();
|
||||
}
|
||||
|
||||
/**
|
||||
* rewrite collection
|
||||
*
|
||||
* @time 2020年10月20日
|
||||
* @param array|iterable $collection
|
||||
* @param string|null $resultSetType
|
||||
* @return CatchModelCollection|mixed
|
||||
*/
|
||||
public function toCollection(iterable $collection = [], string $resultSetType = null): Collection
|
||||
{
|
||||
$resultSetType = $resultSetType ?: $this->resultSetType;
|
||||
|
||||
if ($resultSetType && false !== strpos($resultSetType, '\\')) {
|
||||
$collection = new $resultSetType($collection);
|
||||
} else {
|
||||
$collection = new CatchModelCollection($collection);
|
||||
}
|
||||
|
||||
return $collection;
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user