update:更新aliasField方法,支持数组

This commit is contained in:
JaguarJack 2021-03-01 21:02:01 +08:00
parent c3b36013d7
commit f45b9315a9

View File

@ -40,7 +40,7 @@ trait BaseOptionsTrait
*/ */
public function storeBy(array $data) public function storeBy(array $data)
{ {
if ($this->allowField($this->field)->save($data)) { if ($this->allowField($this->field)->save($this->filterData($data))) {
return $this->{$this->getPk()}; return $this->{$this->getPk()};
} }
@ -70,7 +70,7 @@ trait BaseOptionsTrait
*/ */
public function updateBy($id, $data, $field = ''): bool public function updateBy($id, $data, $field = ''): bool
{ {
if (static::update($data, [$field ? : $this->getPk() => $id], $this->field)) { if (static::update($this->filterData($data), [$field ? : $this->getPk() => $id], $this->field)) {
return true; return true;
} }
@ -162,11 +162,23 @@ trait BaseOptionsTrait
* *
* @time 2020年01月13日 * @time 2020年01月13日
* @param $field * @param $field
* @return string * @return array|string
*/ */
public function aliasField($field): string public function aliasField($field)
{ {
return sprintf('%s.%s', $this->getTable(), $field); if (is_string($field)) {
return sprintf('%s.%s', $this->getTable(), $field);
}
if (is_array($field)) {
foreach ($field as &$value) {
$value = sprintf('%s.%s', $this->getTable(), $field);
}
return $field;
}
return $field;
} }
/** /**
@ -187,4 +199,26 @@ trait BaseOptionsTrait
return $model->save(); return $model->save();
} }
/**
* 过滤数据
*
* @time 2021年02月28日
* @param $data
* @return mixed
*/
protected function filterData($data)
{
foreach ($data as $field => $value) {
if (is_null($value)) {
unset($data[$field]);
}
if ($field == $this->getPk()) {
unset($data[$field]);
}
}
return $data;
}
} }