diff --git a/src/Concern/HasAttributes.php b/src/Concern/HasAttributes.php index 6e99713..f6ecde4 100644 --- a/src/Concern/HasAttributes.php +++ b/src/Concern/HasAttributes.php @@ -114,6 +114,18 @@ public function getAttribute(string $key): array return [$pro, $value]; } + /** + * Get an attribute value from the model. + * + * @param string $key + * @return mixed + * @throws DbException + */ + public function getAttributeValue(string $key) + { + return $this->getAttribute($key)[1]; + } + /** * Get an not hidden attribute from the model. * @@ -390,7 +402,7 @@ public function only(array $attributes) $results = []; foreach ($attributes as $attribute) { - $results[$attribute] = $this->getAttribute($attribute); + $results[$attribute] = $this->getAttributeValue($attribute); } return $results; diff --git a/src/Connection/Connection.php b/src/Connection/Connection.php index 2d4f532..27009a6 100644 --- a/src/Connection/Connection.php +++ b/src/Connection/Connection.php @@ -102,6 +102,7 @@ public function initialize(Pool $pool, Database $database) { $this->pool = $pool; $this->database = $database; + $this->lastTime = time(); // We need to initialize a query grammar and the query post processors // which are both very important parts of the database abstractions @@ -177,6 +178,20 @@ public function create(): void $this->createReadPdo(); } + /** + * Close connection + */ + public function close(): void + { + if (!empty($this->pdo)) { + $this->pdo = null; + } + + if (!empty($this->readPdo)) { + $this->readPdo = null; + } + } + /** * Reconnect */ @@ -213,14 +228,6 @@ public function release(bool $force = false): void } } - /** - * @return int - */ - public function getLastTime(): int - { - return time(); - } - /** * @return Grammar */ diff --git a/src/Eloquent/Builder.php b/src/Eloquent/Builder.php index 542f5f4..3bf1cd4 100644 --- a/src/Eloquent/Builder.php +++ b/src/Eloquent/Builder.php @@ -566,7 +566,7 @@ public function firstOr(array $columns = ['*'], Closure $callback = null) public function value(string $column) { if ($result = $this->first([$column])) { - return $result->getAttribute($column)[1]; + return $result->getAttributeValue($column); } return null; @@ -666,7 +666,7 @@ public function chunkById(int $count, callable $callback, string $column = null, /* @var Model $last */ $last = $results->last(); - $lastId = $last->getAttribute($alias)[1]; + $lastId = $last->getAttributeValue($alias); unset($results); } while ($countResults == $count); diff --git a/src/Eloquent/Collection.php b/src/Eloquent/Collection.php index 7ce76e7..41a4633 100644 --- a/src/Eloquent/Collection.php +++ b/src/Eloquent/Collection.php @@ -175,7 +175,7 @@ public function diff($items) foreach ($this->items as $item) { /* @var Model $item */ - if (!isset($dictionary[$item->getKey()[1]])) { + if (!isset($dictionary[$item->getKey()])) { $diff->add($item); } } @@ -199,7 +199,7 @@ public function intersect($items) foreach ($this->items as $item) { /* @var Model $item */ - if (isset($dictionary[$item->getKey()[1]])) { + if (isset($dictionary[$item->getKey()])) { $intersect->add($item); } } diff --git a/src/Eloquent/Model.php b/src/Eloquent/Model.php index 57758a1..f9db152 100644 --- a/src/Eloquent/Model.php +++ b/src/Eloquent/Model.php @@ -342,7 +342,7 @@ protected function incrementOrDecrement(string $column, $amount, array $extra, s $this->incrementOrDecrementAttributeValue($column, $amount, $extra, $method); return $query->where( - $this->getKeyName(), $this->getKey()[1] + $this->getKeyName(), $this->getKey() )->{$method}($column, $amount, $extra); } @@ -369,11 +369,7 @@ protected function incrementOrDecrement(string $column, $amount, array $extra, s protected function incrementOrDecrementAttributeValue(string $column, $amount, $extra, $method) { $columnValue = $method === 'increment' ? $amount : $amount * -1; - if (!property_exists($this, $column)) { - $this->setAttribute($column, $this->getAttribute($column)[1] + $columnValue); - } else { - $this->{$column} = $this->{$column} + $columnValue; - } + $this->setAttribute($column, $this->getAttributeValue($column) + $columnValue); $this->fill($extra); @@ -893,12 +889,12 @@ public function getIncrementing() /** * Get the value of the model's primary key. * - * @return array + * @return string * @throws DbException */ public function getKey() { - return $this->getAttribute($this->getKeyName()); + return $this->getAttributeValue($this->getKeyName()); } /** @@ -959,7 +955,7 @@ public function offsetExists($offset) */ public function offsetGet($offset) { - return $this->getAttribute($offset); + return $this->getAttributeValue($offset); } /** diff --git a/src/Listener/WorkerStopAndErrorListener.php b/src/Listener/WorkerStopAndErrorListener.php new file mode 100644 index 0000000..a434470 --- /dev/null +++ b/src/Listener/WorkerStopAndErrorListener.php @@ -0,0 +1,54 @@ + 'handle', + SwoftEvent::WORKER_SHUTDOWN => 'handle', + ]; + } + + /** + * @param EventInterface $event + */ + public function handle(EventInterface $event): void + { + go(function () use ($event){ + $pools = BeanFactory::getBeans(Pool::class); + + /* @var Pool $pool */ + foreach ($pools as $pool) { + $count = $pool->close(); + + CLog::info('Close %d database connection on %s!', $count, $event->getName()); + } + }); + + Event::wait(); + } +} \ No newline at end of file diff --git a/src/Query/Builder.php b/src/Query/Builder.php index 3aeef57..aa8ceaf 100644 --- a/src/Query/Builder.php +++ b/src/Query/Builder.php @@ -2403,7 +2403,7 @@ public function paginate(int $page = 1, int $perPage = 15, array $columns = ['*' // Run a pagination count query $count = $this->getCountForPagination($columns); // Get paginate records - $list = $this->forPage($page, $perPage)->addSelect($columns)->get(); + $list = $this->forPage($page, $perPage)->addSelect($columns)->get()->toArray(); return [ 'count' => $count, diff --git a/test/testing/Entity/User.php b/test/testing/Entity/User.php index f9de5fc..fc99616 100644 --- a/test/testing/Entity/User.php +++ b/test/testing/Entity/User.php @@ -46,10 +46,36 @@ class User extends Model /** * @Column(name="user_desc", prop="udesc") + * * @var string|null */ private $userDesc; + /** + * this key is hump + * + * @Column() + * + * @var string|null + */ + private $testHump; + + /** + * @return null|string + */ + public function getTestHump(): ?string + { + return $this->testHump; + } + + /** + * @param null|string $testHump + */ + public function setTestHump(?string $testHump): void + { + $this->testHump = $testHump; + } + /** * @return int|null */ diff --git a/test/unit/Eloquent/ModelTest.php b/test/unit/Eloquent/ModelTest.php index c1a7032..21d0928 100644 --- a/test/unit/Eloquent/ModelTest.php +++ b/test/unit/Eloquent/ModelTest.php @@ -380,4 +380,17 @@ public function testPaginate() $this->assertEquals($res['page'], $page); $this->assertEquals($res['perPage'], $perPage); } + + public function testFull() + { + $expect = "testHump,哈"; + $attributes = ['testHump' => $expect]; + + $user = User::new($attributes); + $this->assertEquals($expect, $user->getTestHump()); + + $userArray = User::new()->fill($attributes)->toArray(); + $this->assertArrayHasKey('testHump', $userArray); + $this->assertEquals($expect, $userArray['testHump']); + } }