From 3691114beff8e68c39853b573942d7f41b110e7d Mon Sep 17 00:00:00 2001 From: thinkphp Date: Thu, 7 Apr 2016 10:50:24 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=B9=E8=BF=9Bmodel=E7=B1=BB=20=E5=A2=9E?= =?UTF-8?q?=E5=8A=A0before=5Fwrite=20=E5=92=8C=20after=5Fwrite=20=E4=BA=8B?= =?UTF-8?q?=E4=BB=B6=20save=E6=96=B9=E6=B3=95=E6=88=90=E5=8A=9F=E5=90=8E?= =?UTF-8?q?=20=E8=87=AA=E5=8A=A8=E5=AE=8C=E6=88=90=E6=93=8D=E4=BD=9C?= =?UTF-8?q?=E4=BC=9A=E7=9B=B4=E6=8E=A5=E5=BD=B1=E5=93=8D=E6=95=B0=E6=8D=AE?= =?UTF-8?q?=E5=AF=B9=E8=B1=A1=E5=80=BC=20=E5=8F=96=E6=B6=88=20auto=20autoi?= =?UTF-8?q?nsert=20autoupdate=20=E6=96=B9=E6=B3=95=20=E6=94=B9=E6=88=90?= =?UTF-8?q?=E7=9B=B4=E6=8E=A5=E5=B1=9E=E6=80=A7=E5=AE=9A=E4=B9=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- library/think/Model.php | 94 ++++++++++++++++------------------------- 1 file changed, 37 insertions(+), 57 deletions(-) diff --git a/library/think/Model.php b/library/think/Model.php index 4f84e87b..5e88d12e 100644 --- a/library/think/Model.php +++ b/library/think/Model.php @@ -256,6 +256,10 @@ abstract class Model implements \JsonSerializable, \ArrayAccess return false; } + if (false === $this->trigger('before_write', $this)) { + return false; + } + // 数据自动完成 foreach ($this->auto as $name => $rule) { if (!in_array($name, $this->change)) { @@ -290,8 +294,11 @@ abstract class Model implements \JsonSerializable, \ArrayAccess } $result = $db->update($data); + // 赋值数据对象值 + $this->data = $data; + // 更新回调 $this->trigger('after_update', $this); - return $result; + } else { if (false === $this->trigger('before_insert', $this)) { @@ -308,13 +315,21 @@ abstract class Model implements \JsonSerializable, \ArrayAccess // 获取自动增长主键 $insertId = self::db()->getLastInsID(); if (is_string($this->pk) && $insertId) { - $data[$this->pk] = $insertId; - $this->data[$this->pk] = $insertId; + $data[$this->pk] = $insertId; } + $result = $insertId ?: $result; + // 数据对象赋值 + $this->data = $data; + // 新增回调 $this->trigger('after_insert', $this); - return $insertId ?: $result; + } + + // 写入回调 + $this->trigger('after_write', $this); + + return $result; } /** @@ -356,57 +371,6 @@ abstract class Model implements \JsonSerializable, \ArrayAccess return $this; } - /** - * 设置字段自动完成(包括新增和更新) - * @access public - * @param string $field 字段名或者数组规则 - * @param array|null $rule 完成规则 - * @return Model - */ - public function auto($field, $rule = null) - { - if (is_array($field)) { - $this->auto = array_merge($this->auto, $field); - } else { - $this->auto[$field] = $rule; - } - return $this; - } - - /** - * 设置写入自动完成 - * @access public - * @param string $field 字段名或者数组规则 - * @param array|null $rule 完成规则 - * @return Model - */ - public function autoInsert($field, $rule = null) - { - if (is_array($field)) { - $this->insert = array_merge($this->insert, $field); - } else { - $this->insert[$field] = $rule; - } - return $this; - } - - /** - * 设置更新自动完成 - * @access public - * @param string $field 字段名或者数组规则 - * @param array|null $rule 完成规则 - * @return Model - */ - public function autoUpdate($field, $rule = null) - { - if (is_array($field)) { - $this->update = array_merge($this->update, $field); - } else { - $this->update[$field] = $rule; - } - return $this; - } - /** * 数据自动验证 * @access protected @@ -544,11 +508,26 @@ abstract class Model implements \JsonSerializable, \ArrayAccess * 查找多条记录 * @access public * @param mixed $data 主键列表 + * @param false|string|array $load 预载入模型 * @return array|string */ - public static function all($data = []) + public static function all($data = [], $load = false) { - return self::db()->select($data); + $resultSet = self::db()->select($data); + if ($load) { + // 预载入关联模型 + if (is_string($load)) { + $load = (array) $load; + } + + foreach ($resultSet as &$result) { + foreach ($load as $relation) { + $model = new static($result); + $result->$relation = $model->$relation(); + } + } + } + return $resultSet; } /** @@ -598,6 +577,7 @@ abstract class Model implements \JsonSerializable, \ArrayAccess $foreignKey = $foreignKey ?: $this->pk; $localKey = $localKey ?: Loader::parseName(basename(str_replace('\\', '/', $model))) . '_id'; $this->relation = self::BELONGS_TO; + return $model::where($foreignKey, $this->data[$localKey]); }