Skip to content

Commit d58ec1b

Browse files
committed
废弃save方法的where参数
1 parent 83185b7 commit d58ec1b

File tree

1 file changed

+21
-40
lines changed

1 file changed

+21
-40
lines changed

src/think/Model.php

Lines changed: 21 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -388,20 +388,22 @@ public function isExists(): bool
388388
* 保存当前数据对象
389389
* @access public
390390
* @param array $data 数据
391-
* @param array $where 更新条件
392391
* @param string $sequence 自增序列名
393392
* @return bool
394393
*/
395-
public function save(array $data = [], array $where = [], string $sequence = null): bool
394+
public function save(array $data = [], string $sequence = null): bool
396395
{
397-
$this->checkBeforeSave($data, $where);
396+
// 数据对象赋值
397+
foreach ($data as $key => $value) {
398+
$this->setAttr($key, $value, $data);
399+
}
398400

399401
// 事件回调
400402
if (false === $this->trigger('before_write')) {
401403
return false;
402404
}
403405

404-
$result = $this->exists ? $this->updateData($where) : $this->insertData($sequence);
406+
$result = $this->exists ? $this->updateData() : $this->insertData($sequence);
405407

406408
if (false === $result) {
407409
return false;
@@ -417,28 +419,6 @@ public function save(array $data = [], array $where = [], string $sequence = nul
417419
return true;
418420
}
419421

420-
/**
421-
* 写入之前检查数据
422-
* @access protected
423-
* @param array $data 数据
424-
* @param array $where 保存条件
425-
* @return void
426-
*/
427-
protected function checkBeforeSave(array $data, $where): void
428-
{
429-
if (!empty($data)) {
430-
// 数据对象赋值
431-
foreach ($data as $key => $value) {
432-
$this->setAttr($key, $value, $data);
433-
}
434-
435-
if (!empty($where)) {
436-
$this->exists = true;
437-
$this->updateWhere = $where;
438-
}
439-
}
440-
}
441-
442422
/**
443423
* 检查数据是否允许写入
444424
* @access protected
@@ -478,13 +458,13 @@ protected function checkAllowFields(array $append = []): array
478458
/**
479459
* 保存写入数据
480460
* @access protected
481-
* @param array $where 保存条件
482461
* @return bool
483462
*/
484-
protected function updateData(array $where): bool
463+
protected function updateData(): bool
485464
{
486465
// 自动更新
487466
$auto = array_merge($this->auto, $this->update);
467+
488468
$this->autoCompleteData($auto);
489469

490470
// 事件回调
@@ -509,10 +489,6 @@ protected function updateData(array $where): bool
509489
$this->data[$this->updateTime] = $data[$this->updateTime];
510490
}
511491

512-
if (empty($where) && !empty($this->updateWhere)) {
513-
$where = $this->updateWhere;
514-
}
515-
516492
// 检查允许字段
517493
$allowFields = $this->checkAllowFields($auto);
518494

@@ -535,14 +511,18 @@ protected function updateData(array $where): bool
535511

536512
if (!empty($array)) {
537513
$where = $array;
514+
} else {
515+
$where = $this->updateWhere;
538516
}
539517

540518
foreach ((array) $this->relationWrite as $name => $val) {
541-
if (is_array($val)) {
542-
foreach ($val as $key) {
543-
if (isset($data[$key])) {
544-
unset($data[$key]);
545-
}
519+
if (!is_array($val)) {
520+
continue;
521+
}
522+
523+
foreach ($val as $key) {
524+
if (isset($data[$key])) {
525+
unset($data[$key]);
546526
}
547527
}
548528
}
@@ -584,6 +564,7 @@ protected function insertData(string $sequence = null): bool
584564
{
585565
// 自动写入
586566
$auto = array_merge($this->auto, $this->insert);
567+
587568
$this->autoCompleteData($auto);
588569

589570
// 时间戳自动写入
@@ -680,7 +661,7 @@ public function saveAll(array $dataSet, bool $replace = true): Collection
680661

681662
foreach ($dataSet as $key => $data) {
682663
if ($this->exists || (!empty($auto) && isset($data[$pk]))) {
683-
$result[$key] = self::update($data, [], $this->field);
664+
$result[$key] = self::update($data, $this->field);
684665
} else {
685666
$result[$key] = self::create($data, $this->field, $this->replace);
686667
}
@@ -794,15 +775,15 @@ public static function create(array $data, array $field = [], bool $replace = fa
794775
* @param array $field 允许字段
795776
* @return static
796777
*/
797-
public static function update(array $data, array $where = [], array $field = [])
778+
public static function update(array $data, array $field = [])
798779
{
799780
$model = new static();
800781

801782
if (!empty($field)) {
802783
$model->allowField($field);
803784
}
804785

805-
$model->isUpdate(true)->save($data, $where);
786+
$model->isUpdate(true)->save($data);
806787

807788
return $model;
808789
}

0 commit comments

Comments
 (0)