修复循环插入数据导致数据错乱

This commit is contained in:
JaguarJack 2020-04-21 10:51:25 +08:00
parent 1b954c1b16
commit e82e058550

View File

@ -12,14 +12,12 @@ trait BaseOptionsTrait
*/ */
public function storeBy(array $data) public function storeBy(array $data)
{ {
if ($this->allowField($this->field)->save($data)) { $model = parent::create($data, $this->field, true);
return $this->{$this->getPk()};
}
return false; return $model->{$this->getPk()};
} }
/** /**33
* *
* @time 2019年12月03日 * @time 2019年12月03日
* @param $id * @param $id
@ -67,6 +65,35 @@ trait BaseOptionsTrait
return static::destroy($id, $force); return static::destroy($id, $force);
} }
/**
* 批量插入
*
* @time 2020年04月19日
* @param array $data
* @return mixed
*/
public function insertAllBy(array $data)
{
$newData = [];
foreach ($data as $item) {
foreach ($item as $field => $value) {
if (!in_array($field, $this->field)) {
unset($item[$field]);
}
if (in_array('created_at', $this->field)) {
$item['created_at'] = time();
}
if (in_array('updated_at', $this->field)) {
$item['updated_at'] = time();
}
}
$newData[] = $item;
}
return $this->insertAll($newData);
}
/** /**
* @time 2019年12月07日 * @time 2019年12月07日
* @param $id * @param $id