diff --git a/Database/ActiveRecord.php b/Database/ActiveRecord.php index 4c965ab3..9c3faeee 100644 --- a/Database/ActiveRecord.php +++ b/Database/ActiveRecord.php @@ -126,7 +126,12 @@ class ActiveRecord extends BaseActiveRecord if (empty($attributes)) { return \logger()->addError(FIND_OR_CREATE_MESSAGE, 'mysql'); } - $select = objectPool(self::class); + + $className = get_called_class(); + $select = objectPool($className, function () use ($className) { + return new $className(); + }); + $select->attributes = $attributes; if (!$select->save()) { throw new Exception($select->getLastError()); @@ -342,7 +347,7 @@ class ActiveRecord extends BaseActiveRecord $relation = $this->getRelation(); - return objectPool(HasOne::class, [$modelName, $foreignKey, $value, $relation]); + return new HasOne($modelName, $foreignKey, $value, $relation); } @@ -363,7 +368,7 @@ class ActiveRecord extends BaseActiveRecord $relation = $this->getRelation(); - return objectPool(HasCount::class, [$modelName, $foreignKey, $value, $relation]); + return new HasCount($modelName, $foreignKey, $value, $relation); } @@ -384,7 +389,7 @@ class ActiveRecord extends BaseActiveRecord $relation = $this->getRelation(); - return objectPool(HasMany::class, [$modelName, $foreignKey, $value, $relation]); + return new HasMany($modelName, $foreignKey, $value, $relation); } /** @@ -404,7 +409,7 @@ class ActiveRecord extends BaseActiveRecord $relation = $this->getRelation(); - return objectPool(HasMany::class, [$modelName, $foreignKey, $value, $relation]); + return new HasMany($modelName, $foreignKey, $value, $relation); } /** diff --git a/Database/Base/AbstractCollection.php b/Database/Base/AbstractCollection.php index 1493bf29..4e61c97c 100644 --- a/Database/Base/AbstractCollection.php +++ b/Database/Base/AbstractCollection.php @@ -114,7 +114,13 @@ abstract class AbstractCollection extends Component implements \IteratorAggregat */ public function getModel(): ActiveRecord { - return objectPool($this->model); + $model = $this->model;; + if (is_object($model)) { + return $model; + } + return objectPool($model, function () use ($model) { + return new $model(); + }); } diff --git a/Database/Base/BaseActiveRecord.php b/Database/Base/BaseActiveRecord.php index 9c11189e..85ff1a72 100644 --- a/Database/Base/BaseActiveRecord.php +++ b/Database/Base/BaseActiveRecord.php @@ -907,8 +907,12 @@ abstract class BaseActiveRecord extends Component implements IOrm, ArrayAccess */ public static function populate(array $data): static { + $className = get_called_class(); + /** @var static $model */ - $model = objectPool(get_called_class()); + $model = objectPool($className, function () use ($className) { + return new $className(); + }); $model->attributes = $data; $model->setIsCreate(false); return $model; diff --git a/Database/Base/CollectionIterator.php b/Database/Base/CollectionIterator.php index 220cec04..00725a5e 100644 --- a/Database/Base/CollectionIterator.php +++ b/Database/Base/CollectionIterator.php @@ -56,7 +56,13 @@ class CollectionIterator extends \ArrayIterator */ protected function newModel($current): ActiveRecord { - return objectPool($this->model)->setAttributes($current); + $model = $this->model; + if (is_object($model)) { + return $model->setAttributes($current); + } + return objectPool($model, function () use ($model) { + return new $model(); + })->setAttributes($current); } diff --git a/Database/Collection.php b/Database/Collection.php index 2e2693e3..337406ca 100644 --- a/Database/Collection.php +++ b/Database/Collection.php @@ -198,7 +198,7 @@ class Collection extends AbstractCollection } $_filters[] = $value; } - return objectPool(Collection::class, [$this->query, $_filters, $this->model]); + return new Collection($this->query, $_filters, $this->model); } diff --git a/System/Pool/ObjectPool.php b/System/Pool/ObjectPool.php index e77a4113..c7e69e16 100644 --- a/System/Pool/ObjectPool.php +++ b/System/Pool/ObjectPool.php @@ -37,11 +37,11 @@ class ObjectPool extends \Snowflake\Abstracts\Pool /** * @param array $config - * @param array $construct + * @param callable $construct * @return mixed * @throws Exception */ - public function load(mixed $config, array $construct = []): mixed + public function load(mixed $config, callable $construct): mixed { if (is_object($config)) { return $config; @@ -57,10 +57,7 @@ class ObjectPool extends \Snowflake\Abstracts\Pool */ public function createClient(string $name, mixed $config): mixed { - if (isset($config[1])) { - return new $config[0](...$config[1]); - } - return new $config[0](); + return call_user_func($config[1]); } diff --git a/function.php b/function.php index c6050753..e8786648 100644 --- a/function.php +++ b/function.php @@ -222,12 +222,11 @@ if (!function_exists('objectPool')) { /** * @param string $className - * @param array $construct + * @param callable $construct * @return mixed * @throws ComponentException - * @throws Exception */ - function objectPool(mixed $className, array $construct = []): mixed + function objectPool(mixed $className, callable $construct): mixed { return Snowflake::app()->getObject()->load($className, $construct); }