diff --git a/src/ActiveQuery.php b/src/ActiveQuery.php index 622378a..6003770 100644 --- a/src/ActiveQuery.php +++ b/src/ActiveQuery.php @@ -269,7 +269,7 @@ class ActiveQuery extends Component implements ISqlBuilder [$sql, $params] = $this->builder->insert($data, true); - return $this->execute($sql, $params)->exec(null, true); + return $this->execute($sql, $params)->exec(); } /** diff --git a/src/Base/Model.php b/src/Base/Model.php index 378fea3..3e2c0b3 100644 --- a/src/Base/Model.php +++ b/src/Base/Model.php @@ -540,7 +540,7 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, T { [$sql, $param] = SqlBuilder::builder(static::query())->insert($param); $dbConnection = $this->getConnection()->createCommand($sql, $param); - if (!($lastId = $dbConnection->save(true, $this->getAutoIncrement()))) { + if (!($lastId = $dbConnection->save(true))) { throw new Exception('保存失败.'); } $lastId = $this->setPrimary((int)$lastId, $param); diff --git a/src/Command.php b/src/Command.php index e511d83..f3286e9 100644 --- a/src/Command.php +++ b/src/Command.php @@ -60,7 +60,7 @@ class Command extends Component */ public function save(bool $isInsert = TRUE, mixed $hasAutoIncrement = null): int|bool|array|string|null { - return $this->execute(static::EXECUTE, $isInsert, $hasAutoIncrement); + return $this->execute(static::EXECUTE); } @@ -111,17 +111,15 @@ class Command extends Component /** * @param $type - * @param null $isInsert - * @param bool|null $hasAutoIncrement * @return int|bool|array|string|null * @throws Exception */ - private function execute($type, $isInsert = null, mixed $hasAutoIncrement = null): int|bool|array|string|null + private function execute($type): int|bool|array|string|null { try { $time = microtime(true); if ($type === static::EXECUTE) { - $result = $this->insert_or_change($isInsert, $hasAutoIncrement); + $result = $this->insert_or_change(); } else { $result = $this->search($type); } @@ -159,19 +157,13 @@ class Command extends Component /** - * @param $isInsert - * @param $hasAutoIncrement * @return bool|int * @throws Exception */ - private function insert_or_change($isInsert, $hasAutoIncrement): bool|int + private function insert_or_change(): bool|int { $pdo = $this->db->getConnect($this->sql); - $result = $pdo->execute($this->sql, $isInsert, $this->params); - if (!is_null($hasAutoIncrement) && $result == 0) { - return false; - } - return $result; + return $pdo->execute($this->sql,$this->params); } /** @@ -184,14 +176,12 @@ class Command extends Component } /** - * @param null $scope - * @param bool $insert * @return int|bool|array|string|null * @throws Exception */ - public function exec($scope = null, bool $insert = false): int|bool|array|string|null + public function exec(): int|bool|array|string|null { - return $this->execute(static::EXECUTE, $insert, $scope); + return $this->execute(static::EXECUTE); } /** diff --git a/src/Mysql/PDO.php b/src/Mysql/PDO.php index ad92347..9332274 100644 --- a/src/Mysql/PDO.php +++ b/src/Mysql/PDO.php @@ -242,12 +242,11 @@ class PDO implements StopHeartbeatCheck /** * @param string $sql - * @param $isInsert * @param array $params * @return int * @throws Exception */ - public function execute(string $sql, $isInsert, array $params = []): int + public function execute(string $sql, array $params = []): int { $this->_last = time(); $pdo = $this->_pdo(); @@ -257,10 +256,7 @@ class PDO implements StopHeartbeatCheck if ($prepare->execute($params) === false) { throw new Exception($prepare->errorInfo()[2] ?? static::DB_ERROR_MESSAGE); } - $result = 1; - if ($isInsert) { - $result = (int)$pdo->lastInsertId(); - } + $result = (int)$pdo->lastInsertId(); $prepare->closeCursor(); return $result; }