Model.php 25 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073
  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. $result = [];
  655. $suffix = $this->getSuffix();
  656. foreach ($dataSet as $key => $data) {
  657. if ($replace) {
  658. $exists = true;
  659. foreach ((array) $pk as $field) {
  660. if (!isset($data[$field])) {
  661. $exists = false;
  662. }
  663. }
  664. }
  665. if ($replace && !empty($exists)) {
  666. $result[$key] = static::update($data, [], [], $suffix);
  667. } else {
  668. $result[$key] = static::create($data, $this->field, $this->replace, $suffix);
  669. }
  670. }
  671. return $result;
  672. });
  673. return $this->toCollection($result);
  674. }
  675. /**
  676. * 删除当前的记录
  677. * @access public
  678. * @return bool
  679. */
  680. public function delete(): bool
  681. {
  682. if (!$this->exists || $this->isEmpty() || false === $this->trigger('BeforeDelete')) {
  683. return false;
  684. }
  685. // 读取更新条件
  686. $where = $this->getWhere();
  687. $db = $this->db();
  688. $db->transaction(function () use ($where, $db) {
  689. // 删除当前模型数据
  690. $db->where($where)->delete();
  691. // 关联删除
  692. if (!empty($this->relationWrite)) {
  693. $this->autoRelationDelete();
  694. }
  695. });
  696. $this->trigger('AfterDelete');
  697. $this->exists = false;
  698. $this->lazySave = false;
  699. return true;
  700. }
  701. /**
  702. * 写入数据
  703. * @access public
  704. * @param array $data 数据数组
  705. * @param array $allowField 允许字段
  706. * @param bool $replace 使用Replace
  707. * @param string $suffix 数据表后缀
  708. * @return static
  709. */
  710. public static function create(array $data, array $allowField = [], bool $replace = false, string $suffix = ''): Model
  711. {
  712. $model = new static();
  713. if (!empty($allowField)) {
  714. $model->allowField($allowField);
  715. }
  716. if (!empty($suffix)) {
  717. $model->setSuffix($suffix);
  718. }
  719. $model->replace($replace)->save($data);
  720. return $model;
  721. }
  722. /**
  723. * 更新数据
  724. * @access public
  725. * @param array $data 数据数组
  726. * @param mixed $where 更新条件
  727. * @param array $allowField 允许字段
  728. * @param string $suffix 数据表后缀
  729. * @return static
  730. */
  731. public static function update(array $data, $where = [], array $allowField = [], string $suffix = '')
  732. {
  733. $model = new static();
  734. if (!empty($allowField)) {
  735. $model->allowField($allowField);
  736. }
  737. if (!empty($where)) {
  738. $model->setUpdateWhere($where);
  739. }
  740. if (!empty($suffix)) {
  741. $model->setSuffix($suffix);
  742. }
  743. $model->exists(true)->save($data);
  744. return $model;
  745. }
  746. /**
  747. * 删除记录
  748. * @access public
  749. * @param mixed $data 主键列表 支持闭包查询条件
  750. * @param bool $force 是否强制删除
  751. * @return bool
  752. */
  753. public static function destroy($data, bool $force = false): bool
  754. {
  755. if (empty($data) && 0 !== $data) {
  756. return false;
  757. }
  758. $model = new static();
  759. $query = $model->db();
  760. if (is_array($data) && key($data) !== 0) {
  761. $query->where($data);
  762. $data = null;
  763. } elseif ($data instanceof \Closure) {
  764. $data($query);
  765. $data = null;
  766. }
  767. $resultSet = $query->select($data);
  768. foreach ($resultSet as $result) {
  769. $result->force($force)->delete();
  770. }
  771. return true;
  772. }
  773. /**
  774. * 解序列化后处理
  775. */
  776. public function __wakeup()
  777. {
  778. $this->initialize();
  779. }
  780. /**
  781. * 修改器 设置数据对象的值
  782. * @access public
  783. * @param string $name 名称
  784. * @param mixed $value 值
  785. * @return void
  786. */
  787. public function __set(string $name, $value): void
  788. {
  789. $this->setAttr($name, $value);
  790. }
  791. /**
  792. * 获取器 获取数据对象的值
  793. * @access public
  794. * @param string $name 名称
  795. * @return mixed
  796. */
  797. public function __get(string $name)
  798. {
  799. return $this->getAttr($name);
  800. }
  801. /**
  802. * 检测数据对象的值
  803. * @access public
  804. * @param string $name 名称
  805. * @return bool
  806. */
  807. public function __isset(string $name): bool
  808. {
  809. return !is_null($this->getAttr($name));
  810. }
  811. /**
  812. * 销毁数据对象的值
  813. * @access public
  814. * @param string $name 名称
  815. * @return void
  816. */
  817. public function __unset(string $name): void
  818. {
  819. unset($this->data[$name],
  820. $this->get[$name],
  821. $this->relation[$name]);
  822. }
  823. // ArrayAccess
  824. #[\ReturnTypeWillChange]
  825. public function offsetSet($name, $value)
  826. {
  827. $this->setAttr($name, $value);
  828. }
  829. #[\ReturnTypeWillChange]
  830. public function offsetExists($name): bool
  831. {
  832. return $this->__isset($name);
  833. }
  834. #[\ReturnTypeWillChange]
  835. public function offsetUnset($name)
  836. {
  837. $this->__unset($name);
  838. }
  839. #[\ReturnTypeWillChange]
  840. public function offsetGet($name)
  841. {
  842. return $this->getAttr($name);
  843. }
  844. /**
  845. * 设置不使用的全局查询范围
  846. * @access public
  847. * @param array $scope 不启用的全局查询范围
  848. * @return Query
  849. */
  850. public static function withoutGlobalScope(array $scope = null)
  851. {
  852. $model = new static();
  853. return $model->db($scope);
  854. }
  855. /**
  856. * 切换后缀进行查询
  857. * @access public
  858. * @param string $suffix 切换的表后缀
  859. * @return Model
  860. */
  861. public static function suffix(string $suffix)
  862. {
  863. $model = new static();
  864. $model->setSuffix($suffix);
  865. return $model;
  866. }
  867. /**
  868. * 切换数据库连接进行查询
  869. * @access public
  870. * @param string $connection 数据库连接标识
  871. * @return Model
  872. */
  873. public static function connect(string $connection)
  874. {
  875. $model = new static();
  876. $model->setConnection($connection);
  877. return $model;
  878. }
  879. public function __call($method, $args)
  880. {
  881. if (isset(static::$macro[static::class][$method])) {
  882. return call_user_func_array(static::$macro[static::class][$method]->bindTo($this, static::class), $args);
  883. }
  884. return call_user_func_array([$this->db(), $method], $args);
  885. }
  886. public static function __callStatic($method, $args)
  887. {
  888. if (isset(static::$macro[static::class][$method])) {
  889. return call_user_func_array(static::$macro[static::class][$method]->bindTo(null, static::class), $args);
  890. }
  891. $model = new static();
  892. return call_user_func_array([$model->db(), $method], $args);
  893. }
  894. /**
  895. * 析构方法
  896. * @access public
  897. */
  898. public function __destruct()
  899. {
  900. if ($this->lazySave) {
  901. $this->save();
  902. }
  903. }
  904. }