Skip to content

Commit

Permalink
with关联查询支持缓存
Browse files Browse the repository at this point in the history
改进JSON数据操作
改进软删除 支持动态设置
模型类增加getError方法
模型类toCollection方法增加参数指定数据集类
  • Loading branch information
liu21st committed Jan 19, 2018
1 parent 1d63709 commit 8bdca98
Show file tree
Hide file tree
Showing 16 changed files with 475 additions and 200 deletions.
20 changes: 18 additions & 2 deletions src/Model.php
Expand Up @@ -98,6 +98,12 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
*/
protected $queryInstance;

/**
* 错误信息
* @var mixed
*/
protected $error;

/**
* 架构函数
* @access public
Expand Down Expand Up @@ -185,7 +191,7 @@ protected function buildQuery()
{
// 设置当前模型 确保查询返回模型对象
$class = $this->query;
$query = (new $class())->connect($this->connection)->model($this);
$query = (new $class())->connect($this->connection)->model($this)->json($this->json);

// 设置当前数据表和模型名
if (!empty($this->table)) {
Expand Down Expand Up @@ -821,7 +827,7 @@ public static function all($data = null, $with = [], $cache = false)
*/
protected static function parseQuery(&$data, $with, $cache)
{
$result = self::with($with)->cache($cache);
$result = self::with($with, true === $cache ? true : false)->cache($cache);

if (is_array($data) && key($data) !== 0) {
$result = $result->where(self::parseWhere($data));
Expand Down Expand Up @@ -872,6 +878,16 @@ public static function destroy($data)
return $count;
}

/**
* 获取错误信息
* @access public
* @return mixed
*/
public function getError()
{
return $this->error;
}

/**
* 解序列化后处理
*/
Expand Down
34 changes: 27 additions & 7 deletions src/db/Builder.php
Expand Up @@ -36,7 +36,7 @@ abstract class Builder
];

// SQL表达式
protected $selectSql = 'SELECT%DISTINCT% %FIELD% FROM %TABLE%%FORCE%%UNION%%JOIN%%WHERE%%GROUP%%HAVING%%ORDER%%LIMIT%%LOCK%%COMMENT%';
protected $selectSql = 'SELECT%DISTINCT% %FIELD% FROM %TABLE%%FORCE%%JOIN%%WHERE%%GROUP%%HAVING%%UNION%%ORDER%%LIMIT%%LOCK%%COMMENT%';

protected $insertSql = '%INSERT% INTO %TABLE% (%FIELD%) VALUES (%DATA%) %COMMENT%';

Expand Down Expand Up @@ -115,14 +115,18 @@ protected function parseData(Query $query, $data = [], $fields = [], $bind = [],
foreach ($data as $key => $val) {
$item = $this->parseKey($query, $key);

if (is_object($val) && method_exists($val, '__toString')) {
if (!is_scalar($val) && (in_array($key, (array) $query->getOptions('json')) || 'json' == $this->connection->getFieldsType($options['table'], $key))) {
$val = json_encode($val);
} elseif (is_object($val) && method_exists($val, '__toString')) {
// 对象数据写入
$val = $val->__toString();
} elseif (is_array($val) && 'json' == $this->connection->getFieldsType($options['table'], $key)) {
$val = json_encode($val);
}

if (false === strpos($key, '.') && !in_array($key, $fields, true)) {
if (false !== strpos($key, '->')) {
list($key, $name) = explode('->', $key);
$item = $this->parseKey($query, $key);
$result[$item] = 'json_set(' . $item . ', \'$.' . $name . '\', ' . $this->parseDataBind($query, $key, $val, $bind, $suffix) . ')';
} elseif (false === strpos($key, '.') && !in_array($key, $fields, true)) {
if ($options['strict']) {
throw new Exception('fields not exists:[' . $key . ']');
}
Expand Down Expand Up @@ -165,7 +169,7 @@ protected function parseDataBind(Query $query, $key, $data, $bind = [], $suffix
if (0 === strpos($data, ':') && $query->isBind(substr($data, 1))) {
return $data;
} else {
$key = str_replace('.', '_', $key);
$key = str_replace(['.', '->'], '_', $key);
$name = 'data__' . $key . $suffix;
$query->bind($name, $data, isset($bind[$key]) ? $bind[$key] : PDO::PARAM_STR);
return ':' . $name;
Expand Down Expand Up @@ -822,7 +826,23 @@ protected function parseOrder(Query $query, $order)
$array = [];

foreach ($order as $key => $val) {
if (is_numeric($key)) {
if (is_array($val)) {
if (isset($val['sort'])) {
$sort = ' ' . $val['sort'];
unset($val['sort']);
} else {
$sort = '';
}

$options = $query->getOptions();
$bind = $this->connection->getFieldsBind($options['table']);

foreach ($val as $k => $item) {
$val[$k] = $this->parseDataBind($query, $key, $item, $bind, $k);
}

$array[] = 'field(' . $this->parseKey($query, $key) . ',' . implode(',', $val) . ')' . $sort;
} elseif (is_numeric($key)) {
if ('[rand]' == $val) {
$array[] = $this->parseRand($query);
} elseif (false === strpos($val, '(')) {
Expand Down
2 changes: 2 additions & 0 deletions src/db/Connection.php
Expand Up @@ -980,6 +980,8 @@ public function insert(Query $query, $replace = false, $getLastInsID = false, $s
* @param bool $replace 是否replace
* @param integer $limit 每次写入数据限制
* @return integer|string
* @throws \Exception
* @throws \Throwable
*/
public function insertAll(Query $query, $dataSet = [], $replace = false, $limit = null)
{
Expand Down

0 comments on commit 8bdca98

Please sign in to comment.