Model.php 25 KB

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