改名
This commit is contained in:
@@ -126,7 +126,12 @@ class ActiveRecord extends BaseActiveRecord
|
|||||||
if (empty($attributes)) {
|
if (empty($attributes)) {
|
||||||
return \logger()->addError(FIND_OR_CREATE_MESSAGE, 'mysql');
|
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;
|
$select->attributes = $attributes;
|
||||||
if (!$select->save()) {
|
if (!$select->save()) {
|
||||||
throw new Exception($select->getLastError());
|
throw new Exception($select->getLastError());
|
||||||
@@ -342,7 +347,7 @@ class ActiveRecord extends BaseActiveRecord
|
|||||||
|
|
||||||
$relation = $this->getRelation();
|
$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();
|
$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();
|
$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();
|
$relation = $this->getRelation();
|
||||||
|
|
||||||
return objectPool(HasMany::class, [$modelName, $foreignKey, $value, $relation]);
|
return new HasMany($modelName, $foreignKey, $value, $relation);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -114,7 +114,13 @@ abstract class AbstractCollection extends Component implements \IteratorAggregat
|
|||||||
*/
|
*/
|
||||||
public function getModel(): ActiveRecord
|
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();
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -907,8 +907,12 @@ abstract class BaseActiveRecord extends Component implements IOrm, ArrayAccess
|
|||||||
*/
|
*/
|
||||||
public static function populate(array $data): static
|
public static function populate(array $data): static
|
||||||
{
|
{
|
||||||
|
$className = get_called_class();
|
||||||
|
|
||||||
/** @var static $model */
|
/** @var static $model */
|
||||||
$model = objectPool(get_called_class());
|
$model = objectPool($className, function () use ($className) {
|
||||||
|
return new $className();
|
||||||
|
});
|
||||||
$model->attributes = $data;
|
$model->attributes = $data;
|
||||||
$model->setIsCreate(false);
|
$model->setIsCreate(false);
|
||||||
return $model;
|
return $model;
|
||||||
|
|||||||
@@ -56,7 +56,13 @@ class CollectionIterator extends \ArrayIterator
|
|||||||
*/
|
*/
|
||||||
protected function newModel($current): ActiveRecord
|
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);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -198,7 +198,7 @@ class Collection extends AbstractCollection
|
|||||||
}
|
}
|
||||||
$_filters[] = $value;
|
$_filters[] = $value;
|
||||||
}
|
}
|
||||||
return objectPool(Collection::class, [$this->query, $_filters, $this->model]);
|
return new Collection($this->query, $_filters, $this->model);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -37,11 +37,11 @@ class ObjectPool extends \Snowflake\Abstracts\Pool
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* @param array $config
|
* @param array $config
|
||||||
* @param array $construct
|
* @param callable $construct
|
||||||
* @return mixed
|
* @return mixed
|
||||||
* @throws Exception
|
* @throws Exception
|
||||||
*/
|
*/
|
||||||
public function load(mixed $config, array $construct = []): mixed
|
public function load(mixed $config, callable $construct): mixed
|
||||||
{
|
{
|
||||||
if (is_object($config)) {
|
if (is_object($config)) {
|
||||||
return $config;
|
return $config;
|
||||||
@@ -57,10 +57,7 @@ class ObjectPool extends \Snowflake\Abstracts\Pool
|
|||||||
*/
|
*/
|
||||||
public function createClient(string $name, mixed $config): mixed
|
public function createClient(string $name, mixed $config): mixed
|
||||||
{
|
{
|
||||||
if (isset($config[1])) {
|
return call_user_func($config[1]);
|
||||||
return new $config[0](...$config[1]);
|
|
||||||
}
|
|
||||||
return new $config[0]();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
+2
-3
@@ -222,12 +222,11 @@ if (!function_exists('objectPool')) {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* @param string $className
|
* @param string $className
|
||||||
* @param array $construct
|
* @param callable $construct
|
||||||
* @return mixed
|
* @return mixed
|
||||||
* @throws ComponentException
|
* @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);
|
return Snowflake::app()->getObject()->load($className, $construct);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user