2020-08-31 12:38:32 +08:00
|
|
|
<?php
|
|
|
|
|
/**
|
|
|
|
|
* Created by PhpStorm.
|
|
|
|
|
* User: whwyy
|
|
|
|
|
* Date: 2018/3/30 0030
|
|
|
|
|
* Time: 14:39
|
|
|
|
|
*/
|
2020-10-29 18:17:25 +08:00
|
|
|
declare(strict_types=1);
|
2020-10-30 01:52:20 +08:00
|
|
|
|
2020-08-31 12:38:32 +08:00
|
|
|
namespace Database\Base;
|
|
|
|
|
|
|
|
|
|
|
2021-02-22 17:44:24 +08:00
|
|
|
use Annotation\Event;
|
|
|
|
|
use Annotation\Inject;
|
2021-01-22 16:52:48 +08:00
|
|
|
use ArrayAccess;
|
2021-02-25 16:43:40 +08:00
|
|
|
use Database\SqlBuilder;
|
2020-11-23 14:20:23 +08:00
|
|
|
use HttpServer\Http\Context;
|
2021-02-26 10:37:12 +08:00
|
|
|
use JetBrains\PhpStorm\Pure;
|
2020-12-17 14:09:14 +08:00
|
|
|
use ReflectionException;
|
2020-08-31 12:38:32 +08:00
|
|
|
use Snowflake\Abstracts\Component;
|
|
|
|
|
use Database\ActiveQuery;
|
|
|
|
|
use Database\ActiveRecord;
|
|
|
|
|
use Database\Connection;
|
|
|
|
|
use Database\HasMany;
|
|
|
|
|
use Database\HasOne;
|
|
|
|
|
use Database\Mysql\Columns;
|
|
|
|
|
use Database\Relation;
|
|
|
|
|
use Exception;
|
2020-09-02 11:38:47 +08:00
|
|
|
use Snowflake\Exception\ComponentException;
|
2020-12-17 14:09:14 +08:00
|
|
|
use Snowflake\Exception\NotFindClassException;
|
2020-08-31 12:38:32 +08:00
|
|
|
use validator\Validator;
|
|
|
|
|
use Database\IOrm;
|
|
|
|
|
use Snowflake\Snowflake;
|
2021-02-22 17:44:24 +08:00
|
|
|
use Snowflake\Event as SEvent;
|
2020-08-31 12:38:32 +08:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Class BOrm
|
|
|
|
|
*
|
|
|
|
|
* @package Snowflake\Abstracts
|
|
|
|
|
*
|
|
|
|
|
* @property bool $isCreate
|
|
|
|
|
* @method rules()
|
|
|
|
|
* @method static tableName()
|
|
|
|
|
*/
|
2021-01-22 16:52:48 +08:00
|
|
|
abstract class BaseActiveRecord extends Component implements IOrm, ArrayAccess
|
2020-08-31 12:38:32 +08:00
|
|
|
{
|
|
|
|
|
|
2021-02-22 17:44:24 +08:00
|
|
|
const AFTER_SAVE = 'after::save';
|
|
|
|
|
const BEFORE_SAVE = 'before::save';
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#[Inject(SEvent::class)]
|
|
|
|
|
protected ?SEvent $event;
|
|
|
|
|
|
|
|
|
|
|
2020-08-31 12:38:32 +08:00
|
|
|
/** @var array */
|
2020-10-29 18:17:25 +08:00
|
|
|
protected array $_attributes = [];
|
2020-08-31 12:38:32 +08:00
|
|
|
|
|
|
|
|
/** @var array */
|
2020-10-29 18:17:25 +08:00
|
|
|
protected array $_oldAttributes = [];
|
2020-08-31 12:38:32 +08:00
|
|
|
|
|
|
|
|
/** @var array */
|
2020-10-29 18:17:25 +08:00
|
|
|
protected array $_relate = [];
|
2020-08-31 12:38:32 +08:00
|
|
|
|
|
|
|
|
/** @var null|string */
|
2020-10-29 18:17:25 +08:00
|
|
|
protected ?string $primary = NULL;
|
2020-08-31 12:38:32 +08:00
|
|
|
|
2021-01-20 15:45:53 +08:00
|
|
|
|
|
|
|
|
private array $_annotations = [];
|
|
|
|
|
|
2021-02-26 10:37:12 +08:00
|
|
|
|
|
|
|
|
private array $_fields = [];
|
|
|
|
|
|
2020-08-31 12:38:32 +08:00
|
|
|
/**
|
|
|
|
|
* @var bool
|
|
|
|
|
*/
|
2020-10-29 18:17:25 +08:00
|
|
|
protected bool $isNewExample = TRUE;
|
2020-08-31 12:38:32 +08:00
|
|
|
|
2020-10-29 18:17:25 +08:00
|
|
|
protected array $actions = [];
|
2020-08-31 12:38:32 +08:00
|
|
|
|
2020-11-23 14:20:23 +08:00
|
|
|
protected ?Relation $_relation;
|
2020-08-31 12:38:32 +08:00
|
|
|
|
2021-01-20 15:45:53 +08:00
|
|
|
|
2021-02-22 17:51:03 +08:00
|
|
|
/**
|
|
|
|
|
* @param SEvent $event
|
|
|
|
|
* 默认注入
|
|
|
|
|
*/
|
|
|
|
|
public function setEvent(SEvent $event)
|
|
|
|
|
{
|
|
|
|
|
$this->event = $event;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2021-01-20 15:45:53 +08:00
|
|
|
/**
|
2021-02-22 17:44:24 +08:00
|
|
|
* object init
|
2021-01-20 15:45:53 +08:00
|
|
|
*/
|
2021-02-22 17:44:24 +08:00
|
|
|
public function clean()
|
2021-01-20 15:45:53 +08:00
|
|
|
{
|
2021-02-22 17:44:24 +08:00
|
|
|
$this->_attributes = [];
|
|
|
|
|
$this->_oldAttributes = [];
|
2021-01-20 15:45:53 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2020-08-31 12:38:32 +08:00
|
|
|
/**
|
|
|
|
|
* @throws Exception
|
|
|
|
|
*/
|
2021-02-22 17:44:24 +08:00
|
|
|
public function init()
|
2020-08-31 12:38:32 +08:00
|
|
|
{
|
2021-02-22 17:44:24 +08:00
|
|
|
if (!Context::hasContext(Relation::class)) {
|
|
|
|
|
$relation = Snowflake::createObject(Relation::class);
|
|
|
|
|
$this->_relation = Context::setContext(Relation::class, $relation);
|
|
|
|
|
} else {
|
|
|
|
|
$this->_relation = Context::getContext(Relation::class);
|
|
|
|
|
}
|
2021-02-26 10:37:12 +08:00
|
|
|
|
|
|
|
|
$this->_fields = static::getColumns()->format();
|
|
|
|
|
|
2021-02-23 16:01:55 +08:00
|
|
|
$this->createAnnotation();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @throws ComponentException
|
|
|
|
|
*/
|
|
|
|
|
private function createAnnotation()
|
|
|
|
|
{
|
|
|
|
|
$annotation = Snowflake::app()->getAttributes();
|
|
|
|
|
|
2021-02-26 10:37:12 +08:00
|
|
|
$this->_annotations = $annotation->getMethods(get_called_class());
|
2021-02-23 16:20:19 +08:00
|
|
|
|
2021-02-26 10:37:12 +08:00
|
|
|
$annotation->setProperty($this);
|
2020-08-31 12:38:32 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @return array
|
|
|
|
|
*/
|
2020-12-17 14:58:54 +08:00
|
|
|
public function getActions(): array
|
2020-08-31 12:38:32 +08:00
|
|
|
{
|
|
|
|
|
return $this->actions;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @return bool
|
|
|
|
|
*/
|
2020-12-17 14:58:54 +08:00
|
|
|
public function getIsCreate(): bool
|
2020-08-31 12:38:32 +08:00
|
|
|
{
|
|
|
|
|
return $this->isNewExample === TRUE;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param bool $bool
|
|
|
|
|
* @return $this
|
|
|
|
|
*/
|
2020-12-17 14:58:54 +08:00
|
|
|
public function setIsCreate($bool = FALSE): static
|
2020-08-31 12:38:32 +08:00
|
|
|
{
|
|
|
|
|
$this->isNewExample = $bool;
|
|
|
|
|
return $this;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @return mixed
|
|
|
|
|
*
|
|
|
|
|
* get last exception or other error
|
2020-09-02 11:38:47 +08:00
|
|
|
* @throws ComponentException
|
2020-08-31 12:38:32 +08:00
|
|
|
*/
|
2020-12-17 14:58:54 +08:00
|
|
|
public function getLastError(): mixed
|
2020-08-31 12:38:32 +08:00
|
|
|
{
|
2020-09-03 11:39:20 +08:00
|
|
|
return Snowflake::app()->getLogger()->getLastError('mysql');
|
2020-08-31 12:38:32 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @return bool
|
|
|
|
|
* @throws Exception
|
|
|
|
|
*/
|
2020-12-17 14:58:54 +08:00
|
|
|
public function hasPrimary(): bool
|
2020-08-31 12:38:32 +08:00
|
|
|
{
|
2020-09-11 17:58:02 +08:00
|
|
|
if ($this->primary !== NULL) {
|
2020-08-31 12:38:32 +08:00
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
$primary = static::getColumns()->getPrimaryKeys();
|
|
|
|
|
if (!empty($primary)) {
|
2020-09-16 21:27:23 +08:00
|
|
|
return $this->primary = is_array($primary) ? current($primary) : $primary;
|
2020-08-31 12:38:32 +08:00
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @throws Exception
|
|
|
|
|
*/
|
2021-02-25 17:25:30 +08:00
|
|
|
public function isAutoIncrement(): bool
|
2020-08-31 12:38:32 +08:00
|
|
|
{
|
2021-02-25 17:25:30 +08:00
|
|
|
return $this->getAutoIncrement() !== null;
|
2020-08-31 12:38:32 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @throws Exception
|
|
|
|
|
*/
|
2020-12-17 14:58:54 +08:00
|
|
|
public function getAutoIncrement(): int|string|null
|
2020-08-31 12:38:32 +08:00
|
|
|
{
|
|
|
|
|
return static::getColumns()->getAutoIncrement();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @return null|string
|
|
|
|
|
* @throws Exception
|
|
|
|
|
*/
|
2020-12-17 14:17:03 +08:00
|
|
|
public function getPrimary(): ?string
|
2020-08-31 12:38:32 +08:00
|
|
|
{
|
2020-09-11 17:58:02 +08:00
|
|
|
if (!$this->hasPrimary()) {
|
2020-08-31 12:38:32 +08:00
|
|
|
return null;
|
|
|
|
|
}
|
2020-09-11 17:58:02 +08:00
|
|
|
return $this->primary;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2020-12-17 14:19:03 +08:00
|
|
|
* @return int|null
|
2020-09-11 17:58:02 +08:00
|
|
|
* @throws Exception
|
|
|
|
|
*/
|
2020-12-17 14:19:03 +08:00
|
|
|
public function getPrimaryValue(): ?int
|
2020-09-11 17:58:02 +08:00
|
|
|
{
|
|
|
|
|
if (!$this->hasPrimary()) {
|
|
|
|
|
return null;
|
2020-08-31 12:38:32 +08:00
|
|
|
}
|
2020-09-11 17:58:02 +08:00
|
|
|
return $this->getAttribute($this->primary);
|
2020-08-31 12:38:32 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2020-12-17 14:09:14 +08:00
|
|
|
* @param $param
|
2021-01-12 18:39:53 +08:00
|
|
|
* @param null $db
|
2021-02-22 17:44:24 +08:00
|
|
|
* @return BaseActiveRecord|null
|
2021-01-12 18:39:53 +08:00
|
|
|
* @throws NotFindClassException
|
|
|
|
|
* @throws ReflectionException
|
|
|
|
|
* @throws Exception
|
2020-08-31 12:38:32 +08:00
|
|
|
*/
|
2021-02-22 10:08:29 +08:00
|
|
|
public static function findOne($param, $db = NULL): static|null
|
2020-08-31 12:38:32 +08:00
|
|
|
{
|
2021-02-22 10:08:29 +08:00
|
|
|
if (is_bool($param)) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
2020-12-17 14:09:14 +08:00
|
|
|
if (is_numeric($param)) {
|
2021-02-22 17:44:24 +08:00
|
|
|
$param = static::getPrimaryCondition($param);
|
2020-08-31 12:38:32 +08:00
|
|
|
}
|
2020-12-17 14:09:14 +08:00
|
|
|
return static::find()->where($param)->first();
|
2020-08-31 12:38:32 +08:00
|
|
|
}
|
|
|
|
|
|
2021-02-22 17:44:24 +08:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param $param
|
|
|
|
|
* @return array
|
|
|
|
|
* @throws Exception
|
|
|
|
|
*/
|
|
|
|
|
private static function getPrimaryCondition($param): array
|
|
|
|
|
{
|
|
|
|
|
$primary = static::getColumns()->getPrimaryKeys();
|
|
|
|
|
if (empty($primary)) {
|
|
|
|
|
throw new Exception('Primary key cannot be empty.');
|
|
|
|
|
}
|
|
|
|
|
if (is_array($primary)) {
|
|
|
|
|
$primary = current($primary);
|
|
|
|
|
}
|
|
|
|
|
return [$primary => $param];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2020-08-31 12:38:32 +08:00
|
|
|
/**
|
|
|
|
|
* @param null $field
|
|
|
|
|
* @return ActiveRecord|null
|
|
|
|
|
* @throws Exception
|
|
|
|
|
* @throws Exception
|
|
|
|
|
*/
|
2020-12-17 14:58:54 +08:00
|
|
|
public static function max($field = null): ?ActiveRecord
|
2020-08-31 12:38:32 +08:00
|
|
|
{
|
2020-09-11 17:58:02 +08:00
|
|
|
$columns = static::getColumns();
|
2020-08-31 12:38:32 +08:00
|
|
|
if (empty($field)) {
|
2020-09-11 17:58:02 +08:00
|
|
|
$field = $columns->getFirstPrimary();
|
2020-08-31 12:38:32 +08:00
|
|
|
}
|
2020-09-11 17:58:02 +08:00
|
|
|
$columns = $columns->get_fields();
|
2020-08-31 12:38:32 +08:00
|
|
|
if (!isset($columns[$field])) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
$first = static::find()->max($field)->first();
|
|
|
|
|
if (empty($first)) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
return $first[$field];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2020-12-17 14:09:14 +08:00
|
|
|
* @return ActiveQuery
|
|
|
|
|
* @throws ReflectionException
|
|
|
|
|
* @throws NotFindClassException
|
2020-08-31 12:38:32 +08:00
|
|
|
*/
|
2020-12-17 14:58:54 +08:00
|
|
|
public static function find(): ActiveQuery
|
2020-08-31 12:38:32 +08:00
|
|
|
{
|
|
|
|
|
return Snowflake::createObject(ActiveQuery::class, [get_called_class()]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param null $condition
|
|
|
|
|
* @param array $attributes
|
|
|
|
|
*
|
|
|
|
|
* @param bool $if_condition_is_null
|
|
|
|
|
* @return bool
|
|
|
|
|
* @throws Exception
|
|
|
|
|
*/
|
2020-12-17 14:17:03 +08:00
|
|
|
public static function deleteByCondition($condition = NULL, $attributes = [], $if_condition_is_null = false): bool
|
2020-08-31 12:38:32 +08:00
|
|
|
{
|
|
|
|
|
if (empty($condition)) {
|
|
|
|
|
if (!$if_condition_is_null) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2020-12-17 14:09:14 +08:00
|
|
|
return static::find()->delete();
|
2020-08-31 12:38:32 +08:00
|
|
|
}
|
|
|
|
|
$model = static::find()->ifNotWhere($if_condition_is_null)->where($condition);
|
|
|
|
|
if (!empty($attributes)) {
|
|
|
|
|
$model->bindParams($attributes);
|
|
|
|
|
}
|
2020-12-17 14:09:14 +08:00
|
|
|
return $model->delete();
|
2020-08-31 12:38:32 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @return array
|
|
|
|
|
*/
|
2020-12-17 14:58:54 +08:00
|
|
|
public function getAttributes(): array
|
2020-08-31 12:38:32 +08:00
|
|
|
{
|
|
|
|
|
return $this->_attributes;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @return array
|
|
|
|
|
*/
|
2020-12-17 14:58:54 +08:00
|
|
|
public function getOldAttributes(): array
|
2020-08-31 12:38:32 +08:00
|
|
|
{
|
|
|
|
|
return $this->_oldAttributes;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param $name
|
|
|
|
|
* @param $value
|
|
|
|
|
* @return mixed
|
|
|
|
|
*/
|
2020-12-17 14:58:54 +08:00
|
|
|
public function setAttribute($name, $value): mixed
|
2020-08-31 12:38:32 +08:00
|
|
|
{
|
|
|
|
|
return $this->_attributes[$name] = $value;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param $name
|
|
|
|
|
* @param $value
|
|
|
|
|
* @return mixed
|
|
|
|
|
*/
|
2020-12-17 14:58:54 +08:00
|
|
|
public function setOldAttribute($name, $value): mixed
|
2020-08-31 12:38:32 +08:00
|
|
|
{
|
|
|
|
|
return $this->_oldAttributes[$name] = $value;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param array $param
|
|
|
|
|
* @return $this
|
2020-09-11 17:58:02 +08:00
|
|
|
* @throws Exception
|
2020-08-31 12:38:32 +08:00
|
|
|
*/
|
2020-12-17 14:58:54 +08:00
|
|
|
public function setAttributes(array $param): static
|
2020-08-31 12:38:32 +08:00
|
|
|
{
|
2020-09-14 14:18:11 +08:00
|
|
|
if (empty($param)) {
|
2020-08-31 12:38:32 +08:00
|
|
|
return $this;
|
|
|
|
|
}
|
|
|
|
|
foreach ($param as $key => $val) {
|
|
|
|
|
if (!$this->has($key)) {
|
|
|
|
|
$this->setAttribute($key, $val);
|
|
|
|
|
} else {
|
|
|
|
|
$this->$key = $val;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return $this;
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-11 17:58:02 +08:00
|
|
|
/**
|
|
|
|
|
* @param $param
|
|
|
|
|
* @return $this
|
|
|
|
|
*/
|
2020-12-17 14:58:54 +08:00
|
|
|
public function setOldAttributes($param): static
|
2020-08-31 12:38:32 +08:00
|
|
|
{
|
|
|
|
|
if (empty($param) || !is_array($param)) {
|
|
|
|
|
return $this;
|
|
|
|
|
}
|
|
|
|
|
foreach ($param as $key => $val) {
|
|
|
|
|
$this->setOldAttribute($key, $val);
|
|
|
|
|
}
|
|
|
|
|
return $this;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param $attributes
|
|
|
|
|
* @param $param
|
|
|
|
|
* @return $this|bool
|
|
|
|
|
* @throws Exception
|
|
|
|
|
*/
|
2020-12-17 14:58:54 +08:00
|
|
|
private function insert($param, $attributes): bool|static
|
2020-08-31 12:38:32 +08:00
|
|
|
{
|
|
|
|
|
if (empty($param)) {
|
|
|
|
|
return FALSE;
|
|
|
|
|
}
|
|
|
|
|
$dbConnection = static::getDb();
|
2021-02-25 16:43:40 +08:00
|
|
|
|
|
|
|
|
[$sql, $param] = SqlBuilder::builder(static::find())->insert($param);
|
2020-08-31 12:38:32 +08:00
|
|
|
|
|
|
|
|
$trance = $dbConnection->beginTransaction();
|
|
|
|
|
try {
|
2021-02-25 16:43:40 +08:00
|
|
|
if (!($lastId = (int)$dbConnection->createCommand($sql, $param)->save(true, $this))) {
|
|
|
|
|
throw new Exception('保存失败.' . $sql);
|
2020-08-31 12:38:32 +08:00
|
|
|
}
|
|
|
|
|
$trance->commit();
|
2021-02-23 18:13:17 +08:00
|
|
|
$lastId = $this->setPrimary($lastId, $param);
|
2021-02-22 17:44:24 +08:00
|
|
|
|
|
|
|
|
$this->event->dispatch(self::AFTER_SAVE, [$attributes, $param]);
|
2020-11-06 16:47:17 +08:00
|
|
|
} catch (\Throwable $exception) {
|
2020-08-31 12:38:32 +08:00
|
|
|
$trance->rollback();
|
2021-02-20 15:45:48 +08:00
|
|
|
$lastId = $this->addError($exception, 'mysql');
|
2020-08-31 12:38:32 +08:00
|
|
|
}
|
|
|
|
|
return $lastId;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2021-02-13 17:05:11 +08:00
|
|
|
/**
|
|
|
|
|
* @param $lastId
|
|
|
|
|
* @param $param
|
2021-02-23 18:21:07 +08:00
|
|
|
* @return static
|
2021-02-13 17:05:11 +08:00
|
|
|
* @throws Exception
|
|
|
|
|
*/
|
2021-02-23 18:21:07 +08:00
|
|
|
private function setPrimary($lastId, $param): static
|
2021-02-13 17:05:11 +08:00
|
|
|
{
|
2021-02-25 17:25:30 +08:00
|
|
|
if ($this->isAutoIncrement()) {
|
2021-02-23 18:21:07 +08:00
|
|
|
$this->setAttribute($this->getAutoIncrement(), (int)$lastId);
|
|
|
|
|
return $this;
|
2021-02-13 17:05:11 +08:00
|
|
|
}
|
2021-02-23 18:21:07 +08:00
|
|
|
|
2021-02-13 17:05:43 +08:00
|
|
|
if (!$this->hasPrimary()) {
|
|
|
|
|
return $this;
|
|
|
|
|
}
|
2021-02-23 18:21:07 +08:00
|
|
|
|
2021-02-13 17:05:43 +08:00
|
|
|
$primary = $this->getPrimary();
|
|
|
|
|
if (!isset($param[$primary]) || empty($param[$primary])) {
|
|
|
|
|
$this->setAttribute($primary, (int)$lastId);
|
2021-02-13 17:05:11 +08:00
|
|
|
}
|
|
|
|
|
return $this->setAttributes($param);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2020-08-31 12:38:32 +08:00
|
|
|
/**
|
2021-02-25 16:43:40 +08:00
|
|
|
* @param $fields
|
2020-08-31 12:38:32 +08:00
|
|
|
* @param $condition
|
2021-02-25 16:43:40 +08:00
|
|
|
* @param $param
|
2020-08-31 12:38:32 +08:00
|
|
|
* @return $this|bool
|
|
|
|
|
* @throws Exception
|
|
|
|
|
*/
|
2021-02-25 18:26:15 +08:00
|
|
|
private function updateInternal($fields, $condition, $param): bool|static
|
2020-08-31 12:38:32 +08:00
|
|
|
{
|
|
|
|
|
if (empty($param)) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
$command = static::getDb();
|
2021-02-25 16:43:40 +08:00
|
|
|
|
|
|
|
|
if ($this->hasPrimary()) {
|
|
|
|
|
$condition = [$this->getPrimary() => $this->getPrimaryValue()];
|
|
|
|
|
}
|
|
|
|
|
|
2021-02-25 18:08:19 +08:00
|
|
|
$generate = SqlBuilder::builder(static::find()->where($condition))->update($param);
|
|
|
|
|
if (is_bool($generate)) {
|
|
|
|
|
return $generate;
|
|
|
|
|
}
|
2020-08-31 12:38:32 +08:00
|
|
|
|
|
|
|
|
$trance = $command->beginTransaction();
|
2021-02-25 18:08:19 +08:00
|
|
|
if (!$command->createCommand(...$generate)->save(false, $this)) {
|
2020-08-31 12:38:32 +08:00
|
|
|
$trance->rollback();
|
2021-02-25 16:43:40 +08:00
|
|
|
return false;
|
|
|
|
|
}
|
2021-02-22 17:44:24 +08:00
|
|
|
|
2021-02-25 16:43:40 +08:00
|
|
|
$trance->commit();
|
2021-02-23 18:02:17 +08:00
|
|
|
|
2021-02-25 16:43:40 +08:00
|
|
|
$this->event->dispatch(self::AFTER_SAVE, [$fields, $param]);
|
|
|
|
|
|
|
|
|
|
return true;
|
2020-08-31 12:38:32 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2021-02-25 18:13:48 +08:00
|
|
|
* @param null $data
|
2020-12-17 15:19:59 +08:00
|
|
|
* @return bool|$this
|
2020-08-31 12:38:32 +08:00
|
|
|
* @throws Exception
|
|
|
|
|
*/
|
2021-02-25 18:13:48 +08:00
|
|
|
public function save($data = NULL): static|bool
|
2020-08-31 12:38:32 +08:00
|
|
|
{
|
2021-02-25 16:43:40 +08:00
|
|
|
if (!is_null($data)) {
|
2021-02-25 18:15:54 +08:00
|
|
|
$this->_attributes = merge($this->_attributes, $data);
|
2020-09-14 14:21:34 +08:00
|
|
|
}
|
2021-02-25 16:43:40 +08:00
|
|
|
|
2021-02-22 17:44:24 +08:00
|
|
|
if (!$this->validator($this->rules())) {
|
2020-08-31 12:38:32 +08:00
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2021-02-23 18:31:30 +08:00
|
|
|
if (!$this->event->dispatch(self::BEFORE_SAVE, [$this], $this)) {
|
2021-02-23 18:22:52 +08:00
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-31 12:38:32 +08:00
|
|
|
static::getDb()->enablingTransactions();
|
2021-02-25 16:43:40 +08:00
|
|
|
[$change, $condition, $fields] = $this->filtration_and_separation();
|
2020-08-31 12:38:32 +08:00
|
|
|
|
2021-02-23 18:02:17 +08:00
|
|
|
if (!$this->isNewExample) {
|
2021-02-25 18:26:15 +08:00
|
|
|
return $this->updateInternal($fields, $condition, $change);
|
2020-08-31 12:38:32 +08:00
|
|
|
}
|
2021-02-25 16:43:40 +08:00
|
|
|
return $this->insert($change, $fields);
|
2020-08-31 12:38:32 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param array $rule
|
|
|
|
|
* @return bool
|
|
|
|
|
* @throws Exception
|
|
|
|
|
*/
|
2020-12-17 14:58:54 +08:00
|
|
|
public function validator(array $rule): bool
|
2020-08-31 12:38:32 +08:00
|
|
|
{
|
|
|
|
|
if (empty($rule)) return true;
|
|
|
|
|
$validate = $this->resolve($rule);
|
|
|
|
|
if (!$validate->validation()) {
|
|
|
|
|
return $this->addError($validate->getError(), 'mysql');
|
|
|
|
|
} else {
|
|
|
|
|
return TRUE;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param $rule
|
|
|
|
|
* @return Validator
|
|
|
|
|
* @throws Exception
|
|
|
|
|
*/
|
2020-12-17 14:58:54 +08:00
|
|
|
private function resolve($rule): Validator
|
2020-08-31 12:38:32 +08:00
|
|
|
{
|
|
|
|
|
$validate = Validator::getInstance();
|
|
|
|
|
$validate->setParams($this->_attributes);
|
|
|
|
|
$validate->setModel($this);
|
|
|
|
|
foreach ($rule as $Key => $val) {
|
|
|
|
|
$field = array_shift($val);
|
|
|
|
|
if (empty($val)) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
$validate->make($field, $val);
|
|
|
|
|
}
|
|
|
|
|
return $validate;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param string $name
|
|
|
|
|
* @return null
|
|
|
|
|
* @throws Exception
|
|
|
|
|
*/
|
|
|
|
|
public function getAttribute(string $name)
|
|
|
|
|
{
|
|
|
|
|
$method = 'get' . ucfirst($name) . 'Attribute';
|
2021-01-19 17:56:46 +08:00
|
|
|
|
2020-08-31 12:38:32 +08:00
|
|
|
if (method_exists($this, $method)) {
|
|
|
|
|
return $this->$method($this->_attributes[$name]);
|
|
|
|
|
}
|
|
|
|
|
return $this->_attributes[$name] ?? null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @return array
|
|
|
|
|
* @throws Exception
|
|
|
|
|
*/
|
2020-12-17 14:58:54 +08:00
|
|
|
private function filtration_and_separation(): array
|
2020-08-31 12:38:32 +08:00
|
|
|
{
|
|
|
|
|
$_tmp = [];
|
|
|
|
|
$condition = [];
|
|
|
|
|
$columns = static::getColumns();
|
2021-02-26 10:37:12 +08:00
|
|
|
|
|
|
|
|
$format = $columns->getAllField();
|
|
|
|
|
|
2020-08-31 12:38:32 +08:00
|
|
|
foreach ($this->_attributes as $key => $val) {
|
|
|
|
|
$oldValue = $this->_oldAttributes[$key] ?? null;
|
|
|
|
|
if ($val !== $oldValue) {
|
2021-02-26 10:37:12 +08:00
|
|
|
$_tmp[$key] = $columns->encode($val, $columns->clean($format[$key]));
|
2020-08-31 12:38:32 +08:00
|
|
|
} else {
|
|
|
|
|
$condition[$key] = $val;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return [$_tmp, $condition, array_keys($_tmp)];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param $name
|
|
|
|
|
* @param $value
|
|
|
|
|
*/
|
|
|
|
|
public function setRelate($name, $value)
|
|
|
|
|
{
|
|
|
|
|
$this->_relate[$name] = $value;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param array $relates
|
|
|
|
|
*/
|
|
|
|
|
public function setRelates(array $relates)
|
|
|
|
|
{
|
|
|
|
|
if (empty($relates)) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
foreach ($relates as $key => $val) {
|
|
|
|
|
$this->setRelate($key, $val);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @return array
|
|
|
|
|
*/
|
2020-12-17 14:58:54 +08:00
|
|
|
public function getRelates(): array
|
2020-08-31 12:38:32 +08:00
|
|
|
{
|
|
|
|
|
return $this->_relate;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2021-02-22 17:44:24 +08:00
|
|
|
* @return Relation|null
|
2020-08-31 12:38:32 +08:00
|
|
|
*/
|
2021-02-22 17:44:24 +08:00
|
|
|
public function getRelation(): ?Relation
|
2020-08-31 12:38:32 +08:00
|
|
|
{
|
|
|
|
|
return $this->_relation;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param $name
|
2020-12-17 14:58:54 +08:00
|
|
|
* @return mixed
|
2020-08-31 12:38:32 +08:00
|
|
|
*/
|
2020-12-17 14:58:54 +08:00
|
|
|
public function getRelate($name): mixed
|
2020-08-31 12:38:32 +08:00
|
|
|
{
|
|
|
|
|
if (!isset($this->_relate[$name])) {
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
return $this->_relate[$name];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param $attribute
|
|
|
|
|
* @return bool
|
|
|
|
|
* @throws Exception
|
|
|
|
|
*/
|
2021-02-26 10:37:12 +08:00
|
|
|
#[Pure] public function has($attribute): bool
|
2020-08-31 12:38:32 +08:00
|
|
|
{
|
2021-02-26 10:37:12 +08:00
|
|
|
return array_key_exists($attribute, $this->_fields);
|
2020-08-31 12:38:32 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**ƒ
|
|
|
|
|
* @return string
|
|
|
|
|
* @throws Exception
|
|
|
|
|
*/
|
2020-12-17 14:58:54 +08:00
|
|
|
public static function getTable(): string
|
2020-08-31 12:38:32 +08:00
|
|
|
{
|
|
|
|
|
$tablePrefix = static::getDb()->tablePrefix;
|
|
|
|
|
|
|
|
|
|
$table = static::tableName();
|
|
|
|
|
|
2020-12-17 14:58:54 +08:00
|
|
|
if (str_starts_with($table, $tablePrefix)) {
|
2020-08-31 12:38:32 +08:00
|
|
|
return $table;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (empty($table)) {
|
2021-02-22 17:44:24 +08:00
|
|
|
$class = preg_replace('/model\\\\/', '', get_called_class());
|
2020-08-31 12:38:32 +08:00
|
|
|
$table = lcfirst($class);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$table = trim($table, '{{%}}');
|
|
|
|
|
if ($tablePrefix) {
|
|
|
|
|
$table = $tablePrefix . $table;
|
|
|
|
|
}
|
|
|
|
|
return $table;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param $attributes
|
|
|
|
|
* @param $changeAttributes
|
2021-02-22 17:44:24 +08:00
|
|
|
* @return bool
|
2020-08-31 12:38:32 +08:00
|
|
|
* @throws Exception
|
|
|
|
|
*/
|
2021-02-22 17:49:10 +08:00
|
|
|
#[Event(ActiveRecord::AFTER_SAVE)]
|
2021-02-22 17:44:24 +08:00
|
|
|
public function afterSave($attributes, $changeAttributes): bool
|
2020-08-31 12:38:32 +08:00
|
|
|
{
|
2021-02-22 17:44:24 +08:00
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param $model
|
|
|
|
|
* @return bool
|
|
|
|
|
*/
|
2021-02-22 17:49:10 +08:00
|
|
|
#[Event(ActiveRecord::BEFORE_SAVE)]
|
2021-02-22 17:44:24 +08:00
|
|
|
public function beforeSave($model): bool
|
|
|
|
|
{
|
|
|
|
|
return true;
|
2020-08-31 12:38:32 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @return Connection
|
|
|
|
|
* @throws Exception
|
|
|
|
|
*/
|
2020-12-17 14:58:54 +08:00
|
|
|
public static function getDb(): Connection
|
2020-08-31 12:38:32 +08:00
|
|
|
{
|
|
|
|
|
return static::setDatabaseConnect('db');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @return static
|
|
|
|
|
*/
|
2020-12-17 14:58:54 +08:00
|
|
|
public function refresh(): static
|
2020-08-31 12:38:32 +08:00
|
|
|
{
|
|
|
|
|
$this->_oldAttributes = $this->_attributes;
|
|
|
|
|
return $this;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param $name
|
|
|
|
|
* @param $value
|
|
|
|
|
* @throws Exception
|
|
|
|
|
*/
|
|
|
|
|
public function __set($name, $value)
|
|
|
|
|
{
|
|
|
|
|
if (!$this->has($name)) {
|
|
|
|
|
parent::__set($name, $value);
|
|
|
|
|
} else {
|
|
|
|
|
$sets = 'set' . ucfirst($name) . 'Attribute';
|
|
|
|
|
if (method_exists($this, $sets)) {
|
|
|
|
|
$value = $this->$sets($value);
|
|
|
|
|
}
|
|
|
|
|
$this->_attributes[$name] = $value;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param $name
|
2020-12-17 14:09:14 +08:00
|
|
|
* @return mixed
|
2020-08-31 12:38:32 +08:00
|
|
|
* @throws Exception
|
|
|
|
|
*/
|
2020-12-17 14:09:14 +08:00
|
|
|
public function __get($name): mixed
|
2020-08-31 12:38:32 +08:00
|
|
|
{
|
2021-01-20 15:45:53 +08:00
|
|
|
$value = $this->_attributes[$name] ?? null;
|
|
|
|
|
if ($this->hasAnnotation($name)) {
|
|
|
|
|
return call_user_func($this->_annotations[$name], $value);
|
2020-08-31 12:38:32 +08:00
|
|
|
}
|
2021-01-20 15:45:53 +08:00
|
|
|
if (array_key_exists($name, $this->_attributes)) {
|
|
|
|
|
return static::getColumns()->_decode($name, $value);
|
2020-08-31 12:38:32 +08:00
|
|
|
}
|
|
|
|
|
if (isset($this->_relate[$name])) {
|
|
|
|
|
$gets = $this->{$this->_relate[$name]}();
|
|
|
|
|
}
|
|
|
|
|
if (isset($gets)) {
|
|
|
|
|
return $this->resolveClass($gets);
|
|
|
|
|
}
|
|
|
|
|
return parent::__get($name);
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-20 15:45:53 +08:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @return array
|
|
|
|
|
*/
|
|
|
|
|
protected function getAnnotation(): array
|
|
|
|
|
{
|
|
|
|
|
return $this->_annotations;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param $name
|
|
|
|
|
* @return bool
|
|
|
|
|
*/
|
|
|
|
|
protected function hasAnnotation($name): bool
|
|
|
|
|
{
|
|
|
|
|
return isset($this->_annotations[$name]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param $item
|
|
|
|
|
* @param $data
|
|
|
|
|
* @return array
|
|
|
|
|
*/
|
|
|
|
|
protected function resolveAttributes($item, $data): array
|
|
|
|
|
{
|
|
|
|
|
return call_user_func($item, $data);
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-31 12:38:32 +08:00
|
|
|
/**
|
|
|
|
|
* @param $name
|
2020-12-17 14:58:54 +08:00
|
|
|
* @return bool
|
2020-08-31 12:38:32 +08:00
|
|
|
*/
|
2020-12-17 14:58:54 +08:00
|
|
|
public function __isset($name): bool
|
2020-08-31 12:38:32 +08:00
|
|
|
{
|
2020-12-17 14:58:54 +08:00
|
|
|
return isset($this->_attributes[$name]);
|
2020-08-31 12:38:32 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param $call
|
2020-12-17 18:18:41 +08:00
|
|
|
* @return mixed
|
2020-08-31 12:38:32 +08:00
|
|
|
* @throws Exception
|
|
|
|
|
*/
|
2020-12-17 18:18:41 +08:00
|
|
|
private function resolveClass($call): mixed
|
2020-08-31 12:38:32 +08:00
|
|
|
{
|
|
|
|
|
if ($call instanceof HasOne) {
|
|
|
|
|
return $call->get();
|
|
|
|
|
} else if ($call instanceof HasMany) {
|
|
|
|
|
return $call->get();
|
|
|
|
|
} else {
|
|
|
|
|
return $call;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param mixed $offset
|
|
|
|
|
* @return bool
|
|
|
|
|
* @throws Exception
|
|
|
|
|
*/
|
2020-12-17 14:58:54 +08:00
|
|
|
public function offsetExists(mixed $offset): bool
|
2020-08-31 12:38:32 +08:00
|
|
|
{
|
|
|
|
|
return $this->has($offset);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param mixed $offset
|
2020-12-17 14:58:54 +08:00
|
|
|
* @return mixed
|
2020-08-31 12:38:32 +08:00
|
|
|
* @throws Exception
|
|
|
|
|
*/
|
2020-12-17 14:58:54 +08:00
|
|
|
public function offsetGet(mixed $offset): mixed
|
2020-08-31 12:38:32 +08:00
|
|
|
{
|
|
|
|
|
return $this->__get($offset);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param mixed $offset
|
|
|
|
|
* @param mixed $value
|
|
|
|
|
* @throws Exception
|
|
|
|
|
*/
|
2020-12-17 14:58:54 +08:00
|
|
|
public function offsetSet(mixed $offset, mixed $value)
|
2020-08-31 12:38:32 +08:00
|
|
|
{
|
2020-12-17 14:58:54 +08:00
|
|
|
$this->__set($offset, $value);
|
2020-08-31 12:38:32 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param mixed $offset
|
|
|
|
|
* @throws Exception
|
|
|
|
|
*/
|
2020-12-17 14:58:54 +08:00
|
|
|
public function offsetUnset(mixed $offset)
|
2020-08-31 12:38:32 +08:00
|
|
|
{
|
|
|
|
|
if (!$this->has($offset)) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
unset($this->_attributes[$offset]);
|
|
|
|
|
unset($this->_oldAttributes[$offset]);
|
|
|
|
|
if (isset($this->_relate)) {
|
|
|
|
|
unset($this->_relate[$offset]);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @return array
|
|
|
|
|
*/
|
2020-12-17 14:58:54 +08:00
|
|
|
public function unset(): array
|
2020-08-31 12:38:32 +08:00
|
|
|
{
|
|
|
|
|
$fields = func_get_args();
|
|
|
|
|
$fields = array_shift($fields);
|
|
|
|
|
if (!is_array($fields)) {
|
|
|
|
|
$fields = explode(',', $fields);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$array = array_combine($fields, $fields);
|
|
|
|
|
|
|
|
|
|
return array_diff_assoc($array, $this->_attributes);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2020-12-17 14:58:54 +08:00
|
|
|
* @param $dbName
|
2020-08-31 12:38:32 +08:00
|
|
|
* @return mixed
|
|
|
|
|
* @throws Exception
|
|
|
|
|
*/
|
2020-12-17 14:58:54 +08:00
|
|
|
public static function setDatabaseConnect($dbName): Connection
|
2020-08-31 12:38:32 +08:00
|
|
|
{
|
2020-12-17 14:58:54 +08:00
|
|
|
return Snowflake::app()->db->get($dbName);
|
2020-08-31 12:38:32 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @return Columns
|
|
|
|
|
* @throws Exception
|
|
|
|
|
*/
|
2020-12-17 14:58:54 +08:00
|
|
|
public static function getColumns(): Columns
|
2020-08-31 12:38:32 +08:00
|
|
|
{
|
|
|
|
|
return static::getDb()->getSchema()
|
|
|
|
|
->getColumns()
|
|
|
|
|
->table(static::getTable());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param array $data
|
|
|
|
|
* @return static
|
|
|
|
|
* @throws
|
|
|
|
|
*/
|
2020-12-17 14:58:54 +08:00
|
|
|
public static function populate(array $data): static
|
2020-08-31 12:38:32 +08:00
|
|
|
{
|
2021-02-24 14:22:56 +08:00
|
|
|
$className = get_called_class();
|
|
|
|
|
|
2021-02-23 18:06:23 +08:00
|
|
|
/** @var static $model */
|
2021-02-24 14:22:56 +08:00
|
|
|
$model = objectPool($className, function () use ($className) {
|
|
|
|
|
return new $className();
|
|
|
|
|
});
|
2021-02-25 18:14:42 +08:00
|
|
|
$model->setAttributes($data);
|
2021-02-25 18:16:45 +08:00
|
|
|
$model->setOldAttributes($data);
|
2020-08-31 12:38:32 +08:00
|
|
|
$model->setIsCreate(false);
|
|
|
|
|
return $model;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|