Model.php 26 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060
  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. * @return Model
  232. */
  233. public function newInstance(array $data = [], $where = null): Model
  234. {
  235. $model = new static($data);
  236. if ($this->connection) {
  237. $model->setConnection($this->connection);
  238. }
  239. if ($this->suffix) {
  240. $model->setSuffix($this->suffix);
  241. }
  242. if (empty($data)) {
  243. return $model;
  244. }
  245. $model->exists(true);
  246. $model->setUpdateWhere($where);
  247. $model->trigger('AfterRead');
  248. return $model;
  249. }
  250. /**
  251. * 设置模型的更新条件
  252. * @access protected
  253. * @param mixed $where 更新条件
  254. * @return void
  255. */
  256. protected function setUpdateWhere($where): void
  257. {
  258. $this->updateWhere = $where;
  259. }
  260. /**
  261. * 设置当前模型的数据库连接
  262. * @access public
  263. * @param string $connection 数据表连接标识
  264. * @return $this
  265. */
  266. public function setConnection(string $connection)
  267. {
  268. $this->connection = $connection;
  269. return $this;
  270. }
  271. /**
  272. * 获取当前模型的数据库连接标识
  273. * @access public
  274. * @return string
  275. */
  276. public function getConnection(): string
  277. {
  278. return $this->connection ?: '';
  279. }
  280. /**
  281. * 设置当前模型数据表的后缀
  282. * @access public
  283. * @param string $suffix 数据表后缀
  284. * @return $this
  285. */
  286. public function setSuffix(string $suffix)
  287. {
  288. $this->suffix = $suffix;
  289. return $this;
  290. }
  291. /**
  292. * 获取当前模型的数据表后缀
  293. * @access public
  294. * @return string
  295. */
  296. public function getSuffix(): string
  297. {
  298. return $this->suffix ?: '';
  299. }
  300. /**
  301. * 获取当前模型的数据库查询对象
  302. * @access public
  303. * @param array $scope 设置不使用的全局查询范围
  304. * @return Query
  305. */
  306. public function db($scope = []): Query
  307. {
  308. /** @var Query $query */
  309. $query = self::$db->connect($this->connection)
  310. ->name($this->name . $this->suffix)
  311. ->pk($this->pk);
  312. if (!empty($this->table)) {
  313. $query->table($this->table . $this->suffix);
  314. }
  315. $query->model($this)
  316. ->json($this->json, $this->jsonAssoc)
  317. ->setFieldType(array_merge($this->schema, $this->jsonType));
  318. // 软删除
  319. if (property_exists($this, 'withTrashed') && !$this->withTrashed) {
  320. $this->withNoTrashed($query);
  321. }
  322. // 全局作用域
  323. if (is_array($scope)) {
  324. $globalScope = array_diff($this->globalScope, $scope);
  325. $query->scope($globalScope);
  326. }
  327. // 返回当前模型的数据库查询对象
  328. return $query;
  329. }
  330. /**
  331. * 初始化模型
  332. * @access private
  333. * @return void
  334. */
  335. private function initialize(): void
  336. {
  337. if (!isset(static::$initialized[static::class])) {
  338. static::$initialized[static::class] = true;
  339. static::init();
  340. }
  341. }
  342. /**
  343. * 初始化处理
  344. * @access protected
  345. * @return void
  346. */
  347. protected static function init()
  348. {
  349. }
  350. protected function checkData(): void
  351. {
  352. }
  353. protected function checkResult($result): void
  354. {
  355. }
  356. /**
  357. * 更新是否强制写入数据 而不做比较(亦可用于软删除的强制删除)
  358. * @access public
  359. * @param bool $force
  360. * @return $this
  361. */
  362. public function force(bool $force = true)
  363. {
  364. $this->force = $force;
  365. return $this;
  366. }
  367. /**
  368. * 判断force
  369. * @access public
  370. * @return bool
  371. */
  372. public function isForce(): bool
  373. {
  374. return $this->force;
  375. }
  376. /**
  377. * 新增数据是否使用Replace
  378. * @access public
  379. * @param bool $replace
  380. * @return $this
  381. */
  382. public function replace(bool $replace = true)
  383. {
  384. $this->replace = $replace;
  385. return $this;
  386. }
  387. /**
  388. * 刷新模型数据
  389. * @access public
  390. * @param bool $relation 是否刷新关联数据
  391. * @return $this
  392. */
  393. public function refresh(bool $relation = false)
  394. {
  395. if ($this->exists) {
  396. $this->data = $this->db()->find($this->getKey())->getData();
  397. $this->origin = $this->data;
  398. if ($relation) {
  399. $this->relation = [];
  400. }
  401. }
  402. return $this;
  403. }
  404. /**
  405. * 设置数据是否存在
  406. * @access public
  407. * @param bool $exists
  408. * @return $this
  409. */
  410. public function exists(bool $exists = true)
  411. {
  412. $this->exists = $exists;
  413. return $this;
  414. }
  415. /**
  416. * 判断数据是否存在数据库
  417. * @access public
  418. * @return bool
  419. */
  420. public function isExists(): bool
  421. {
  422. return $this->exists;
  423. }
  424. /**
  425. * 判断模型是否为空
  426. * @access public
  427. * @return bool
  428. */
  429. public function isEmpty(): bool
  430. {
  431. return empty($this->data);
  432. }
  433. /**
  434. * 延迟保存当前数据对象
  435. * @access public
  436. * @param array|bool $data 数据
  437. * @return void
  438. */
  439. public function lazySave($data = []): void
  440. {
  441. if (false === $data) {
  442. $this->lazySave = false;
  443. } else {
  444. if (is_array($data)) {
  445. $this->setAttrs($data);
  446. }
  447. $this->lazySave = true;
  448. }
  449. }
  450. /**
  451. * 保存当前数据对象
  452. * @access public
  453. * @param array $data 数据
  454. * @param string $sequence 自增序列名
  455. * @return bool
  456. */
  457. public function save(array $data = [], string $sequence = null): bool
  458. {
  459. // 数据对象赋值
  460. $this->setAttrs($data);
  461. if ($this->isEmpty() || false === $this->trigger('BeforeWrite')) {
  462. return false;
  463. }
  464. $result = $this->exists ? $this->updateData() : $this->insertData($sequence);
  465. if (false === $result) {
  466. return false;
  467. }
  468. // 写入回调
  469. $this->trigger('AfterWrite');
  470. // 重新记录原始数据
  471. $this->origin = $this->data;
  472. $this->set = [];
  473. $this->lazySave = false;
  474. return true;
  475. }
  476. /**
  477. * 检查数据是否允许写入
  478. * @access protected
  479. * @return array
  480. */
  481. protected function checkAllowFields(): array
  482. {
  483. // 检测字段
  484. if (empty($this->field)) {
  485. if (!empty($this->schema)) {
  486. $this->field = array_keys(array_merge($this->schema, $this->jsonType));
  487. } else {
  488. $query = $this->db();
  489. $table = $this->table ? $this->table . $this->suffix : $query->getTable();
  490. $this->field = $query->getConnection()->getTableFields($table);
  491. }
  492. return $this->field;
  493. }
  494. $field = $this->field;
  495. if ($this->autoWriteTimestamp) {
  496. array_push($field, $this->createTime, $this->updateTime);
  497. }
  498. if (!empty($this->disuse)) {
  499. // 废弃字段
  500. $field = array_diff($field, $this->disuse);
  501. }
  502. return $field;
  503. }
  504. /**
  505. * 保存写入数据
  506. * @access protected
  507. * @return bool
  508. */
  509. protected function updateData(): bool
  510. {
  511. // 事件回调
  512. if (false === $this->trigger('BeforeUpdate')) {
  513. return false;
  514. }
  515. $this->checkData();
  516. // 获取有更新的数据
  517. $data = $this->getChangedData();
  518. if (empty($data)) {
  519. // 关联更新
  520. if (!empty($this->relationWrite)) {
  521. $this->autoRelationUpdate();
  522. }
  523. return true;
  524. }
  525. if ($this->autoWriteTimestamp && $this->updateTime && !isset($data[$this->updateTime])) {
  526. // 自动写入更新时间
  527. $data[$this->updateTime] = $this->autoWriteTimestamp($this->updateTime);
  528. $this->data[$this->updateTime] = $data[$this->updateTime];
  529. }
  530. // 检查允许字段
  531. $allowFields = $this->checkAllowFields();
  532. foreach ($this->relationWrite as $name => $val) {
  533. if (!is_array($val)) {
  534. continue;
  535. }
  536. foreach ($val as $key) {
  537. if (isset($data[$key])) {
  538. unset($data[$key]);
  539. }
  540. }
  541. }
  542. // 模型更新
  543. $db = $this->db();
  544. $db->transaction(function () use ($data, $allowFields, $db) {
  545. $this->key = null;
  546. $where = $this->getWhere();
  547. $result = $db->where($where)
  548. ->strict(false)
  549. ->cache(true)
  550. ->setOption('key', $this->key)
  551. ->field($allowFields)
  552. ->update($data);
  553. $this->checkResult($result);
  554. // 关联更新
  555. if (!empty($this->relationWrite)) {
  556. $this->autoRelationUpdate();
  557. }
  558. });
  559. // 更新回调
  560. $this->trigger('AfterUpdate');
  561. return true;
  562. }
  563. /**
  564. * 新增写入数据
  565. * @access protected
  566. * @param string $sequence 自增名
  567. * @return bool
  568. */
  569. protected function insertData(string $sequence = null): bool
  570. {
  571. // 时间戳自动写入
  572. if ($this->autoWriteTimestamp) {
  573. if ($this->createTime && !isset($this->data[$this->createTime])) {
  574. $this->data[$this->createTime] = $this->autoWriteTimestamp($this->createTime);
  575. }
  576. if ($this->updateTime && !isset($this->data[$this->updateTime])) {
  577. $this->data[$this->updateTime] = $this->autoWriteTimestamp($this->updateTime);
  578. }
  579. }
  580. if (false === $this->trigger('BeforeInsert')) {
  581. return false;
  582. }
  583. $this->checkData();
  584. // 检查允许字段
  585. $allowFields = $this->checkAllowFields();
  586. $db = $this->db();
  587. $db->transaction(function () use ($sequence, $allowFields, $db) {
  588. $result = $db->strict(false)
  589. ->field($allowFields)
  590. ->replace($this->replace)
  591. ->sequence($sequence)
  592. ->insert($this->data, true);
  593. // 获取自动增长主键
  594. if ($result) {
  595. $pk = $this->getPk();
  596. if (is_string($pk) && (!isset($this->data[$pk]) || '' == $this->data[$pk])) {
  597. $this->data[$pk] = $result;
  598. }
  599. }
  600. // 关联写入
  601. if (!empty($this->relationWrite)) {
  602. $this->autoRelationInsert();
  603. }
  604. });
  605. // 标记数据已经存在
  606. $this->exists = true;
  607. // 新增回调
  608. $this->trigger('AfterInsert');
  609. return true;
  610. }
  611. /**
  612. * 获取当前的更新条件
  613. * @access public
  614. * @return mixed
  615. */
  616. public function getWhere()
  617. {
  618. $pk = $this->getPk();
  619. if (is_string($pk) && isset($this->origin[$pk])) {
  620. $where = [[$pk, '=', $this->origin[$pk]]];
  621. $this->key = $this->origin[$pk];
  622. } elseif (is_array($pk)) {
  623. foreach ($pk as $field) {
  624. if (isset($this->origin[$field])) {
  625. $where[] = [$field, '=', $this->origin[$field]];
  626. }
  627. }
  628. }
  629. if (empty($where)) {
  630. $where = empty($this->updateWhere) ? null : $this->updateWhere;
  631. }
  632. return $where;
  633. }
  634. /**
  635. * 保存多个数据到当前数据对象
  636. * @access public
  637. * @param iterable $dataSet 数据
  638. * @param boolean $replace 是否自动识别更新和写入
  639. * @return Collection
  640. * @throws \Exception
  641. */
  642. public function saveAll(iterable $dataSet, bool $replace = true): Collection
  643. {
  644. $db = $this->db();
  645. $result = $db->transaction(function () use ($replace, $dataSet) {
  646. $pk = $this->getPk();
  647. if (is_string($pk) && $replace) {
  648. $auto = true;
  649. }
  650. $result = [];
  651. $suffix = $this->getSuffix();
  652. foreach ($dataSet as $key => $data) {
  653. if ($this->exists || (!empty($auto) && isset($data[$pk]))) {
  654. $result[$key] = static::update($data, [], [], $suffix);
  655. } else {
  656. $result[$key] = static::create($data, $this->field, $this->replace, $suffix);
  657. }
  658. }
  659. return $result;
  660. });
  661. return $this->toCollection($result);
  662. }
  663. /**
  664. * 删除当前的记录
  665. * @access public
  666. * @return bool
  667. */
  668. public function delete(): bool
  669. {
  670. if (!$this->exists || $this->isEmpty() || false === $this->trigger('BeforeDelete')) {
  671. return false;
  672. }
  673. // 读取更新条件
  674. $where = $this->getWhere();
  675. $db = $this->db();
  676. $db->transaction(function () use ($where, $db) {
  677. // 删除当前模型数据
  678. $db->where($where)->delete();
  679. // 关联删除
  680. if (!empty($this->relationWrite)) {
  681. $this->autoRelationDelete();
  682. }
  683. });
  684. $this->trigger('AfterDelete');
  685. $this->exists = false;
  686. $this->lazySave = false;
  687. return true;
  688. }
  689. /**
  690. * 写入数据
  691. * @access public
  692. * @param array $data 数据数组
  693. * @param array $allowField 允许字段
  694. * @param bool $replace 使用Replace
  695. * @param string $suffix 数据表后缀
  696. * @return static
  697. */
  698. public static function create(array $data, array $allowField = [], bool $replace = false, string $suffix = ''): Model
  699. {
  700. $model = new static();
  701. if (!empty($allowField)) {
  702. $model->allowField($allowField);
  703. }
  704. if (!empty($suffix)) {
  705. $model->setSuffix($suffix);
  706. }
  707. $model->replace($replace)->save($data);
  708. return $model;
  709. }
  710. /**
  711. * 更新数据
  712. * @access public
  713. * @param array $data 数据数组
  714. * @param mixed $where 更新条件
  715. * @param array $allowField 允许字段
  716. * @param string $suffix 数据表后缀
  717. * @return static
  718. */
  719. public static function update(array $data, $where = [], array $allowField = [], string $suffix = '')
  720. {
  721. $model = new static();
  722. if (!empty($allowField)) {
  723. $model->allowField($allowField);
  724. }
  725. if (!empty($where)) {
  726. $model->setUpdateWhere($where);
  727. }
  728. if (!empty($suffix)) {
  729. $model->setSuffix($suffix);
  730. }
  731. $model->exists(true)->save($data);
  732. return $model;
  733. }
  734. /**
  735. * 删除记录
  736. * @access public
  737. * @param mixed $data 主键列表 支持闭包查询条件
  738. * @param bool $force 是否强制删除
  739. * @return bool
  740. */
  741. public static function destroy($data, bool $force = false): bool
  742. {
  743. if (empty($data) && 0 !== $data) {
  744. return false;
  745. }
  746. $model = new static();
  747. $query = $model->db();
  748. if (is_array($data) && key($data) !== 0) {
  749. $query->where($data);
  750. $data = null;
  751. } elseif ($data instanceof \Closure) {
  752. $data($query);
  753. $data = null;
  754. }
  755. $resultSet = $query->select($data);
  756. foreach ($resultSet as $result) {
  757. $result->force($force)->delete();
  758. }
  759. return true;
  760. }
  761. /**
  762. * 解序列化后处理
  763. */
  764. public function __wakeup()
  765. {
  766. $this->initialize();
  767. }
  768. /**
  769. * 修改器 设置数据对象的值
  770. * @access public
  771. * @param string $name 名称
  772. * @param mixed $value 值
  773. * @return void
  774. */
  775. public function __set(string $name, $value): void
  776. {
  777. $this->setAttr($name, $value);
  778. }
  779. /**
  780. * 获取器 获取数据对象的值
  781. * @access public
  782. * @param string $name 名称
  783. * @return mixed
  784. */
  785. public function __get(string $name)
  786. {
  787. return $this->getAttr($name);
  788. }
  789. /**
  790. * 检测数据对象的值
  791. * @access public
  792. * @param string $name 名称
  793. * @return bool
  794. */
  795. public function __isset(string $name): bool
  796. {
  797. return !is_null($this->getAttr($name));
  798. }
  799. /**
  800. * 销毁数据对象的值
  801. * @access public
  802. * @param string $name 名称
  803. * @return void
  804. */
  805. public function __unset(string $name): void
  806. {
  807. unset($this->data[$name], $this->relation[$name]);
  808. }
  809. // ArrayAccess
  810. public function offsetSet($name, $value)
  811. {
  812. $this->setAttr($name, $value);
  813. }
  814. public function offsetExists($name): bool
  815. {
  816. return $this->__isset($name);
  817. }
  818. public function offsetUnset($name)
  819. {
  820. $this->__unset($name);
  821. }
  822. public function offsetGet($name)
  823. {
  824. return $this->getAttr($name);
  825. }
  826. /**
  827. * 设置不使用的全局查询范围
  828. * @access public
  829. * @param array $scope 不启用的全局查询范围
  830. * @return Query
  831. */
  832. public static function withoutGlobalScope(array $scope = null)
  833. {
  834. $model = new static();
  835. return $model->db($scope);
  836. }
  837. /**
  838. * 切换后缀进行查询
  839. * @access public
  840. * @param string $suffix 切换的表后缀
  841. * @return Model
  842. */
  843. public static function suffix(string $suffix)
  844. {
  845. $model = new static();
  846. $model->setSuffix($suffix);
  847. return $model;
  848. }
  849. /**
  850. * 切换数据库连接进行查询
  851. * @access public
  852. * @param string $connection 数据库连接标识
  853. * @return Model
  854. */
  855. public static function connect(string $connection)
  856. {
  857. $model = new static();
  858. $model->setConnection($connection);
  859. return $model;
  860. }
  861. public function __call($method, $args)
  862. {
  863. if (isset(static::$macro[static::class][$method])) {
  864. return call_user_func_array(static::$macro[static::class][$method]->bindTo($this, static::class), $args);
  865. }
  866. if ('withattr' == strtolower($method)) {
  867. return call_user_func_array([$this, 'withAttribute'], $args);
  868. }
  869. return call_user_func_array([$this->db(), $method], $args);
  870. }
  871. public static function __callStatic($method, $args)
  872. {
  873. if (isset(static::$macro[static::class][$method])) {
  874. return call_user_func_array(static::$macro[static::class][$method]->bindTo(null, static::class), $args);
  875. }
  876. $model = new static();
  877. return call_user_func_array([$model->db(), $method], $args);
  878. }
  879. /**
  880. * 析构方法
  881. * @access public
  882. */
  883. public function __destruct()
  884. {
  885. if ($this->lazySave) {
  886. $this->save();
  887. }
  888. }
  889. }