Revert "改名"

This reverts commit fdf58326
This commit is contained in:
2022-01-07 14:38:36 +08:00
parent cf2f26ec21
commit 614b601afa
3 changed files with 1058 additions and 1065 deletions
+18 -19
View File
@@ -25,10 +25,7 @@ use Database\Relation;
use Database\SqlBuilder; use Database\SqlBuilder;
use Database\Traits\HasBase; use Database\Traits\HasBase;
use Exception; use Exception;
use JetBrains\PhpStorm\Pure;
use Kiri\Abstracts\Component; use Kiri\Abstracts\Component;
use Kiri\Application;
use Kiri\Events\EventDispatch;
use Kiri\Exception\NotFindClassException; use Kiri\Exception\NotFindClassException;
use Kiri\Kiri; use Kiri\Kiri;
use Kiri\ToArray; use Kiri\ToArray;
@@ -42,8 +39,6 @@ use validator\Validator;
* @package Kiri\Abstracts * @package Kiri\Abstracts
* *
* @property bool $isNowExample * @property bool $isNowExample
* @property Application $container
* @property EventDispatch $eventDispatch
* @property array $attributes * @property array $attributes
* @property array $oldAttributes * @property array $oldAttributes
*/ */
@@ -165,16 +160,6 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, T
} }
/**
* @return EventDispatch
* @throws ReflectionException
*/
protected function getEventDispatch(): EventDispatch
{
return Kiri::getDi()->get(EventDispatch::class);
}
/** /**
* @param $data * @param $data
* @return Model * @return Model
@@ -302,6 +287,20 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, T
return $this->primary; return $this->primary;
} }
/**
* @return bool
* @throws Exception
*/
public function hasPrimaryValue(): bool
{
if ($this->hasPrimary()) {
return !empty($this->{$this->getPrimary()});
}
return false;
}
/** /**
* @return int|null * @return int|null
* @throws Exception * @throws Exception
@@ -513,6 +512,7 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, T
return $this; return $this;
} }
/** /**
* @param $param * @param $param
* @return $this * @return $this
@@ -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);
$lastId = $dbConnection->save(TRUE); $lastId = $dbConnection->save();
$lastId = $this->setPrimary((int)$lastId, $param); $lastId = $this->setPrimary((int)$lastId, $param);
@@ -595,7 +595,7 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, T
return $generate; return $generate;
} }
$command = $this->getConnection()->createCommand($generate[0], $generate[1]); $command = $this->getConnection()->createCommand($generate[0], $generate[1]);
if ($command->save(FALSE, $this)) { if ($command->save()) {
return $this->refresh()->afterSave($fields, $param); return $this->refresh()->afterSave($fields, $param);
} }
return FALSE; return FALSE;
@@ -778,7 +778,6 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, T
/** /**
* @return Relation|null * @return Relation|null
* @throws ReflectionException
*/ */
public function getRelation(): ?Relation public function getRelation(): ?Relation
{ {
@@ -819,7 +818,7 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, T
if (empty($this->table)) { if (empty($this->table)) {
throw new Exception('You need add static method `tableName` and return table name.'); throw new Exception('You need add static method `tableName` and return table name.');
} }
$table = trim($this->table, '{{%}}'); $table = trim($this->table, '{%}');
if (!empty($tablePrefix) && !str_starts_with($table, $tablePrefix)) { if (!empty($tablePrefix) && !str_starts_with($table, $tablePrefix)) {
$table = $tablePrefix . $table; $table = $tablePrefix . $table;
} }
+5 -13
View File
@@ -39,9 +39,6 @@ class Command extends Component
/** @var string */ /** @var string */
public string $dbname = ''; public string $dbname = '';
/** @var PDOStatement|null */
private ?PDOStatement $prepare = null;
/** /**
* @return array|bool|int|string|PDOStatement|null * @return array|bool|int|string|PDOStatement|null
@@ -53,12 +50,10 @@ class Command extends Component
} }
/** /**
* @param bool $isInsert
* @param mixed $hasAutoIncrement
* @return int|bool|array|string|null * @return int|bool|array|string|null
* @throws Exception * @throws Exception
*/ */
public function save(bool $isInsert = TRUE, mixed $hasAutoIncrement = null): int|bool|array|string|null public function save(): int|bool|array|string|null
{ {
return $this->execute(static::EXECUTE); return $this->execute(static::EXECUTE);
} }
@@ -110,11 +105,11 @@ class Command extends Component
} }
/** /**
* @param $type * @param string $type
* @return int|bool|array|string|null * @return int|bool|array|string|null
* @throws Exception * @throws Exception
*/ */
private function execute($type): int|bool|array|string|null private function execute(string $type): int|bool|array|string|null
{ {
try { try {
$time = microtime(true); $time = microtime(true);
@@ -136,11 +131,11 @@ class Command extends Component
/** /**
* @param $type * @param string $type
* @return array|int|bool|null * @return array|int|bool|null
* @throws Exception * @throws Exception
*/ */
private function search($type): array|int|bool|null private function search(string $type): array|int|bool|null
{ {
$pdo = $this->db->getConnect($this->sql); $pdo = $this->db->getConnect($this->sql);
if ($type === static::FETCH_COLUMN) { if ($type === static::FETCH_COLUMN) {
@@ -180,9 +175,6 @@ class Command extends Component
*/ */
public function bindValues(array $data = []): static public function bindValues(array $data = []): static
{ {
if (!is_array($this->params)) {
$this->params = [];
}
if (!empty($data)) { if (!empty($data)) {
$this->params = array_merge($this->params, $data); $this->params = array_merge($this->params, $data);
} }
+14 -12
View File
@@ -17,6 +17,7 @@ use Kiri\Exception\NotFindClassException;
use Kiri\Kiri; use Kiri\Kiri;
use Kiri\ToArray; use Kiri\ToArray;
use ReflectionException; use ReflectionException;
use Swoole\Coroutine;
defined('SAVE_FAIL') or define('SAVE_FAIL', 3227); defined('SAVE_FAIL') or define('SAVE_FAIL', 3227);
defined('FIND_OR_CREATE_MESSAGE') or define('FIND_OR_CREATE_MESSAGE', 'Create a new model, but the data cannot be empty.'); defined('FIND_OR_CREATE_MESSAGE') or define('FIND_OR_CREATE_MESSAGE', 'Create a new model, but the data cannot be empty.');
@@ -213,16 +214,18 @@ class Model extends Base\Model
*/ */
public function delete(): bool public function delete(): bool
{ {
$conditions = $this->_oldAttributes;
if (empty($conditions)) {
return $this->addError("Delete condition do not empty.", 'mysql');
}
$primary = $this->getPrimary(); $primary = $this->getPrimary();
if (empty($primary) || !$this->hasPrimaryValue()) {
if (!empty($primary)) { return $this->addError("Only primary key operations are supported.", 'mysql');
$conditions = [$primary => $this->getAttribute($primary)];
} }
return static::deleteByCondition($conditions); if (!$this->beforeDelete()) {
$result = static::deleteByCondition([$primary => $this->getPrimaryValue()]);
Coroutine::create(function () use ($result) {
$this->afterDelete($result);
});
return $result;
}
return false;
} }
@@ -401,12 +404,11 @@ class Model extends Base\Model
} }
/** /**
* @return bool * @param bool $result
* @throws Exception * @return void
*/ */
public function afterDelete(): bool public function afterDelete(bool $result): void
{ {
return TRUE;
} }
/** /**