Model.php 23 KB

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