modify
This commit is contained in:
@@ -126,7 +126,7 @@ class ActiveRecord extends BaseActiveRecord
|
||||
if (empty($attributes)) {
|
||||
return $logger->addError(FIND_OR_CREATE_MESSAGE, 'mysql');
|
||||
}
|
||||
$select = self::getModelClass();
|
||||
$select = duplicate(static::class);
|
||||
$select->attributes = $attributes;
|
||||
if (!$select->save()) {
|
||||
return $logger->addError($select->getLastError(), 'mysql');
|
||||
@@ -150,7 +150,7 @@ class ActiveRecord extends BaseActiveRecord
|
||||
/** @var static $select */
|
||||
$select = static::find()->where($condition)->first();
|
||||
if (empty($select)) {
|
||||
$select = self::getModelClass();
|
||||
$select = duplicate(static::class);
|
||||
}
|
||||
$select->attributes = $attributes;
|
||||
if (!$select->save()) {
|
||||
@@ -160,20 +160,6 @@ class ActiveRecord extends BaseActiveRecord
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @return static
|
||||
* @throws Exception
|
||||
*/
|
||||
private static function getModelClass(): static
|
||||
{
|
||||
/** @var Channel $channel */
|
||||
$channel = Snowflake::app()->get('channel');
|
||||
return $channel->pop(static::class, function () {
|
||||
return new static();
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param $action
|
||||
* @param $columns
|
||||
@@ -306,10 +292,6 @@ class ActiveRecord extends BaseActiveRecord
|
||||
$data[$key] = $this->{$item}($data[$key] ?? null);
|
||||
}
|
||||
$data = array_merge($data, $this->runRelate());
|
||||
|
||||
$class = Snowflake::app()->getChannel();
|
||||
$class->push($this, static::class);
|
||||
|
||||
return $data;
|
||||
}
|
||||
|
||||
|
||||
@@ -26,142 +26,136 @@ use Traversable;
|
||||
abstract class AbstractCollection extends Component implements \IteratorAggregate, \ArrayAccess
|
||||
{
|
||||
|
||||
/**
|
||||
* @var ActiveRecord[]
|
||||
*/
|
||||
protected array $_item = [];
|
||||
/**
|
||||
* @var ActiveRecord[]
|
||||
*/
|
||||
protected array $_item = [];
|
||||
|
||||
protected ActiveRecord|string|null $model;
|
||||
protected ActiveRecord|string|null $model;
|
||||
|
||||
protected ActiveQuery $query;
|
||||
protected ActiveQuery $query;
|
||||
|
||||
|
||||
public function clean()
|
||||
{
|
||||
unset($this->query, $this->model, $this->_item);
|
||||
}
|
||||
public function clean()
|
||||
{
|
||||
unset($this->query, $this->model, $this->_item);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Collection constructor.
|
||||
*
|
||||
* @param $query
|
||||
* @param array $array
|
||||
* @param string|ActiveRecord|null $model
|
||||
* @throws Exception
|
||||
*/
|
||||
public function __construct($query, array $array = [], string|ActiveRecord $model = null)
|
||||
{
|
||||
$this->_item = $array;
|
||||
$this->query = $query;
|
||||
$this->model = $model;
|
||||
/**
|
||||
* Collection constructor.
|
||||
*
|
||||
* @param $query
|
||||
* @param array $array
|
||||
* @param string|ActiveRecord|null $model
|
||||
* @throws Exception
|
||||
*/
|
||||
public function __construct($query, array $array = [], string|ActiveRecord $model = null)
|
||||
{
|
||||
$this->_item = $array;
|
||||
$this->query = $query;
|
||||
$this->model = duplicate($model);
|
||||
|
||||
parent::__construct([]);
|
||||
}
|
||||
parent::__construct([]);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @return int
|
||||
*/
|
||||
#[Pure] public function getLength(): int
|
||||
{
|
||||
return count($this->_item);
|
||||
}
|
||||
/**
|
||||
* @return int
|
||||
*/
|
||||
#[Pure] public function getLength(): int
|
||||
{
|
||||
return count($this->_item);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param $item
|
||||
*/
|
||||
public function setItems($item)
|
||||
{
|
||||
$this->_item = $item;
|
||||
}
|
||||
/**
|
||||
* @param $item
|
||||
*/
|
||||
public function setItems($item)
|
||||
{
|
||||
$this->_item = $item;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param $model
|
||||
*/
|
||||
public function setModel($model)
|
||||
{
|
||||
$this->model = $model;
|
||||
}
|
||||
/**
|
||||
* @param $model
|
||||
*/
|
||||
public function setModel($model)
|
||||
{
|
||||
$this->model = $model;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $item
|
||||
*/
|
||||
public function addItem($item)
|
||||
{
|
||||
array_push($this->_item, $item);
|
||||
}
|
||||
/**
|
||||
* @param $item
|
||||
*/
|
||||
public function addItem($item)
|
||||
{
|
||||
array_push($this->_item, $item);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Traversable|CollectionIterator|ArrayIterator
|
||||
* @throws Exception
|
||||
*/
|
||||
public function getIterator(): Traversable|CollectionIterator|ArrayIterator
|
||||
{
|
||||
return new CollectionIterator($this->model, $this->query, $this->_item);
|
||||
}
|
||||
/**
|
||||
* @return Traversable|CollectionIterator|ArrayIterator
|
||||
* @throws Exception
|
||||
*/
|
||||
public function getIterator(): Traversable|CollectionIterator|ArrayIterator
|
||||
{
|
||||
return new CollectionIterator($this->model, $this->query, $this->_item);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @return mixed
|
||||
* @throws Exception
|
||||
*/
|
||||
public function getModel(): ActiveRecord
|
||||
{
|
||||
if (!is_object($this->model)) {
|
||||
$this->model = duplicate($this->model);
|
||||
$this->model->setIsCreate(false);
|
||||
}
|
||||
return $this->model;
|
||||
}
|
||||
/**
|
||||
* @return mixed
|
||||
* @throws Exception
|
||||
*/
|
||||
public function getModel(): ActiveRecord
|
||||
{
|
||||
return $this->model;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param mixed $offset
|
||||
* @return bool
|
||||
*/
|
||||
public function offsetExists(mixed $offset): bool
|
||||
{
|
||||
return !empty($this->_item) && isset($this->_item[$offset]);
|
||||
}
|
||||
/**
|
||||
* @param mixed $offset
|
||||
* @return bool
|
||||
*/
|
||||
public function offsetExists(mixed $offset): bool
|
||||
{
|
||||
return !empty($this->_item) && isset($this->_item[$offset]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param mixed $offset
|
||||
* @return ActiveRecord|null
|
||||
* @throws Exception
|
||||
*/
|
||||
public function offsetGet(mixed $offset): ?ActiveRecord
|
||||
{
|
||||
if (!$this->offsetExists($offset)) {
|
||||
return NULL;
|
||||
}
|
||||
/**
|
||||
* @param mixed $offset
|
||||
* @return ActiveRecord|null
|
||||
* @throws Exception
|
||||
*/
|
||||
public function offsetGet(mixed $offset): ?ActiveRecord
|
||||
{
|
||||
if (!$this->offsetExists($offset)) {
|
||||
return NULL;
|
||||
}
|
||||
if (!($this->_item[$offset] instanceof ActiveRecord)) {
|
||||
return $this->model->setAttributes($this->_item[$offset]);
|
||||
}
|
||||
return $this->_item[$offset];
|
||||
}
|
||||
|
||||
if ($this->_item[$offset] instanceof ActiveRecord) {
|
||||
return $this->_item[$offset];
|
||||
}
|
||||
|
||||
return $this->model::populate($this->_item[$offset]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param mixed $offset
|
||||
* @param mixed $value
|
||||
*/
|
||||
public function offsetSet(mixed $offset, mixed $value)
|
||||
{
|
||||
$this->_item[$offset] = $value;
|
||||
}
|
||||
/**
|
||||
* @param mixed $offset
|
||||
* @param mixed $value
|
||||
*/
|
||||
public function offsetSet(mixed $offset, mixed $value)
|
||||
{
|
||||
$this->_item[$offset] = $value;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param mixed $offset
|
||||
*/
|
||||
public function offsetUnset(mixed $offset)
|
||||
{
|
||||
if ($this->offsetExists($offset)) {
|
||||
unset($this->_item[$offset]);
|
||||
}
|
||||
}
|
||||
/**
|
||||
* @param mixed $offset
|
||||
*/
|
||||
public function offsetUnset(mixed $offset)
|
||||
{
|
||||
if ($this->offsetExists($offset)) {
|
||||
unset($this->_item[$offset]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -56,7 +56,7 @@ class CollectionIterator extends \ArrayIterator
|
||||
*/
|
||||
protected function newModel($current): ActiveRecord
|
||||
{
|
||||
return $this->model::populate($current);
|
||||
return $this->model->setAttributes($current);
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user