From 5aa5d3ea134cc94d0c9c222fa1bd838b52b49a17 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=90=91=E6=9E=97?= Date: Thu, 20 Jan 2022 19:04:15 +0800 Subject: [PATCH] =?UTF-8?q?Revert=20"=E6=94=B9=E5=90=8D"?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This reverts commit fdf58326 --- Collection.php | 13 +++++-------- Connection.php | 10 +++++----- DatabasesProviders.php | 6 +++--- ModelInterface.php | 16 +++++++++++----- 4 files changed, 24 insertions(+), 21 deletions(-) diff --git a/Collection.php b/Collection.php index 928a7f8..3b3725e 100644 --- a/Collection.php +++ b/Collection.php @@ -166,15 +166,12 @@ class Collection extends AbstractCollection public function delete(): bool { $model = $this->getModel(); - if (!$model->hasPrimary()) return false; - $ids = []; - foreach ($this as $item) { - $id = $item->getPrimaryValue(); - if (!empty($id)) { - $ids[] = $id; - } + if ($model->hasPrimary()) { + $key = $model->getPrimary(); + + return $model::query()->whereIn($key, $this->column($key))->delete(); } - return $model::query()->whereIn($model->getPrimary(), $ids)->delete(); + throw new Exception('Must set primary key. if you wante delete'); } /** diff --git a/Connection.php b/Connection.php index fff6b34..8134577 100644 --- a/Connection.php +++ b/Connection.php @@ -75,11 +75,11 @@ class Connection extends Component */ public function init() { - $this->eventProvider->on(OnWorkerStop::class, [$this, 'clear_connection'], 0); - $this->eventProvider->on(OnWorkerExit::class, [$this, 'clear_connection'], 0); - $this->eventProvider->on(BeginTransaction::class, [$this, 'beginTransaction'], 0); - $this->eventProvider->on(Rollback::class, [$this, 'rollback'], 0); - $this->eventProvider->on(Commit::class, [$this, 'commit'], 0); + $this->getEventProvider()->on(OnWorkerStop::class, [$this, 'clear_connection'], 0); + $this->getEventProvider()->on(OnWorkerExit::class, [$this, 'clear_connection'], 0); + $this->getEventProvider()->on(BeginTransaction::class, [$this, 'beginTransaction'], 0); + $this->getEventProvider()->on(Rollback::class, [$this, 'rollback'], 0); + $this->getEventProvider()->on(Commit::class, [$this, 'commit'], 0); } diff --git a/DatabasesProviders.php b/DatabasesProviders.php index 9087e05..f1e00fb 100644 --- a/DatabasesProviders.php +++ b/DatabasesProviders.php @@ -28,9 +28,9 @@ class DatabasesProviders extends Providers */ public function onImport(Application $application) { - $this->eventProvider->on(OnWorkerStart::class, [$this, 'createPool']); - $this->eventProvider->on(OnProcessStart::class, [$this, 'createPool']); - $this->eventProvider->on(OnTaskerStart::class, [$this, 'createPool']); + $this->getEventProvider()->on(OnWorkerStart::class, [$this, 'createPool']); + $this->getEventProvider()->on(OnProcessStart::class, [$this, 'createPool']); + $this->getEventProvider()->on(OnTaskerStart::class, [$this, 'createPool']); } diff --git a/ModelInterface.php b/ModelInterface.php index 1ece44c..945893e 100644 --- a/ModelInterface.php +++ b/ModelInterface.php @@ -32,11 +32,11 @@ interface ModelInterface public static function find(string|int $param): mixed; - /** - * @param array $data - * @return static - */ - public static function populate(array $data): static; + /** + * @param array $data + * @return static + */ + public static function populate(array $data): static; /** @@ -46,6 +46,12 @@ interface ModelInterface public static function query(): ActiveQuery; + /** + * @return string + */ + public function getPrimary(): string; + + /** * @return string */