Skip to content

Commit

Permalink
column方法支持json字段属性
Browse files Browse the repository at this point in the history
模型对象实例化支持传入对象
  • Loading branch information
liu21st committed Feb 12, 2023
1 parent 6bf1b41 commit 653adaf
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 33 deletions.
24 changes: 10 additions & 14 deletions src/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -238,20 +238,12 @@ public function invoke($method, array $vars = [])
/**
* 架构函数.
*
* @param array $data 数据
* @param array|Model $data 数据
*/
public function __construct(array $data = [])
public function __construct(array|Model $data = [])
{
$this->data = $data;

if (!empty($this->data)) {
// 废弃字段
foreach ($this->disuse as $key) {
if (array_key_exists($key, $this->data)) {
unset($this->data[$key]);
}
}
}
// 设置数据
$this->data($data);

// 记录原始数据
$this->origin = $this->data;
Expand Down Expand Up @@ -556,13 +548,17 @@ public function lazySave(array|bool $data = []): void
/**
* 保存当前数据对象
*
* @param array $data 数据
* @param array|Model $data 数据
* @param string $sequence 自增序列名
*
* @return bool
*/
public function save(array $data = [], string $sequence = null): bool
public function save(array|Model $data = [], string $sequence = null): bool
{
if ($data instanceof Model) {
$data = $data->getData();
}

// 数据对象赋值
$this->setAttrs($data);

Expand Down
4 changes: 4 additions & 0 deletions src/db/PDOConnection.php
Original file line number Diff line number Diff line change
Expand Up @@ -1317,6 +1317,10 @@ public function column(BaseQuery $query, string|array $column, string $key = '')
[$alias, $column] = explode('.', $column);
}

if (str_contains($column, '->')) {
$column = $this->builder->parseKey($query, $column);
}

$result = array_column($resultSet, $column, $key);
} elseif ($key) {
$result = array_column($resultSet, null, $key);
Expand Down
8 changes: 6 additions & 2 deletions src/model/concern/Attribute.php
Original file line number Diff line number Diff line change
Expand Up @@ -213,14 +213,18 @@ protected function getRealFieldName(string $name): string
/**
* 设置数据对象值
*
* @param array $data 数据
* @param array|Model $data 数据
* @param bool $set 是否调用修改器
* @param array $allow 允许的字段名
*
* @return $this
*/
public function data(array $data, bool $set = false, array $allow = [])
public function data(array|Model $data, bool $set = false, array $allow = [])
{
if ($data instanceof Model) {
$data = $data->getData();
}

// 清空数据
$this->data = [];

Expand Down
10 changes: 5 additions & 5 deletions src/model/relation/HasMany.php
Original file line number Diff line number Diff line change
Expand Up @@ -237,16 +237,16 @@ protected function eagerlyOneToMany(array $where, array $subRelation = [], Closu
/**
* 保存(新增)当前关联数据对象
*
* @param mixed $data 数据 可以使用数组 关联模型对象
* @param array|Model $data 数据 可以使用数组 关联模型对象
* @param bool $replace 是否自动识别更新和写入
*
* @return Model|false
*/
public function save($data, bool $replace = true)
public function save(array|Model $data, bool $replace = true)
{
$model = $this->make($data);
$model = $this->make();

return $model->replace($replace)->save() ? $model : false;
return $model->replace($replace)->save($data) ? $model : false;
}

/**
Expand All @@ -256,7 +256,7 @@ public function save($data, bool $replace = true)
*
* @return Model
*/
public function make($data = []): Model
public function make(array|Model $data = []): Model
{
if ($data instanceof Model) {
$data = $data->getData();
Expand Down
4 changes: 2 additions & 2 deletions src/model/relation/MorphMany.php
Original file line number Diff line number Diff line change
Expand Up @@ -294,12 +294,12 @@ protected function eagerlyMorphToMany(array $where, array $subRelation = [], Clo
/**
* 保存(新增)当前关联数据对象
*
* @param mixed $data 数据 可以使用数组 关联模型对象
* @param array|Model $data 数据 可以使用数组 关联模型对象
* @param bool $replace 是否自动识别更新和写入
*
* @return Model|false
*/
public function save($data, bool $replace = true)
public function save(array|Model $data, bool $replace = true)
{
$model = $this->make();

Expand Down
10 changes: 5 additions & 5 deletions src/model/relation/MorphOne.php
Original file line number Diff line number Diff line change
Expand Up @@ -269,16 +269,16 @@ protected function eagerlyMorphToOne(array $where, array $subRelation = [], $clo
/**
* 保存(新增)当前关联数据对象
*
* @param mixed $data 数据 可以使用数组 关联模型对象
* @param array|Model $data 数据 可以使用数组 关联模型对象
* @param bool $replace 是否自动识别更新和写入
*
* @return Model|false
*/
public function save($data, bool $replace = true)
public function save(array|Model $data, bool $replace = true)
{
$model = $this->make($data);
$model = $this->make();

return $model->replace($replace)->save() ? $model : false;
return $model->replace($replace)->save($data) ? $model : false;
}

/**
Expand All @@ -288,7 +288,7 @@ public function save($data, bool $replace = true)
*
* @return Model
*/
public function make($data = []): Model
public function make(array|Model $data = []): Model
{
if ($data instanceof Model) {
$data = $data->getData();
Expand Down
10 changes: 5 additions & 5 deletions src/model/relation/OneToOne.php
Original file line number Diff line number Diff line change
Expand Up @@ -204,16 +204,16 @@ public function eagerlyResult(Model $result, string $relation, array $subRelatio
/**
* 保存(新增)当前关联数据对象
*
* @param mixed $data 数据 可以使用数组 关联模型对象
* @param array|Model $data 数据 可以使用数组 关联模型对象
* @param bool $replace 是否自动识别更新和写入
*
* @return Model|false
*/
public function save($data, bool $replace = true)
public function save(array|Model $data, bool $replace = true)
{
$model = $this->make($data);
$model = $this->make();

return $model->replace($replace)->save() ? $model : false;
return $model->replace($replace)->save($data) ? $model : false;
}

/**
Expand All @@ -223,7 +223,7 @@ public function save($data, bool $replace = true)
*
* @return Model
*/
public function make($data = []): Model
public function make(array|Model $data = []): Model
{
if ($data instanceof Model) {
$data = $data->getData();
Expand Down

0 comments on commit 653adaf

Please sign in to comment.