提交的内容
This commit is contained in:
209
vendor/topthink/think-orm/src/Model.php
vendored
Executable file → Normal file
209
vendor/topthink/think-orm/src/Model.php
vendored
Executable file → Normal file
@ -9,7 +9,7 @@
|
||||
// +----------------------------------------------------------------------
|
||||
// | Author: liu21st <liu21st@gmail.com>
|
||||
// +----------------------------------------------------------------------
|
||||
declare(strict_types=1);
|
||||
declare (strict_types = 1);
|
||||
|
||||
namespace think;
|
||||
|
||||
@ -23,7 +23,7 @@ use think\db\BaseQuery as Query;
|
||||
/**
|
||||
* Class Model.
|
||||
*
|
||||
* @mixin Query
|
||||
* @mixin \think\db\Query
|
||||
*
|
||||
* @method static void onAfterRead(Model $model) after_read事件定义
|
||||
* @method static mixed onBeforeInsert(Model $model) before_insert事件定义
|
||||
@ -43,6 +43,7 @@ abstract class Model implements JsonSerializable, ArrayAccess, Arrayable, Jsonab
|
||||
use model\concern\RelationShip;
|
||||
use model\concern\ModelEvent;
|
||||
use model\concern\TimeStamp;
|
||||
use model\concern\AutoWriteId;
|
||||
use model\concern\Conversion;
|
||||
|
||||
/**
|
||||
@ -115,13 +116,6 @@ abstract class Model implements JsonSerializable, ArrayAccess, Arrayable, Jsonab
|
||||
*/
|
||||
protected static $initialized = [];
|
||||
|
||||
/**
|
||||
* 软删除字段默认值
|
||||
*
|
||||
* @var mixed
|
||||
*/
|
||||
protected $defaultSoftDelete;
|
||||
|
||||
/**
|
||||
* 全局查询范围.
|
||||
*
|
||||
@ -129,6 +123,27 @@ abstract class Model implements JsonSerializable, ArrayAccess, Arrayable, Jsonab
|
||||
*/
|
||||
protected $globalScope = [];
|
||||
|
||||
/**
|
||||
* 数据字段值的变化.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $change = [];
|
||||
|
||||
/**
|
||||
* 数据表延迟写入的字段
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $lazyFields = [];
|
||||
|
||||
/**
|
||||
* 软删除字段默认值
|
||||
*
|
||||
* @var mixed
|
||||
*/
|
||||
protected $defaultSoftDelete;
|
||||
|
||||
/**
|
||||
* Db对象
|
||||
*
|
||||
@ -182,6 +197,7 @@ abstract class Model implements JsonSerializable, ArrayAccess, Arrayable, Jsonab
|
||||
if (!isset(static::$macro[static::class])) {
|
||||
static::$macro[static::class] = [];
|
||||
}
|
||||
|
||||
static::$macro[static::class][$method] = $closure;
|
||||
}
|
||||
|
||||
@ -219,13 +235,17 @@ abstract class Model implements JsonSerializable, ArrayAccess, Arrayable, Jsonab
|
||||
*/
|
||||
public function invoke($method, array $vars = [])
|
||||
{
|
||||
if (is_string($method)) {
|
||||
$method = [$this, $method];
|
||||
}
|
||||
|
||||
if (self::$invoker) {
|
||||
$call = self::$invoker;
|
||||
|
||||
return $call($method instanceof Closure ? $method : Closure::fromCallable([$this, $method]), $vars);
|
||||
return $call($method instanceof Closure ? $method : Closure::fromCallable($method), $vars);
|
||||
}
|
||||
|
||||
return call_user_func_array($method instanceof Closure ? $method : [$this, $method], $vars);
|
||||
return call_user_func_array($method, $vars);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -233,7 +253,7 @@ abstract class Model implements JsonSerializable, ArrayAccess, Arrayable, Jsonab
|
||||
*
|
||||
* @param array|object $data 数据
|
||||
*/
|
||||
public function __construct(array|object $data = [])
|
||||
public function __construct(array | object $data = [])
|
||||
{
|
||||
// 设置数据
|
||||
$this->data($data);
|
||||
@ -243,7 +263,7 @@ abstract class Model implements JsonSerializable, ArrayAccess, Arrayable, Jsonab
|
||||
|
||||
if (empty($this->name)) {
|
||||
// 当前模型名
|
||||
$name = str_replace('\\', '/', static::class);
|
||||
$name = str_replace('\\', '/', static::class);
|
||||
$this->name = basename($name);
|
||||
}
|
||||
|
||||
@ -267,6 +287,20 @@ abstract class Model implements JsonSerializable, ArrayAccess, Arrayable, Jsonab
|
||||
return $this->name;
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置当前模型名称.
|
||||
*
|
||||
* @param string $name 模型名称
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
public function setName(string $name)
|
||||
{
|
||||
$this->name = $name;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建新的模型实例.
|
||||
*
|
||||
@ -358,7 +392,7 @@ abstract class Model implements JsonSerializable, ArrayAccess, Arrayable, Jsonab
|
||||
*/
|
||||
public function getSuffix(): string
|
||||
{
|
||||
return $this->suffix ?: '';
|
||||
return $this->suffix ?? '';
|
||||
}
|
||||
|
||||
/**
|
||||
@ -372,17 +406,24 @@ abstract class Model implements JsonSerializable, ArrayAccess, Arrayable, Jsonab
|
||||
{
|
||||
/** @var Query $query */
|
||||
$query = self::$db->connect($this->connection)
|
||||
->name($this->name . $this->suffix)
|
||||
->name($this->name)
|
||||
->pk($this->pk);
|
||||
|
||||
if (!empty($this->autoInc)) {
|
||||
$query->autoinc(is_string($this->autoInc) ? $this->autoInc : $this->pk);
|
||||
}
|
||||
|
||||
if (!empty($this->table)) {
|
||||
$query->table($this->table . $this->suffix);
|
||||
} elseif (!empty($this->suffix)) {
|
||||
$query->suffix($this->suffix);
|
||||
}
|
||||
|
||||
$query->model($this)
|
||||
->json($this->json, $this->jsonAssoc)
|
||||
->setFieldType(array_merge($this->schema, $this->jsonType))
|
||||
->setKey($this->getKey())
|
||||
->readonly($this->readonly)
|
||||
->lazyFields($this->lazyFields);
|
||||
|
||||
// 软删除
|
||||
@ -478,9 +519,9 @@ abstract class Model implements JsonSerializable, ArrayAccess, Arrayable, Jsonab
|
||||
public function refresh(bool $relation = false)
|
||||
{
|
||||
if ($this->exists) {
|
||||
$this->data = $this->db()->find($this->getKey())->getData();
|
||||
$this->origin = $this->data;
|
||||
$this->get = [];
|
||||
$this->data = $this->db()->find($this->getKey())->getData();
|
||||
$this->origin = $this->data;
|
||||
$this->get = [];
|
||||
|
||||
if ($relation) {
|
||||
$this->relation = [];
|
||||
@ -524,6 +565,36 @@ abstract class Model implements JsonSerializable, ArrayAccess, Arrayable, Jsonab
|
||||
return empty($this->data);
|
||||
}
|
||||
|
||||
/**
|
||||
* 字段值增长
|
||||
*
|
||||
* @param string $field 字段名
|
||||
* @param float $step 增长值
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
public function inc(string $field, float $step = 1)
|
||||
{
|
||||
$this->setAttr($field, ['INC', $step]);
|
||||
$this->change[$field] = $this->origin[$field] + $step;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 字段值减少.
|
||||
*
|
||||
* @param string $field 字段名
|
||||
* @param float $step 增长值
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
public function dec(string $field, float $step = 1)
|
||||
{
|
||||
$this->setAttr($field, ['DEC', $step]);
|
||||
$this->change[$field] = $this->origin[$field] - $step;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 保存当前数据对象
|
||||
*
|
||||
@ -532,7 +603,7 @@ abstract class Model implements JsonSerializable, ArrayAccess, Arrayable, Jsonab
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function save(array|object $data = [], string $sequence = null): bool
|
||||
public function save(array | object $data = [], ?string $sequence = null): bool
|
||||
{
|
||||
if ($data instanceof Model) {
|
||||
$data = $data->getData();
|
||||
@ -556,9 +627,17 @@ abstract class Model implements JsonSerializable, ArrayAccess, Arrayable, Jsonab
|
||||
// 写入回调
|
||||
$this->trigger('AfterWrite');
|
||||
|
||||
if (!empty($this->change)) {
|
||||
// 处理递增递减数据
|
||||
foreach ($this->change as $field => $val) {
|
||||
$this->data[$field] = $val;
|
||||
}
|
||||
$this->change = [];
|
||||
}
|
||||
|
||||
// 重新记录原始数据
|
||||
$this->origin = $this->data;
|
||||
$this->get = [];
|
||||
$this->origin = $this->data;
|
||||
$this->get = [];
|
||||
|
||||
return true;
|
||||
}
|
||||
@ -626,14 +705,14 @@ abstract class Model implements JsonSerializable, ArrayAccess, Arrayable, Jsonab
|
||||
|
||||
if ($this->autoWriteTimestamp && $this->updateTime) {
|
||||
// 自动写入更新时间
|
||||
$data[$this->updateTime] = $this->autoWriteTimestamp();
|
||||
$data[$this->updateTime] = $this->autoWriteTimestamp();
|
||||
$this->data[$this->updateTime] = $data[$this->updateTime];
|
||||
}
|
||||
|
||||
// 检查允许字段
|
||||
$allowFields = $this->checkAllowFields();
|
||||
|
||||
foreach ($this->relationWrite as $name => $val) {
|
||||
foreach ($this->relationWrite as $val) {
|
||||
if (!is_array($val)) {
|
||||
continue;
|
||||
}
|
||||
@ -646,12 +725,12 @@ abstract class Model implements JsonSerializable, ArrayAccess, Arrayable, Jsonab
|
||||
}
|
||||
|
||||
// 模型更新
|
||||
$db = $this->db();
|
||||
$db = $this->db(null);
|
||||
|
||||
$db->transaction(function () use ($data, $allowFields, $db) {
|
||||
$this->key = null;
|
||||
$where = $this->getWhere();
|
||||
$result = $db->where($where)
|
||||
$this->key = null;
|
||||
$where = $this->getWhere();
|
||||
$result = $db->where($where)
|
||||
->strict(false)
|
||||
->cache(true)
|
||||
->setOption('key', $this->key)
|
||||
@ -679,32 +758,50 @@ abstract class Model implements JsonSerializable, ArrayAccess, Arrayable, Jsonab
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
protected function insertData(string $sequence = null): bool
|
||||
protected function insertData(?string $sequence = null): bool
|
||||
{
|
||||
if (false === $this->trigger('BeforeInsert')) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$this->checkData();
|
||||
$data = $this->data;
|
||||
|
||||
// 时间戳自动写入
|
||||
if ($this->autoWriteTimestamp) {
|
||||
if ($this->createTime && !isset($data[$this->createTime])) {
|
||||
$data[$this->createTime] = $this->autoWriteTimestamp();
|
||||
$this->data[$this->createTime] = $data[$this->createTime];
|
||||
// 主键自动写入
|
||||
if ($this->isAutoWriteId()) {
|
||||
$pk = $this->getPk();
|
||||
if (is_string($pk) && !isset($this->data[$pk])) {
|
||||
$this->data[$pk] = $this->autoWriteId();
|
||||
}
|
||||
}
|
||||
|
||||
if ($this->updateTime && !isset($data[$this->updateTime])) {
|
||||
$data[$this->updateTime] = $this->autoWriteTimestamp();
|
||||
$this->data[$this->updateTime] = $data[$this->updateTime];
|
||||
// 时间字段自动写入
|
||||
if ($this->autoWriteTimestamp) {
|
||||
foreach ([$this->createTime, $this->updateTime] as $field) {
|
||||
if ($field && !array_key_exists($field, $this->data)) {
|
||||
$this->data[$field] = $this->autoWriteTimestamp();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 自动(使用修改器)写入字段
|
||||
if (!empty($this->insert)) {
|
||||
foreach ($this->insert as $name => $val) {
|
||||
$field = is_string($name) ? $name : $val;
|
||||
if (!isset($this->data[$field])) {
|
||||
if (is_string($name)) {
|
||||
$this->data[$name] = $val;
|
||||
} else {
|
||||
$this->setAttr($field, null);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 检查允许字段
|
||||
$allowFields = $this->checkAllowFields();
|
||||
|
||||
$db = $this->db();
|
||||
$db = $this->db();
|
||||
$data = $this->data;
|
||||
|
||||
$db->transaction(function () use ($data, $sequence, $allowFields, $db) {
|
||||
$result = $db->strict(false)
|
||||
@ -714,7 +811,7 @@ abstract class Model implements JsonSerializable, ArrayAccess, Arrayable, Jsonab
|
||||
->insert($data, true);
|
||||
|
||||
// 获取自动增长主键
|
||||
if ($result) {
|
||||
if ($result && !$this->isAutoWriteId()) {
|
||||
$pk = $this->getPk();
|
||||
|
||||
if (is_string($pk) && (!isset($this->data[$pk]) || '' == $this->data[$pk])) {
|
||||
@ -749,7 +846,7 @@ abstract class Model implements JsonSerializable, ArrayAccess, Arrayable, Jsonab
|
||||
$pk = $this->getPk();
|
||||
|
||||
if (is_string($pk) && isset($this->origin[$pk])) {
|
||||
$where = [[$pk, '=', $this->origin[$pk]]];
|
||||
$where = [[$pk, '=', $this->origin[$pk]]];
|
||||
$this->key = $this->origin[$pk];
|
||||
} elseif (is_array($pk)) {
|
||||
foreach ($pk as $field) {
|
||||
@ -821,8 +918,8 @@ abstract class Model implements JsonSerializable, ArrayAccess, Arrayable, Jsonab
|
||||
}
|
||||
|
||||
// 读取更新条件
|
||||
$where = $this->getWhere();
|
||||
$db = $this->db();
|
||||
$where = $this->getWhere();
|
||||
$db = $this->db();
|
||||
|
||||
$db->transaction(function () use ($where, $db) {
|
||||
// 删除当前模型数据
|
||||
@ -836,7 +933,7 @@ abstract class Model implements JsonSerializable, ArrayAccess, Arrayable, Jsonab
|
||||
|
||||
$this->trigger('AfterDelete');
|
||||
|
||||
$this->exists = false;
|
||||
$this->exists = false;
|
||||
|
||||
return true;
|
||||
}
|
||||
@ -844,14 +941,14 @@ abstract class Model implements JsonSerializable, ArrayAccess, Arrayable, Jsonab
|
||||
/**
|
||||
* 写入数据.
|
||||
*
|
||||
* @param array $data 数据数组
|
||||
* @param array $allowField 允许字段
|
||||
* @param bool $replace 使用Replace
|
||||
* @param string $suffix 数据表后缀
|
||||
* @param array|object $data 数据
|
||||
* @param array $allowField 允许字段
|
||||
* @param bool $replace 使用Replace
|
||||
* @param string $suffix 数据表后缀
|
||||
*
|
||||
* @return static
|
||||
*/
|
||||
public static function create(array $data, array $allowField = [], bool $replace = false, string $suffix = ''): Model
|
||||
public static function create(array | object $data, array $allowField = [], bool $replace = false, string $suffix = ''): Model
|
||||
{
|
||||
$model = new static();
|
||||
|
||||
@ -871,14 +968,14 @@ abstract class Model implements JsonSerializable, ArrayAccess, Arrayable, Jsonab
|
||||
/**
|
||||
* 更新数据.
|
||||
*
|
||||
* @param array $data 数据数组
|
||||
* @param mixed $where 更新条件
|
||||
* @param array $allowField 允许字段
|
||||
* @param string $suffix 数据表后缀
|
||||
* @param array|object $data 数据数组
|
||||
* @param mixed $where 更新条件
|
||||
* @param array $allowField 允许字段
|
||||
* @param string $suffix 数据表后缀
|
||||
*
|
||||
* @return static
|
||||
*/
|
||||
public static function update(array $data, $where = [], array $allowField = [], string $suffix = '')
|
||||
public static function update(array | object $data, $where = [], array $allowField = [], string $suffix = '')
|
||||
{
|
||||
$model = new static();
|
||||
|
||||
@ -1022,7 +1119,7 @@ abstract class Model implements JsonSerializable, ArrayAccess, Arrayable, Jsonab
|
||||
*
|
||||
* @return Query
|
||||
*/
|
||||
public static function withoutGlobalScope(array $scope = null): Query
|
||||
public static function withoutGlobalScope(?array $scope = null): Query
|
||||
{
|
||||
$model = new static();
|
||||
|
||||
@ -1074,6 +1171,10 @@ abstract class Model implements JsonSerializable, ArrayAccess, Arrayable, Jsonab
|
||||
return call_user_func_array(static::$macro[static::class][$method]->bindTo($this, static::class), $args);
|
||||
}
|
||||
|
||||
if ($this->exists && strtolower($method) == 'withattr') {
|
||||
return call_user_func_array([$this, 'withFieldAttr'], $args);
|
||||
}
|
||||
|
||||
return call_user_func_array([$this->db(), $method], $args);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user