mirror of
https://gitee.com/fastadminnet/framework.git
synced 2026-07-01 20:52:48 +08:00
改进Model类支持 按照查询条件 更新
This commit is contained in:
@@ -64,6 +64,8 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
|
||||
protected $type = [];
|
||||
// 是否为更新数据
|
||||
protected $isUpdate = false;
|
||||
// 更新条件
|
||||
protected $updateWhere;
|
||||
// 当前执行的关联对象
|
||||
protected $relation;
|
||||
|
||||
@@ -287,6 +289,17 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
|
||||
}
|
||||
}
|
||||
|
||||
if (empty($where) && !empty($this->updateWhere)) {
|
||||
$where = $this->updateWhere;
|
||||
}
|
||||
|
||||
if (!empty($where)) {
|
||||
$pk = $this->getPk();
|
||||
if (is_string($pk) && isset($data[$pk])) {
|
||||
unset($data[$pk]);
|
||||
}
|
||||
}
|
||||
|
||||
$result = self::db()->where($where)->update($data);
|
||||
|
||||
// 更新回调
|
||||
@@ -353,11 +366,15 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
|
||||
* 是否为更新数据
|
||||
* @access public
|
||||
* @param bool $update
|
||||
* @param mixed $where
|
||||
* @return $this
|
||||
*/
|
||||
public function isUpdate($update = true)
|
||||
public function isUpdate($update = true, $where = null)
|
||||
{
|
||||
$this->isUpdate = $update;
|
||||
if (!empty($where)) {
|
||||
$this->updateWhere = $where;
|
||||
}
|
||||
return $this;
|
||||
}
|
||||
|
||||
|
||||
@@ -1337,7 +1337,7 @@ class Query
|
||||
if (!empty($options['model'])) {
|
||||
// 返回模型对象
|
||||
$data = new $options['model']($data);
|
||||
$data->isUpdate(true);
|
||||
$data->isUpdate(true, $options['where']['AND']);
|
||||
// 关联查询
|
||||
if (!empty($options['relation'])) {
|
||||
$data->relationQuery($options['relation']);
|
||||
|
||||
Reference in New Issue
Block a user