This commit is contained in:
2021-11-10 02:41:39 +08:00
parent ee44cfbca9
commit 6b78d130fb
4 changed files with 11 additions and 25 deletions
+1 -1
View File
@@ -269,7 +269,7 @@ class ActiveQuery extends Component implements ISqlBuilder
[$sql, $params] = $this->builder->insert($data, true); [$sql, $params] = $this->builder->insert($data, true);
return $this->execute($sql, $params)->exec(null, true); return $this->execute($sql, $params)->exec();
} }
/** /**
+1 -1
View File
@@ -540,7 +540,7 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, T
{ {
[$sql, $param] = SqlBuilder::builder(static::query())->insert($param); [$sql, $param] = SqlBuilder::builder(static::query())->insert($param);
$dbConnection = $this->getConnection()->createCommand($sql, $param); $dbConnection = $this->getConnection()->createCommand($sql, $param);
if (!($lastId = $dbConnection->save(true, $this->getAutoIncrement()))) { if (!($lastId = $dbConnection->save(true))) {
throw new Exception('保存失败.'); throw new Exception('保存失败.');
} }
$lastId = $this->setPrimary((int)$lastId, $param); $lastId = $this->setPrimary((int)$lastId, $param);
+7 -17
View File
@@ -60,7 +60,7 @@ class Command extends Component
*/ */
public function save(bool $isInsert = TRUE, mixed $hasAutoIncrement = null): int|bool|array|string|null 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 $type
* @param null $isInsert
* @param bool|null $hasAutoIncrement
* @return int|bool|array|string|null * @return int|bool|array|string|null
* @throws Exception * @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 { try {
$time = microtime(true); $time = microtime(true);
if ($type === static::EXECUTE) { if ($type === static::EXECUTE) {
$result = $this->insert_or_change($isInsert, $hasAutoIncrement); $result = $this->insert_or_change();
} else { } else {
$result = $this->search($type); $result = $this->search($type);
} }
@@ -159,19 +157,13 @@ class Command extends Component
/** /**
* @param $isInsert
* @param $hasAutoIncrement
* @return bool|int * @return bool|int
* @throws Exception * @throws Exception
*/ */
private function insert_or_change($isInsert, $hasAutoIncrement): bool|int private function insert_or_change(): bool|int
{ {
$pdo = $this->db->getConnect($this->sql); $pdo = $this->db->getConnect($this->sql);
$result = $pdo->execute($this->sql, $isInsert, $this->params); return $pdo->execute($this->sql,$this->params);
if (!is_null($hasAutoIncrement) && $result == 0) {
return false;
}
return $result;
} }
/** /**
@@ -184,14 +176,12 @@ class Command extends Component
} }
/** /**
* @param null $scope
* @param bool $insert
* @return int|bool|array|string|null * @return int|bool|array|string|null
* @throws Exception * @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);
} }
/** /**
+2 -6
View File
@@ -242,12 +242,11 @@ class PDO implements StopHeartbeatCheck
/** /**
* @param string $sql * @param string $sql
* @param $isInsert
* @param array $params * @param array $params
* @return int * @return int
* @throws Exception * @throws Exception
*/ */
public function execute(string $sql, $isInsert, array $params = []): int public function execute(string $sql, array $params = []): int
{ {
$this->_last = time(); $this->_last = time();
$pdo = $this->_pdo(); $pdo = $this->_pdo();
@@ -257,10 +256,7 @@ class PDO implements StopHeartbeatCheck
if ($prepare->execute($params) === false) { if ($prepare->execute($params) === false) {
throw new Exception($prepare->errorInfo()[2] ?? static::DB_ERROR_MESSAGE); throw new Exception($prepare->errorInfo()[2] ?? static::DB_ERROR_MESSAGE);
} }
$result = 1; $result = (int)$pdo->lastInsertId();
if ($isInsert) {
$result = (int)$pdo->lastInsertId();
}
$prepare->closeCursor(); $prepare->closeCursor();
return $result; return $result;
} }