改进Query类的find和select方法的默认值

This commit is contained in:
thinkphp
2016-06-21 10:58:19 +08:00
parent c4669f142d
commit 850376dfe7
3 changed files with 27 additions and 27 deletions

View File

@@ -248,7 +248,7 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
$value = $_SERVER['REQUEST_TIME']; $value = $_SERVER['REQUEST_TIME'];
break; break;
} }
} elseif (is_string($this->autoWriteTimestamp) && in_array(strtolower($this->autoWriteTimestamp),['datetime','date','timestamp'])) { } elseif (is_string($this->autoWriteTimestamp) && in_array(strtolower($this->autoWriteTimestamp), ['datetime', 'date', 'timestamp'])) {
$value = date($this->dateFormat, $_SERVER['REQUEST_TIME']); $value = date($this->dateFormat, $_SERVER['REQUEST_TIME']);
} else { } else {
$value = $_SERVER['REQUEST_TIME']; $value = $_SERVER['REQUEST_TIME'];
@@ -282,9 +282,9 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
*/ */
protected function writeTransform($value, $type) protected function writeTransform($value, $type)
{ {
if(is_array($type)){ if (is_array($type)) {
list($type,$param) = $type; list($type, $param) = $type;
}elseif (strpos($type, ':')) { } elseif (strpos($type, ':')) {
list($type, $param) = explode(':', $type, 2); list($type, $param) = explode(':', $type, 2);
} }
switch ($type) { switch ($type) {
@@ -318,7 +318,7 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
case 'array': case 'array':
$value = (array) $value; $value = (array) $value;
case 'json': case 'json':
$option = !empty($param) ? (int)$param : JSON_UNESCAPED_UNICODE; $option = !empty($param) ? (int) $param : JSON_UNESCAPED_UNICODE;
$value = json_encode($value, $option); $value = json_encode($value, $option);
break; break;
case 'serialize': case 'serialize':
@@ -363,9 +363,9 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
*/ */
protected function readTransform($value, $type) protected function readTransform($value, $type)
{ {
if(is_array($type)){ if (is_array($type)) {
list($type,$param) = $type; list($type, $param) = $type;
}elseif (strpos($type, ':')) { } elseif (strpos($type, ':')) {
list($type, $param) = explode(':', $type, 2); list($type, $param) = explode(':', $type, 2);
} }
switch ($type) { switch ($type) {
@@ -636,7 +636,7 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
public function saveAll($dataSet) public function saveAll($dataSet)
{ {
$result = 0; $result = 0;
$db = $this->db(); $db = $this->db();
$db->startTrans(); $db->startTrans();
try { try {
foreach ($dataSet as $data) { foreach ($dataSet as $data) {
@@ -881,7 +881,7 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
* @return static * @return static
* @throws exception\DbException * @throws exception\DbException
*/ */
public static function get($data = '', $with = [], $cache = false) public static function get($data = null, $with = [], $cache = false)
{ {
$query = self::parseQuery($data, $with, $cache); $query = self::parseQuery($data, $with, $cache);
return $query->find($data); return $query->find($data);
@@ -896,7 +896,7 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
* @return static[]|false * @return static[]|false
* @throws exception\DbException * @throws exception\DbException
*/ */
public static function all($data = [], $with = [], $cache = false) public static function all($data = null, $with = [], $cache = false)
{ {
$query = self::parseQuery($data, $with, $cache); $query = self::parseQuery($data, $with, $cache);
return $query->select($data); return $query->select($data);
@@ -915,13 +915,13 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
$result = self::with($with)->cache($cache); $result = self::with($with)->cache($cache);
if (is_array($data) && key($data) !== 0) { if (is_array($data) && key($data) !== 0) {
$result = $result->where($data); $result = $result->where($data);
$data = []; $data = null;
} elseif ($data instanceof \Closure) { } elseif ($data instanceof \Closure) {
call_user_func_array($data, [ & $result]); call_user_func_array($data, [ & $result]);
$data = []; $data = null;
} elseif ($data instanceof Query) { } elseif ($data instanceof Query) {
$result = $data->with($with)->cache($cache); $result = $data->with($with)->cache($cache);
$data = []; $data = null;
} }
return $result; return $result;
} }
@@ -938,10 +938,10 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
$query = $model->db(); $query = $model->db();
if (is_array($data) && key($data) !== 0) { if (is_array($data) && key($data) !== 0) {
$query->where($data); $query->where($data);
$data = []; $data = null;
} elseif ($data instanceof \Closure) { } elseif ($data instanceof \Closure) {
call_user_func_array($data, [ & $query]); call_user_func_array($data, [ & $query]);
$data = []; $data = null;
} }
$resultSet = $query->select($data); $resultSet = $query->select($data);
$count = 0; $count = 0;

View File

@@ -1703,13 +1703,13 @@ class Query
* @throws Exception * @throws Exception
* @throws PDOException * @throws PDOException
*/ */
public function select($data = []) public function select($data = null)
{ {
if ($data instanceof Query) { if ($data instanceof Query) {
return $data->select(); return $data->select();
} elseif ($data instanceof \Closure) { } elseif ($data instanceof \Closure) {
call_user_func_array($data, [ & $this]); call_user_func_array($data, [ & $this]);
$data = []; $data = null;
} }
// 分析查询表达式 // 分析查询表达式
$options = $this->parseExpress(); $options = $this->parseExpress();
@@ -1717,7 +1717,7 @@ class Query
if (false === $data) { if (false === $data) {
// 用于子查询 不查询只返回SQL // 用于子查询 不查询只返回SQL
$options['fetch_sql'] = true; $options['fetch_sql'] = true;
} elseif (!empty($data)) { } elseif (!is_null($data)) {
// 主键条件分析 // 主键条件分析
$this->parsePkWhere($data, $options); $this->parsePkWhere($data, $options);
} }
@@ -1790,18 +1790,18 @@ class Query
* @throws Exception * @throws Exception
* @throws PDOException * @throws PDOException
*/ */
public function find($data = []) public function find($data = null)
{ {
if ($data instanceof Query) { if ($data instanceof Query) {
return $data->find(); return $data->find();
} elseif ($data instanceof \Closure) { } elseif ($data instanceof \Closure) {
call_user_func_array($data, [ & $this]); call_user_func_array($data, [ & $this]);
$data = []; $data = null;
} }
// 分析查询表达式 // 分析查询表达式
$options = $this->parseExpress(); $options = $this->parseExpress();
if (!empty($data) || 0 === $data) { if (!is_null($data)) {
// AR模式分析主键条件 // AR模式分析主键条件
$this->parsePkWhere($data, $options); $this->parsePkWhere($data, $options);
} }

View File

@@ -44,7 +44,7 @@ class Merge extends Model
* @param bool $cache 是否缓存 * @param bool $cache 是否缓存
* @return \think\Model * @return \think\Model
*/ */
public static function get($data = '', $with = [], $cache = false) public static function get($data = null, $with = [], $cache = false)
{ {
$query = self::parseQuery($data, $with, $cache); $query = self::parseQuery($data, $with, $cache);
$query = self::attachQuery($query); $query = self::attachQuery($query);
@@ -106,7 +106,7 @@ class Merge extends Model
* @param string $with 关联预查询 * @param string $with 关联预查询
* @return array|false|string * @return array|false|string
*/ */
public static function all($data = [], $with = [], $cache = false) public static function all($data = null, $with = [], $cache = false)
{ {
$query = self::parseQuery($data, $with, $cache); $query = self::parseQuery($data, $with, $cache);
$query = self::attachQuery($query); $query = self::attachQuery($query);