内核更新

This commit is contained in:
2016-08-16 16:14:36 +08:00
parent ebde0fc13e
commit 81a25be484
25 changed files with 340 additions and 129 deletions

View File

@@ -56,12 +56,12 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
protected $error;
// 字段验证规则
protected $validate;
// 数据表主键 复合主键使用数组定义
// 数据表主键 复合主键使用数组定义 不设置则自动获取
protected $pk;
// 字段属性
// 数据表字段信息 留空则自动获取
protected $field = [];
// 字段类型
protected $fieldType = [];
// 只读字段
protected $readonly = [];
// 显示属性
protected $visible = [];
// 隐藏属性
@@ -97,6 +97,8 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
protected $relation;
// 验证失败是否抛出异常
protected $failException = false;
// 全局查询范围
protected static $useGlobalScope = true;
/**
* 初始化过的模型.
@@ -155,13 +157,23 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
}
if (!empty($this->field)) {
if (true === $this->field) {
$type = $this->db()->getTableInfo('', 'type');
} else {
$type = [];
foreach ((array) $this->field as $key => $val) {
if (is_int($key)) {
$key = $val;
$val = 'varchar';
}
$type[$key] = $val;
}
}
$query->setFieldType($type);
$this->field = array_keys($type);
$query->allowField($this->field);
}
if (!empty($this->fieldType)) {
$query->setFieldType($this->fieldType);
}
if (!empty($this->pk)) {
$query->pk($this->pk);
}
@@ -594,11 +606,10 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
* @access public
* @param array $data 数据
* @param array $where 更新条件
* @param bool $getId 新增的时候是否获取id
* @param bool $replace 是否replace
* @param string $sequence 自增序列名
* @return integer
*/
public function save($data = [], $where = [], $getId = true, $replace = false)
public function save($data = [], $where = [], $sequence = null)
{
if (!empty($data)) {
// 数据自动验证
@@ -616,9 +627,6 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
// 检测字段
if (!empty($this->field)) {
if (true === $this->field) {
$this->field = $this->db()->getTableInfo('', 'fields');
}
foreach ($this->data as $key => $val) {
if (!in_array($key, $this->field)) {
unset($this->data[$key]);
@@ -656,6 +664,15 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
}
}
if (!empty($this->readonly)) {
// 只读字段不允许更新
foreach ($this->readonly as $key => $field) {
if (isset($data[$field])) {
unset($data[$field]);
}
}
}
if (empty($where) && !empty($this->updateWhere)) {
$where = $this->updateWhere;
}
@@ -685,16 +702,15 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
return false;
}
$result = $this->db()->insert($this->data, $replace);
$result = $this->db()->insert($this->data);
// 获取自动增长主键
if ($result && $getId) {
$insertId = $this->db()->getLastInsID();
if ($result) {
$insertId = $this->db()->getLastInsID($sequence);
$pk = $this->getPk();
if (is_string($pk) && $insertId) {
$this->data[$pk] = $insertId;
}
$result = $insertId;
}
// 标记为更新
$this->isUpdate = true;
@@ -713,17 +729,16 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
* 保存多个数据到当前数据对象
* @access public
* @param array $dataSet 数据
* @param bool $replace 是否replace
* @return array|false
*/
public function saveAll($dataSet, $replace = false)
public function saveAll($dataSet)
{
$result = [];
$db = $this->db();
$db->startTrans();
try {
foreach ($dataSet as $key => $data) {
$result[$key] = self::create($data, $replace);
$result[$key] = self::create($data);
}
$db->commit();
return $result;
@@ -741,6 +756,11 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
*/
public function allowField($field)
{
if (true === $field) {
$field = $this->db()->getTableInfo('', 'type');
$this->db()->setFieldType($field);
$field = array_keys($field);
}
$this->field = $field;
return $this;
}
@@ -931,13 +951,12 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
* 写入数据
* @access public
* @param array $data 数据数组
* @param bool $replace 是否replace
* @return $this
*/
public static function create($data = [], $replace = false)
public static function create($data = [])
{
$model = new static();
$model->isUpdate(false)->save($data, [], true, $replace);
$model->isUpdate(false)->save($data, []);
return $model;
}
@@ -1068,6 +1087,19 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
return $model;
}
/**
* 设置是否使用全局查询范围
* @param bool $use 是否启用全局查询范围
* @access public
* @return Model
*/
public static function useGlobalScope($use)
{
$model = new static();
static::$useGlobalScope = $use;
return $model;
}
/**
* 根据关联条件查询当前模型
* @access public
@@ -1105,6 +1137,7 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
$model = new static();
$info = $model->$relation()->getRelationInfo();
switch ($info['type']) {
case Relation::HAS_ONE:
case Relation::HAS_MANY:
$table = $info['model']::getTable();
if (is_array($where)) {
@@ -1287,7 +1320,7 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
{
$query = $this->db();
// 全局作用域
if (method_exists($this, 'base')) {
if (static::$useGlobalScope && method_exists($this, 'base')) {
call_user_func_array('static::base', [ & $query]);
}
if (method_exists($this, 'scope' . $method)) {
@@ -1309,7 +1342,7 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
}
$query = self::$links[$model];
// 全局作用域
if (method_exists($model, 'base')) {
if (static::$useGlobalScope && method_exists($model, 'base')) {
call_user_func_array('static::base', [ & $query]);
}
return call_user_func_array([$query, $method], $params);