Model.php 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068
  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. $this->get = [];
  399. if ($relation) {
  400. $this->relation = [];
  401. }
  402. }
  403. return $this;
  404. }
  405. /**
  406. * 设置数据是否存在
  407. * @access public
  408. * @param bool $exists
  409. * @return $this
  410. */
  411. public function exists(bool $exists = true)
  412. {
  413. $this->exists = $exists;
  414. return $this;
  415. }
  416. /**
  417. * 判断数据是否存在数据库
  418. * @access public
  419. * @return bool
  420. */
  421. public function isExists(): bool
  422. {
  423. return $this->exists;
  424. }
  425. /**
  426. * 判断模型是否为空
  427. * @access public
  428. * @return bool
  429. */
  430. public function isEmpty(): bool
  431. {
  432. return empty($this->data);
  433. }
  434. /**
  435. * 延迟保存当前数据对象
  436. * @access public
  437. * @param array|bool $data 数据
  438. * @return void
  439. */
  440. public function lazySave($data = []): void
  441. {
  442. if (false === $data) {
  443. $this->lazySave = false;
  444. } else {
  445. if (is_array($data)) {
  446. $this->setAttrs($data);
  447. }
  448. $this->lazySave = true;
  449. }
  450. }
  451. /**
  452. * 保存当前数据对象
  453. * @access public
  454. * @param array $data 数据
  455. * @param string $sequence 自增序列名
  456. * @return bool
  457. */
  458. public function save(array $data = [], string $sequence = null): bool
  459. {
  460. // 数据对象赋值
  461. $this->setAttrs($data);
  462. if ($this->isEmpty() || false === $this->trigger('BeforeWrite')) {
  463. return false;
  464. }
  465. $result = $this->exists ? $this->updateData() : $this->insertData($sequence);
  466. if (false === $result) {
  467. return false;
  468. }
  469. // 写入回调
  470. $this->trigger('AfterWrite');
  471. // 重新记录原始数据
  472. $this->origin = $this->data;
  473. $this->get = [];
  474. $this->lazySave = false;
  475. return true;
  476. }
  477. /**
  478. * 检查数据是否允许写入
  479. * @access protected
  480. * @return array
  481. */
  482. protected function checkAllowFields(): array
  483. {
  484. // 检测字段
  485. if (empty($this->field)) {
  486. if (!empty($this->schema)) {
  487. $this->field = array_keys(array_merge($this->schema, $this->jsonType));
  488. } else {
  489. $query = $this->db();
  490. $table = $this->table ? $this->table . $this->suffix : $query->getTable();
  491. $this->field = $query->getConnection()->getTableFields($table);
  492. }
  493. return $this->field;
  494. }
  495. $field = $this->field;
  496. if ($this->autoWriteTimestamp) {
  497. array_push($field, $this->createTime, $this->updateTime);
  498. }
  499. if (!empty($this->disuse)) {
  500. // 废弃字段
  501. $field = array_diff($field, $this->disuse);
  502. }
  503. return $field;
  504. }
  505. /**
  506. * 保存写入数据
  507. * @access protected
  508. * @return bool
  509. */
  510. protected function updateData(): bool
  511. {
  512. // 事件回调
  513. if (false === $this->trigger('BeforeUpdate')) {
  514. return false;
  515. }
  516. $this->checkData();
  517. // 获取有更新的数据
  518. $data = $this->getChangedData();
  519. if (empty($data)) {
  520. // 关联更新
  521. if (!empty($this->relationWrite)) {
  522. $this->autoRelationUpdate();
  523. }
  524. return true;
  525. }
  526. if ($this->autoWriteTimestamp && $this->updateTime) {
  527. // 自动写入更新时间
  528. $data[$this->updateTime] = $this->autoWriteTimestamp();
  529. $this->data[$this->updateTime] = $this->getTimestampValue($data[$this->updateTime]);
  530. }
  531. // 检查允许字段
  532. $allowFields = $this->checkAllowFields();
  533. foreach ($this->relationWrite as $name => $val) {
  534. if (!is_array($val)) {
  535. continue;
  536. }
  537. foreach ($val as $key) {
  538. if (isset($data[$key])) {
  539. unset($data[$key]);
  540. }
  541. }
  542. }
  543. // 模型更新
  544. $db = $this->db();
  545. $db->transaction(function () use ($data, $allowFields, $db) {
  546. $this->key = null;
  547. $where = $this->getWhere();
  548. $result = $db->where($where)
  549. ->strict(false)
  550. ->cache(true)
  551. ->setOption('key', $this->key)
  552. ->field($allowFields)
  553. ->update($data);
  554. $this->checkResult($result);
  555. // 关联更新
  556. if (!empty($this->relationWrite)) {
  557. $this->autoRelationUpdate();
  558. }
  559. });
  560. // 更新回调
  561. $this->trigger('AfterUpdate');
  562. return true;
  563. }
  564. /**
  565. * 新增写入数据
  566. * @access protected
  567. * @param string $sequence 自增名
  568. * @return bool
  569. */
  570. protected function insertData(string $sequence = null): bool
  571. {
  572. if (false === $this->trigger('BeforeInsert')) {
  573. return false;
  574. }
  575. $this->checkData();
  576. $data = $this->data;
  577. // 时间戳自动写入
  578. if ($this->autoWriteTimestamp) {
  579. if ($this->createTime && !isset($data[$this->createTime])) {
  580. $data[$this->createTime] = $this->autoWriteTimestamp();
  581. $this->data[$this->createTime] = $this->getTimestampValue($data[$this->createTime]);
  582. }
  583. if ($this->updateTime && !isset($data[$this->updateTime])) {
  584. $data[$this->updateTime] = $this->autoWriteTimestamp();
  585. $this->data[$this->updateTime] = $this->getTimestampValue($data[$this->updateTime]);
  586. }
  587. }
  588. // 检查允许字段
  589. $allowFields = $this->checkAllowFields();
  590. $db = $this->db();
  591. $db->transaction(function () use ($data, $sequence, $allowFields, $db) {
  592. $result = $db->strict(false)
  593. ->field($allowFields)
  594. ->replace($this->replace)
  595. ->sequence($sequence)
  596. ->insert($data, true);
  597. // 获取自动增长主键
  598. if ($result) {
  599. $pk = $this->getPk();
  600. if (is_string($pk) && (!isset($this->data[$pk]) || '' == $this->data[$pk])) {
  601. unset($this->get[$pk]);
  602. $this->data[$pk] = $result;
  603. }
  604. }
  605. // 关联写入
  606. if (!empty($this->relationWrite)) {
  607. $this->autoRelationInsert();
  608. }
  609. });
  610. // 标记数据已经存在
  611. $this->exists = true;
  612. $this->origin = $this->data;
  613. // 新增回调
  614. $this->trigger('AfterInsert');
  615. return true;
  616. }
  617. /**
  618. * 获取当前的更新条件
  619. * @access public
  620. * @return mixed
  621. */
  622. public function getWhere()
  623. {
  624. $pk = $this->getPk();
  625. if (is_string($pk) && isset($this->origin[$pk])) {
  626. $where = [[$pk, '=', $this->origin[$pk]]];
  627. $this->key = $this->origin[$pk];
  628. } elseif (is_array($pk)) {
  629. foreach ($pk as $field) {
  630. if (isset($this->origin[$field])) {
  631. $where[] = [$field, '=', $this->origin[$field]];
  632. }
  633. }
  634. }
  635. if (empty($where)) {
  636. $where = empty($this->updateWhere) ? null : $this->updateWhere;
  637. }
  638. return $where;
  639. }
  640. /**
  641. * 保存多个数据到当前数据对象
  642. * @access public
  643. * @param iterable $dataSet 数据
  644. * @param boolean $replace 是否自动识别更新和写入
  645. * @return Collection
  646. * @throws \Exception
  647. */
  648. public function saveAll(iterable $dataSet, bool $replace = true): Collection
  649. {
  650. $db = $this->db();
  651. $result = $db->transaction(function () use ($replace, $dataSet) {
  652. $pk = $this->getPk();
  653. if (is_string($pk) && $replace) {
  654. $auto = true;
  655. }
  656. $result = [];
  657. $suffix = $this->getSuffix();
  658. foreach ($dataSet as $key => $data) {
  659. if ($this->exists || (!empty($auto) && isset($data[$pk]))) {
  660. $result[$key] = static::update($data, [], [], $suffix);
  661. } else {
  662. $result[$key] = static::create($data, $this->field, $this->replace, $suffix);
  663. }
  664. }
  665. return $result;
  666. });
  667. return $this->toCollection($result);
  668. }
  669. /**
  670. * 删除当前的记录
  671. * @access public
  672. * @return bool
  673. */
  674. public function delete(): bool
  675. {
  676. if (!$this->exists || $this->isEmpty() || false === $this->trigger('BeforeDelete')) {
  677. return false;
  678. }
  679. // 读取更新条件
  680. $where = $this->getWhere();
  681. $db = $this->db();
  682. $db->transaction(function () use ($where, $db) {
  683. // 删除当前模型数据
  684. $db->where($where)->delete();
  685. // 关联删除
  686. if (!empty($this->relationWrite)) {
  687. $this->autoRelationDelete();
  688. }
  689. });
  690. $this->trigger('AfterDelete');
  691. $this->exists = false;
  692. $this->lazySave = false;
  693. return true;
  694. }
  695. /**
  696. * 写入数据
  697. * @access public
  698. * @param array $data 数据数组
  699. * @param array $allowField 允许字段
  700. * @param bool $replace 使用Replace
  701. * @param string $suffix 数据表后缀
  702. * @return static
  703. */
  704. public static function create(array $data, array $allowField = [], bool $replace = false, string $suffix = ''): Model
  705. {
  706. $model = new static();
  707. if (!empty($allowField)) {
  708. $model->allowField($allowField);
  709. }
  710. if (!empty($suffix)) {
  711. $model->setSuffix($suffix);
  712. }
  713. $model->replace($replace)->save($data);
  714. return $model;
  715. }
  716. /**
  717. * 更新数据
  718. * @access public
  719. * @param array $data 数据数组
  720. * @param mixed $where 更新条件
  721. * @param array $allowField 允许字段
  722. * @param string $suffix 数据表后缀
  723. * @return static
  724. */
  725. public static function update(array $data, $where = [], array $allowField = [], string $suffix = '')
  726. {
  727. $model = new static();
  728. if (!empty($allowField)) {
  729. $model->allowField($allowField);
  730. }
  731. if (!empty($where)) {
  732. $model->setUpdateWhere($where);
  733. }
  734. if (!empty($suffix)) {
  735. $model->setSuffix($suffix);
  736. }
  737. $model->exists(true)->save($data);
  738. return $model;
  739. }
  740. /**
  741. * 删除记录
  742. * @access public
  743. * @param mixed $data 主键列表 支持闭包查询条件
  744. * @param bool $force 是否强制删除
  745. * @return bool
  746. */
  747. public static function destroy($data, bool $force = false): bool
  748. {
  749. if (empty($data) && 0 !== $data) {
  750. return false;
  751. }
  752. $model = new static();
  753. $query = $model->db();
  754. if (is_array($data) && key($data) !== 0) {
  755. $query->where($data);
  756. $data = null;
  757. } elseif ($data instanceof \Closure) {
  758. $data($query);
  759. $data = null;
  760. }
  761. $resultSet = $query->select($data);
  762. foreach ($resultSet as $result) {
  763. $result->force($force)->delete();
  764. }
  765. return true;
  766. }
  767. /**
  768. * 解序列化后处理
  769. */
  770. public function __wakeup()
  771. {
  772. $this->initialize();
  773. }
  774. /**
  775. * 修改器 设置数据对象的值
  776. * @access public
  777. * @param string $name 名称
  778. * @param mixed $value 值
  779. * @return void
  780. */
  781. public function __set(string $name, $value): void
  782. {
  783. $this->setAttr($name, $value);
  784. }
  785. /**
  786. * 获取器 获取数据对象的值
  787. * @access public
  788. * @param string $name 名称
  789. * @return mixed
  790. */
  791. public function __get(string $name)
  792. {
  793. return $this->getAttr($name);
  794. }
  795. /**
  796. * 检测数据对象的值
  797. * @access public
  798. * @param string $name 名称
  799. * @return bool
  800. */
  801. public function __isset(string $name): bool
  802. {
  803. return !is_null($this->getAttr($name));
  804. }
  805. /**
  806. * 销毁数据对象的值
  807. * @access public
  808. * @param string $name 名称
  809. * @return void
  810. */
  811. public function __unset(string $name): void
  812. {
  813. unset($this->data[$name],
  814. $this->get[$name],
  815. $this->relation[$name]);
  816. }
  817. // ArrayAccess
  818. public function offsetSet($name, $value)
  819. {
  820. $this->setAttr($name, $value);
  821. }
  822. public function offsetExists($name): bool
  823. {
  824. return $this->__isset($name);
  825. }
  826. public function offsetUnset($name)
  827. {
  828. $this->__unset($name);
  829. }
  830. public function offsetGet($name)
  831. {
  832. return $this->getAttr($name);
  833. }
  834. /**
  835. * 设置不使用的全局查询范围
  836. * @access public
  837. * @param array $scope 不启用的全局查询范围
  838. * @return Query
  839. */
  840. public static function withoutGlobalScope(array $scope = null)
  841. {
  842. $model = new static();
  843. return $model->db($scope);
  844. }
  845. /**
  846. * 切换后缀进行查询
  847. * @access public
  848. * @param string $suffix 切换的表后缀
  849. * @return Model
  850. */
  851. public static function suffix(string $suffix)
  852. {
  853. $model = new static();
  854. $model->setSuffix($suffix);
  855. return $model;
  856. }
  857. /**
  858. * 切换数据库连接进行查询
  859. * @access public
  860. * @param string $connection 数据库连接标识
  861. * @return Model
  862. */
  863. public static function connect(string $connection)
  864. {
  865. $model = new static();
  866. $model->setConnection($connection);
  867. return $model;
  868. }
  869. public function __call($method, $args)
  870. {
  871. if (isset(static::$macro[static::class][$method])) {
  872. return call_user_func_array(static::$macro[static::class][$method]->bindTo($this, static::class), $args);
  873. }
  874. if ('withattr' == strtolower($method)) {
  875. return call_user_func_array([$this, 'withAttribute'], $args);
  876. }
  877. return call_user_func_array([$this->db(), $method], $args);
  878. }
  879. public static function __callStatic($method, $args)
  880. {
  881. if (isset(static::$macro[static::class][$method])) {
  882. return call_user_func_array(static::$macro[static::class][$method]->bindTo(null, static::class), $args);
  883. }
  884. $model = new static();
  885. return call_user_func_array([$model->db(), $method], $args);
  886. }
  887. /**
  888. * 析构方法
  889. * @access public
  890. */
  891. public function __destruct()
  892. {
  893. if ($this->lazySave) {
  894. $this->save();
  895. }
  896. }
  897. }