This commit is contained in:
2023-07-25 09:33:24 +08:00
parent 4c6dbc9b3c
commit 4abc1a621b
3 changed files with 288 additions and 275 deletions
+24 -12
View File
@@ -40,15 +40,15 @@ class ActiveQuery extends Component implements ISqlBuilder
* 参数绑定 * 参数绑定
*/ */
public array $attributes = []; public array $attributes = [];
protected mixed $_mock = null;
/** /**
* Comply constructor. * Comply constructor.
* @param $model * @param $model
* @param array $config
* @throws * @throws
*/ */
public function __construct($model, array $config = []) public function __construct($model)
{ {
$this->modelClass = $model; $this->modelClass = $model;
@@ -180,10 +180,18 @@ class ActiveQuery extends Component implements ISqlBuilder
/** /**
* @return array|Collection * @return array|Collection
* @throws Exception
*/ */
public function get(): Collection|array public function get(): Collection|array
{ {
return $this->all(); $data = $this->execute($this->builder->all(), $this->attributes)->all();
if ($data === false) {
return new Collection($this, [], $this->modelClass);
}
$collect = new Collection($this, $data, $this->modelClass);
return $this->asArray ? $collect->toArray() : $collect;
} }
@@ -221,26 +229,30 @@ class ActiveQuery extends Component implements ISqlBuilder
*/ */
public function column(string $field, string $setKey = ''): ?array public function column(string $field, string $setKey = ''): ?array
{ {
return $this->all()->column($field, $setKey); return $this->get()->column($field, $setKey);
} }
/** /**
* @return array|Collection * @param mixed $value
* @throws * @return $this
*/ */
public function all(): Collection|array public function withMock(mixed $value): static
{ {
$data = $this->execute($this->builder->all(), $this->attributes)->all(); $this->_mock = $value;
if ($data === false) { return $this;
return new Collection($this, [], $this->modelClass);
} }
$collect = new Collection($this, $data, $this->modelClass);
return $this->asArray ? $collect->toArray() : $collect; /**
* @return mixed
*/
public function mock(): mixed
{
return $this->_mock;
} }
/** /**
* @param $data * @param $data
* @return ModelInterface|array * @return ModelInterface|array
+2 -2
View File
@@ -229,7 +229,7 @@ class Model extends Base\Model
*/ */
public static function get($condition): Collection|array public static function get($condition): Collection|array
{ {
return static::query()->where($condition)->all(); return static::query()->where($condition)->get();
} }
@@ -246,7 +246,7 @@ class Model extends Base\Model
if (!empty($attributes)) { if (!empty($attributes)) {
$query->bindParams($attributes); $query->bindParams($attributes);
} }
return $query->all(); return $query->get();
} }
+3 -2
View File
@@ -60,7 +60,7 @@ class Relation extends Component
} }
$activeModel = $this->_query[$_identification]->first(); $activeModel = $this->_query[$_identification]->first();
if (empty($activeModel)) { if (empty($activeModel)) {
return null; return $this->_query[$_identification]->mock();
} }
unset($this->_query[$_identification]); unset($this->_query[$_identification]);
return Context::set($_identification, $activeModel); return Context::set($_identification, $activeModel);
@@ -78,7 +78,7 @@ class Relation extends Component
} }
$activeModel = $this->_query[$_identification]->count(); $activeModel = $this->_query[$_identification]->count();
if (empty($activeModel)) { if (empty($activeModel)) {
return null; return $this->_query[$_identification]->mock();
} }
unset($this->_query[$_identification]); unset($this->_query[$_identification]);
return Context::set($_identification, $activeModel); return Context::set($_identification, $activeModel);
@@ -88,6 +88,7 @@ class Relation extends Component
/** /**
* @param string $_identification * @param string $_identification
* @return mixed * @return mixed
* @throws Exception
*/ */
public function get(string $_identification): mixed public function get(string $_identification): mixed
{ {