This commit is contained in:
2021-02-24 14:22:56 +08:00
parent c704dc05eb
commit 19775e937e
7 changed files with 35 additions and 18 deletions
+10 -5
View File
@@ -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);
}
/**
+7 -1
View File
@@ -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();
});
}
+5 -1
View File
@@ -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;
+7 -1
View File
@@ -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);
}
+1 -1
View File
@@ -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);
}
+3 -6
View File
@@ -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]);
}
+2 -3
View File
@@ -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);
}