Model.php 25 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkPHP [ WE CAN DO IT JUST THINK ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2006~2019 http://thinkphp.cn All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
  8. // +----------------------------------------------------------------------
  9. // | Author: liu21st <liu21st@gmail.com>
  10. // +----------------------------------------------------------------------
  11. declare (strict_types = 1);
  12. namespace think;
  13. use ArrayAccess;
  14. use Closure;
  15. use JsonSerializable;
  16. use think\contract\Arrayable;
  17. use think\contract\Jsonable;
  18. use think\db\BaseQuery as Query;
  19. /**
  20. * Class Model
  21. * @package think
  22. * @mixin Query
  23. * @method void onAfterRead(Model $model) static after_read事件定义
  24. * @method mixed onBeforeInsert(Model $model) static before_insert事件定义
  25. * @method void onAfterInsert(Model $model) static after_insert事件定义
  26. * @method mixed onBeforeUpdate(Model $model) static before_update事件定义
  27. * @method void onAfterUpdate(Model $model) static after_update事件定义
  28. * @method mixed onBeforeWrite(Model $model) static before_write事件定义
  29. * @method void onAfterWrite(Model $model) static after_write事件定义
  30. * @method mixed onBeforeDelete(Model $model) static before_write事件定义
  31. * @method void onAfterDelete(Model $model) static after_delete事件定义
  32. * @method void onBeforeRestore(Model $model) static before_restore事件定义
  33. * @method void onAfterRestore(Model $model) static after_restore事件定义
  34. */
  35. abstract class Model implements JsonSerializable, ArrayAccess, Arrayable, Jsonable
  36. {
  37. use model\concern\Attribute;
  38. use model\concern\RelationShip;
  39. use model\concern\ModelEvent;
  40. use model\concern\TimeStamp;
  41. use model\concern\Conversion;
  42. /**
  43. * 数据是否存在
  44. * @var bool
  45. */
  46. private $exists = false;
  47. /**
  48. * 是否强制更新所有数据
  49. * @var bool
  50. */
  51. private $force = false;
  52. /**
  53. * 是否Replace
  54. * @var bool
  55. */
  56. private $replace = false;
  57. /**
  58. * 数据表后缀
  59. * @var string
  60. */
  61. protected $suffix;
  62. /**
  63. * 更新条件
  64. * @var array
  65. */
  66. private $updateWhere;
  67. /**
  68. * 数据库配置
  69. * @var string
  70. */
  71. protected $connection;
  72. /**
  73. * 模型名称
  74. * @var string
  75. */
  76. protected $name;
  77. /**
  78. * 主键值
  79. * @var string
  80. */
  81. protected $key;
  82. /**
  83. * 数据表名称
  84. * @var string
  85. */
  86. protected $table;
  87. /**
  88. * 初始化过的模型.
  89. * @var array
  90. */
  91. protected static $initialized = [];
  92. /**
  93. * 软删除字段默认值
  94. * @var mixed
  95. */
  96. protected $defaultSoftDelete;
  97. /**
  98. * 全局查询范围
  99. * @var array
  100. */
  101. protected $globalScope = [];
  102. /**
  103. * 延迟保存信息
  104. * @var bool
  105. */
  106. private $lazySave = false;
  107. /**
  108. * Db对象
  109. * @var DbManager
  110. */
  111. protected static $db;
  112. /**
  113. * 容器对象的依赖注入方法
  114. * @var callable
  115. */
  116. protected static $invoker;
  117. /**
  118. * 服务注入
  119. * @var Closure[]
  120. */
  121. protected static $maker = [];
  122. /**
  123. * 方法注入
  124. * @var Closure[][]
  125. */
  126. protected static $macro = [];
  127. /**
  128. * 设置服务注入
  129. * @access public
  130. * @param Closure $maker
  131. * @return void
  132. */
  133. public static function maker(Closure $maker)
  134. {
  135. static::$maker[] = $maker;
  136. }
  137. /**
  138. * 设置方法注入
  139. * @access public
  140. * @param string $method
  141. * @param Closure $closure
  142. * @return void
  143. */
  144. public static function macro(string $method, Closure $closure)
  145. {
  146. if (!isset(static::$macro[static::class])) {
  147. static::$macro[static::class] = [];
  148. }
  149. static::$macro[static::class][$method] = $closure;
  150. }
  151. /**
  152. * 设置Db对象
  153. * @access public
  154. * @param DbManager $db Db对象
  155. * @return void
  156. */
  157. public static function setDb(DbManager $db)
  158. {
  159. self::$db = $db;
  160. }
  161. /**
  162. * 设置容器对象的依赖注入方法
  163. * @access public
  164. * @param callable $callable 依赖注入方法
  165. * @return void
  166. */
  167. public static function setInvoker(callable $callable): void
  168. {
  169. self::$invoker = $callable;
  170. }
  171. /**
  172. * 调用反射执行模型方法 支持参数绑定
  173. * @access public
  174. * @param mixed $method
  175. * @param array $vars 参数
  176. * @return mixed
  177. */
  178. public function invoke($method, array $vars = [])
  179. {
  180. if (self::$invoker) {
  181. $call = self::$invoker;
  182. return $call($method instanceof Closure ? $method : Closure::fromCallable([$this, $method]), $vars);
  183. }
  184. return call_user_func_array($method instanceof Closure ? $method : [$this, $method], $vars);
  185. }
  186. /**
  187. * 架构函数
  188. * @access public
  189. * @param array $data 数据
  190. */
  191. public function __construct(array $data = [])
  192. {
  193. $this->data = $data;
  194. if (!empty($this->data)) {
  195. // 废弃字段
  196. foreach ((array) $this->disuse as $key) {
  197. if (array_key_exists($key, $this->data)) {
  198. unset($this->data[$key]);
  199. }
  200. }
  201. }
  202. // 记录原始数据
  203. $this->origin = $this->data;
  204. if (empty($this->name)) {
  205. // 当前模型名
  206. $name = str_replace('\\', '/', static::class);
  207. $this->name = basename($name);
  208. }
  209. if (!empty(static::$maker)) {
  210. foreach (static::$maker as $maker) {
  211. call_user_func($maker, $this);
  212. }
  213. }
  214. // 执行初始化操作
  215. $this->initialize();
  216. }
  217. /**
  218. * 获取当前模型名称
  219. * @access public
  220. * @return string
  221. */
  222. public function getName(): string
  223. {
  224. return $this->name;
  225. }
  226. /**
  227. * 创建新的模型实例
  228. * @access public
  229. * @param array $data 数据
  230. * @param mixed $where 更新条件
  231. * @param array $options 参数
  232. * @return Model
  233. */
  234. public function newInstance(array $data = [], $where = null, array $options = []): Model
  235. {
  236. $model = new static($data);
  237. if ($this->connection) {
  238. $model->setConnection($this->connection);
  239. }
  240. if ($this->suffix) {
  241. $model->setSuffix($this->suffix);
  242. }
  243. if (empty($data)) {
  244. return $model;
  245. }
  246. $model->exists(true);
  247. $model->setUpdateWhere($where);
  248. $model->trigger('AfterRead');
  249. return $model;
  250. }
  251. /**
  252. * 设置模型的更新条件
  253. * @access protected
  254. * @param mixed $where 更新条件
  255. * @return void
  256. */
  257. protected function setUpdateWhere($where): void
  258. {
  259. $this->updateWhere = $where;
  260. }
  261. /**
  262. * 设置当前模型的数据库连接
  263. * @access public
  264. * @param string $connection 数据表连接标识
  265. * @return $this
  266. */
  267. public function setConnection(string $connection)
  268. {
  269. $this->connection = $connection;
  270. return $this;
  271. }
  272. /**
  273. * 获取当前模型的数据库连接标识
  274. * @access public
  275. * @return string
  276. */
  277. public function getConnection(): string
  278. {
  279. return $this->connection ?: '';
  280. }
  281. /**
  282. * 设置当前模型数据表的后缀
  283. * @access public
  284. * @param string $suffix 数据表后缀
  285. * @return $this
  286. */
  287. public function setSuffix(string $suffix)
  288. {
  289. $this->suffix = $suffix;
  290. return $this;
  291. }
  292. /**
  293. * 获取当前模型的数据表后缀
  294. * @access public
  295. * @return string
  296. */
  297. public function getSuffix(): string
  298. {
  299. return $this->suffix ?: '';
  300. }
  301. /**
  302. * 获取当前模型的数据库查询对象
  303. * @access public
  304. * @param array $scope 设置不使用的全局查询范围
  305. * @return Query
  306. */
  307. public function db($scope = []): Query
  308. {
  309. /** @var Query $query */
  310. $query = self::$db->connect($this->connection)
  311. ->name($this->name . $this->suffix)
  312. ->pk($this->pk);
  313. if (!empty($this->table)) {
  314. $query->table($this->table . $this->suffix);
  315. }
  316. $query->model($this)
  317. ->json($this->json, $this->jsonAssoc)
  318. ->setFieldType(array_merge($this->schema, $this->jsonType));
  319. // 软删除
  320. if (property_exists($this, 'withTrashed') && !$this->withTrashed) {
  321. $this->withNoTrashed($query);
  322. }
  323. // 全局作用域
  324. if (is_array($scope)) {
  325. $globalScope = array_diff($this->globalScope, $scope);
  326. $query->scope($globalScope);
  327. }
  328. // 返回当前模型的数据库查询对象
  329. return $query;
  330. }
  331. /**
  332. * 初始化模型
  333. * @access private
  334. * @return void
  335. */
  336. private function initialize(): void
  337. {
  338. if (!isset(static::$initialized[static::class])) {
  339. static::$initialized[static::class] = true;
  340. static::init();
  341. }
  342. }
  343. /**
  344. * 初始化处理
  345. * @access protected
  346. * @return void
  347. */
  348. protected static function init()
  349. {
  350. }
  351. protected function checkData(): void
  352. {
  353. }
  354. protected function checkResult($result): void
  355. {
  356. }
  357. /**
  358. * 更新是否强制写入数据 而不做比较(亦可用于软删除的强制删除)
  359. * @access public
  360. * @param bool $force
  361. * @return $this
  362. */
  363. public function force(bool $force = true)
  364. {
  365. $this->force = $force;
  366. return $this;
  367. }
  368. /**
  369. * 判断force
  370. * @access public
  371. * @return bool
  372. */
  373. public function isForce(): bool
  374. {
  375. return $this->force;
  376. }
  377. /**
  378. * 新增数据是否使用Replace
  379. * @access public
  380. * @param bool $replace
  381. * @return $this
  382. */
  383. public function replace(bool $replace = true)
  384. {
  385. $this->replace = $replace;
  386. return $this;
  387. }
  388. /**
  389. * 刷新模型数据
  390. * @access public
  391. * @param bool $relation 是否刷新关联数据
  392. * @return $this
  393. */
  394. public function refresh(bool $relation = false)
  395. {
  396. if ($this->exists) {
  397. $this->data = $this->db()->find($this->getKey())->getData();
  398. $this->origin = $this->data;
  399. $this->get = [];
  400. if ($relation) {
  401. $this->relation = [];
  402. }
  403. }
  404. return $this;
  405. }
  406. /**
  407. * 设置数据是否存在
  408. * @access public
  409. * @param bool $exists
  410. * @return $this
  411. */
  412. public function exists(bool $exists = true)
  413. {
  414. $this->exists = $exists;
  415. return $this;
  416. }
  417. /**
  418. * 判断数据是否存在数据库
  419. * @access public
  420. * @return bool
  421. */
  422. public function isExists(): bool
  423. {
  424. return $this->exists;
  425. }
  426. /**
  427. * 判断模型是否为空
  428. * @access public
  429. * @return bool
  430. */
  431. public function isEmpty(): bool
  432. {
  433. return empty($this->data);
  434. }
  435. /**
  436. * 延迟保存当前数据对象
  437. * @access public
  438. * @param array|bool $data 数据
  439. * @return void
  440. */
  441. public function lazySave($data = []): void
  442. {
  443. if (false === $data) {
  444. $this->lazySave = false;
  445. } else {
  446. if (is_array($data)) {
  447. $this->setAttrs($data);
  448. }
  449. $this->lazySave = true;
  450. }
  451. }
  452. /**
  453. * 保存当前数据对象
  454. * @access public
  455. * @param array $data 数据
  456. * @param string $sequence 自增序列名
  457. * @return bool
  458. */
  459. public function save(array $data = [], string $sequence = null): bool
  460. {
  461. // 数据对象赋值
  462. $this->setAttrs($data);
  463. if ($this->isEmpty() || false === $this->trigger('BeforeWrite')) {
  464. return false;
  465. }
  466. $result = $this->exists ? $this->updateData() : $this->insertData($sequence);
  467. if (false === $result) {
  468. return false;
  469. }
  470. // 写入回调
  471. $this->trigger('AfterWrite');
  472. // 重新记录原始数据
  473. $this->origin = $this->data;
  474. $this->get = [];
  475. $this->lazySave = false;
  476. return true;
  477. }
  478. /**
  479. * 检查数据是否允许写入
  480. * @access protected
  481. * @return array
  482. */
  483. protected function checkAllowFields(): array
  484. {
  485. // 检测字段
  486. if (empty($this->field)) {
  487. if (!empty($this->schema)) {
  488. $this->field = array_keys(array_merge($this->schema, $this->jsonType));
  489. } else {
  490. $query = $this->db();
  491. $table = $this->table ? $this->table . $this->suffix : $query->getTable();
  492. $this->field = $query->getConnection()->getTableFields($table);
  493. }
  494. return $this->field;
  495. }
  496. $field = $this->field;
  497. if ($this->autoWriteTimestamp) {
  498. array_push($field, $this->createTime, $this->updateTime);
  499. }
  500. if (!empty($this->disuse)) {
  501. // 废弃字段
  502. $field = array_diff($field, $this->disuse);
  503. }
  504. return $field;
  505. }
  506. /**
  507. * 保存写入数据
  508. * @access protected
  509. * @return bool
  510. */
  511. protected function updateData(): bool
  512. {
  513. // 事件回调
  514. if (false === $this->trigger('BeforeUpdate')) {
  515. return false;
  516. }
  517. $this->checkData();
  518. // 获取有更新的数据
  519. $data = $this->getChangedData();
  520. if (empty($data)) {
  521. // 关联更新
  522. if (!empty($this->relationWrite)) {
  523. $this->autoRelationUpdate();
  524. }
  525. return true;
  526. }
  527. if ($this->autoWriteTimestamp && $this->updateTime) {
  528. // 自动写入更新时间
  529. $data[$this->updateTime] = $this->autoWriteTimestamp();
  530. $this->data[$this->updateTime] = $data[$this->updateTime];
  531. }
  532. // 检查允许字段
  533. $allowFields = $this->checkAllowFields();
  534. foreach ($this->relationWrite as $name => $val) {
  535. if (!is_array($val)) {
  536. continue;
  537. }
  538. foreach ($val as $key) {
  539. if (isset($data[$key])) {
  540. unset($data[$key]);
  541. }
  542. }
  543. }
  544. // 模型更新
  545. $db = $this->db();
  546. $db->transaction(function () use ($data, $allowFields, $db) {
  547. $this->key = null;
  548. $where = $this->getWhere();
  549. $result = $db->where($where)
  550. ->strict(false)
  551. ->cache(true)
  552. ->setOption('key', $this->key)
  553. ->field($allowFields)
  554. ->update($data);
  555. $this->checkResult($result);
  556. // 关联更新
  557. if (!empty($this->relationWrite)) {
  558. $this->autoRelationUpdate();
  559. }
  560. });
  561. // 更新回调
  562. $this->trigger('AfterUpdate');
  563. return true;
  564. }
  565. /**
  566. * 新增写入数据
  567. * @access protected
  568. * @param string $sequence 自增名
  569. * @return bool
  570. */
  571. protected function insertData(string $sequence = null): bool
  572. {
  573. if (false === $this->trigger('BeforeInsert')) {
  574. return false;
  575. }
  576. $this->checkData();
  577. $data = $this->data;
  578. // 时间戳自动写入
  579. if ($this->autoWriteTimestamp) {
  580. if ($this->createTime && !isset($data[$this->createTime])) {
  581. $data[$this->createTime] = $this->autoWriteTimestamp();
  582. $this->data[$this->createTime] = $data[$this->createTime];
  583. }
  584. if ($this->updateTime && !isset($data[$this->updateTime])) {
  585. $data[$this->updateTime] = $this->autoWriteTimestamp();
  586. $this->data[$this->updateTime] = $data[$this->updateTime];
  587. }
  588. }
  589. // 检查允许字段
  590. $allowFields = $this->checkAllowFields();
  591. $db = $this->db();
  592. $db->transaction(function () use ($data, $sequence, $allowFields, $db) {
  593. $result = $db->strict(false)
  594. ->field($allowFields)
  595. ->replace($this->replace)
  596. ->sequence($sequence)
  597. ->insert($data, true);
  598. // 获取自动增长主键
  599. if ($result) {
  600. $pk = $this->getPk();
  601. if (is_string($pk) && (!isset($this->data[$pk]) || '' == $this->data[$pk])) {
  602. unset($this->get[$pk]);
  603. $this->data[$pk] = $result;
  604. }
  605. }
  606. // 关联写入
  607. if (!empty($this->relationWrite)) {
  608. $this->autoRelationInsert();
  609. }
  610. });
  611. // 标记数据已经存在
  612. $this->exists = true;
  613. $this->origin = $this->data;
  614. // 新增回调
  615. $this->trigger('AfterInsert');
  616. return true;
  617. }
  618. /**
  619. * 获取当前的更新条件
  620. * @access public
  621. * @return mixed
  622. */
  623. public function getWhere()
  624. {
  625. $pk = $this->getPk();
  626. if (is_string($pk) && isset($this->origin[$pk])) {
  627. $where = [[$pk, '=', $this->origin[$pk]]];
  628. $this->key = $this->origin[$pk];
  629. } elseif (is_array($pk)) {
  630. foreach ($pk as $field) {
  631. if (isset($this->origin[$field])) {
  632. $where[] = [$field, '=', $this->origin[$field]];
  633. }
  634. }
  635. }
  636. if (empty($where)) {
  637. $where = empty($this->updateWhere) ? null : $this->updateWhere;
  638. }
  639. return $where;
  640. }
  641. /**
  642. * 保存多个数据到当前数据对象
  643. * @access public
  644. * @param iterable $dataSet 数据
  645. * @param boolean $replace 是否自动识别更新和写入
  646. * @return Collection
  647. * @throws \Exception
  648. */
  649. public function saveAll(iterable $dataSet, bool $replace = true): Collection
  650. {
  651. $db = $this->db();
  652. $result = $db->transaction(function () use ($replace, $dataSet) {
  653. $pk = $this->getPk();
  654. if (is_string($pk) && $replace) {
  655. $auto = true;
  656. }
  657. $result = [];
  658. $suffix = $this->getSuffix();
  659. foreach ($dataSet as $key => $data) {
  660. if ($this->exists || (!empty($auto) && isset($data[$pk]))) {
  661. $result[$key] = static::update($data, [], [], $suffix);
  662. } else {
  663. $result[$key] = static::create($data, $this->field, $this->replace, $suffix);
  664. }
  665. }
  666. return $result;
  667. });
  668. return $this->toCollection($result);
  669. }
  670. /**
  671. * 删除当前的记录
  672. * @access public
  673. * @return bool
  674. */
  675. public function delete(): bool
  676. {
  677. if (!$this->exists || $this->isEmpty() || false === $this->trigger('BeforeDelete')) {
  678. return false;
  679. }
  680. // 读取更新条件
  681. $where = $this->getWhere();
  682. $db = $this->db();
  683. $db->transaction(function () use ($where, $db) {
  684. // 删除当前模型数据
  685. $db->where($where)->delete();
  686. // 关联删除
  687. if (!empty($this->relationWrite)) {
  688. $this->autoRelationDelete();
  689. }
  690. });
  691. $this->trigger('AfterDelete');
  692. $this->exists = false;
  693. $this->lazySave = false;
  694. return true;
  695. }
  696. /**
  697. * 写入数据
  698. * @access public
  699. * @param array $data 数据数组
  700. * @param array $allowField 允许字段
  701. * @param bool $replace 使用Replace
  702. * @param string $suffix 数据表后缀
  703. * @return static
  704. */
  705. public static function create(array $data, array $allowField = [], bool $replace = false, string $suffix = ''): Model
  706. {
  707. $model = new static();
  708. if (!empty($allowField)) {
  709. $model->allowField($allowField);
  710. }
  711. if (!empty($suffix)) {
  712. $model->setSuffix($suffix);
  713. }
  714. $model->replace($replace)->save($data);
  715. return $model;
  716. }
  717. /**
  718. * 更新数据
  719. * @access public
  720. * @param array $data 数据数组
  721. * @param mixed $where 更新条件
  722. * @param array $allowField 允许字段
  723. * @param string $suffix 数据表后缀
  724. * @return static
  725. */
  726. public static function update(array $data, $where = [], array $allowField = [], string $suffix = '')
  727. {
  728. $model = new static();
  729. if (!empty($allowField)) {
  730. $model->allowField($allowField);
  731. }
  732. if (!empty($where)) {
  733. $model->setUpdateWhere($where);
  734. }
  735. if (!empty($suffix)) {
  736. $model->setSuffix($suffix);
  737. }
  738. $model->exists(true)->save($data);
  739. return $model;
  740. }
  741. /**
  742. * 删除记录
  743. * @access public
  744. * @param mixed $data 主键列表 支持闭包查询条件
  745. * @param bool $force 是否强制删除
  746. * @return bool
  747. */
  748. public static function destroy($data, bool $force = false): bool
  749. {
  750. if (empty($data) && 0 !== $data) {
  751. return false;
  752. }
  753. $model = new static();
  754. $query = $model->db();
  755. if (is_array($data) && key($data) !== 0) {
  756. $query->where($data);
  757. $data = null;
  758. } elseif ($data instanceof \Closure) {
  759. $data($query);
  760. $data = null;
  761. }
  762. $resultSet = $query->select($data);
  763. foreach ($resultSet as $result) {
  764. $result->force($force)->delete();
  765. }
  766. return true;
  767. }
  768. /**
  769. * 解序列化后处理
  770. */
  771. public function __wakeup()
  772. {
  773. $this->initialize();
  774. }
  775. /**
  776. * 修改器 设置数据对象的值
  777. * @access public
  778. * @param string $name 名称
  779. * @param mixed $value 值
  780. * @return void
  781. */
  782. public function __set(string $name, $value): void
  783. {
  784. $this->setAttr($name, $value);
  785. }
  786. /**
  787. * 获取器 获取数据对象的值
  788. * @access public
  789. * @param string $name 名称
  790. * @return mixed
  791. */
  792. public function __get(string $name)
  793. {
  794. return $this->getAttr($name);
  795. }
  796. /**
  797. * 检测数据对象的值
  798. * @access public
  799. * @param string $name 名称
  800. * @return bool
  801. */
  802. public function __isset(string $name): bool
  803. {
  804. return !is_null($this->getAttr($name));
  805. }
  806. /**
  807. * 销毁数据对象的值
  808. * @access public
  809. * @param string $name 名称
  810. * @return void
  811. */
  812. public function __unset(string $name): void
  813. {
  814. unset($this->data[$name],
  815. $this->get[$name],
  816. $this->relation[$name]);
  817. }
  818. // ArrayAccess
  819. #[\ReturnTypeWillChange]
  820. public function offsetSet($name, $value)
  821. {
  822. $this->setAttr($name, $value);
  823. }
  824. #[\ReturnTypeWillChange]
  825. public function offsetExists($name): bool
  826. {
  827. return $this->__isset($name);
  828. }
  829. #[\ReturnTypeWillChange]
  830. public function offsetUnset($name)
  831. {
  832. $this->__unset($name);
  833. }
  834. #[\ReturnTypeWillChange]
  835. public function offsetGet($name)
  836. {
  837. return $this->getAttr($name);
  838. }
  839. /**
  840. * 设置不使用的全局查询范围
  841. * @access public
  842. * @param array $scope 不启用的全局查询范围
  843. * @return Query
  844. */
  845. public static function withoutGlobalScope(array $scope = null)
  846. {
  847. $model = new static();
  848. return $model->db($scope);
  849. }
  850. /**
  851. * 切换后缀进行查询
  852. * @access public
  853. * @param string $suffix 切换的表后缀
  854. * @return Model
  855. */
  856. public static function suffix(string $suffix)
  857. {
  858. $model = new static();
  859. $model->setSuffix($suffix);
  860. return $model;
  861. }
  862. /**
  863. * 切换数据库连接进行查询
  864. * @access public
  865. * @param string $connection 数据库连接标识
  866. * @return Model
  867. */
  868. public static function connect(string $connection)
  869. {
  870. $model = new static();
  871. $model->setConnection($connection);
  872. return $model;
  873. }
  874. public function __call($method, $args)
  875. {
  876. if (isset(static::$macro[static::class][$method])) {
  877. return call_user_func_array(static::$macro[static::class][$method]->bindTo($this, static::class), $args);
  878. }
  879. return call_user_func_array([$this->db(), $method], $args);
  880. }
  881. public static function __callStatic($method, $args)
  882. {
  883. if (isset(static::$macro[static::class][$method])) {
  884. return call_user_func_array(static::$macro[static::class][$method]->bindTo(null, static::class), $args);
  885. }
  886. $model = new static();
  887. return call_user_func_array([$model->db(), $method], $args);
  888. }
  889. /**
  890. * 析构方法
  891. * @access public
  892. */
  893. public function __destruct()
  894. {
  895. if ($this->lazySave) {
  896. $this->save();
  897. }
  898. }
  899. }