修复循环插入数据导致数据错乱
This commit is contained in:
parent
1b954c1b16
commit
e82e058550
@ -12,14 +12,12 @@ trait BaseOptionsTrait
|
||||
*/
|
||||
public function storeBy(array $data)
|
||||
{
|
||||
if ($this->allowField($this->field)->save($data)) {
|
||||
return $this->{$this->getPk()};
|
||||
$model = parent::create($data, $this->field, true);
|
||||
|
||||
return $model->{$this->getPk()};
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
/**33
|
||||
*
|
||||
* @time 2019年12月03日
|
||||
* @param $id
|
||||
@ -67,6 +65,35 @@ trait BaseOptionsTrait
|
||||
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日
|
||||
* @param $id
|
||||
|
Loading…
x
Reference in New Issue
Block a user