代码规范

This commit is contained in:
thinkphp
2016-05-25 21:55:57 +08:00
parent afa64cfd21
commit 0bffec6aaa
3 changed files with 36 additions and 35 deletions

View File

@@ -740,7 +740,7 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
$model = new static(); $model = new static();
$info = $model->$relation()->getRelationInfo(); $info = $model->$relation()->getRelationInfo();
$table = $info['model']::getTable(); $table = $info['model']::getTable();
switch($info['type']){ switch ($info['type']) {
case Relation::HAS_MANY: case Relation::HAS_MANY:
return $model->db()->alias('a') return $model->db()->alias('a')
->join($table . ' b', 'a.' . $info['localKey'] . '=b.' . $info['foreignKey']) ->join($table . ' b', 'a.' . $info['localKey'] . '=b.' . $info['foreignKey'])
@@ -763,7 +763,7 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
{ {
$model = new static(); $model = new static();
$info = $model->$relation()->getRelationInfo(); $info = $model->$relation()->getRelationInfo();
switch($info['type']){ switch ($info['type']) {
case Relation::HAS_MANY: case Relation::HAS_MANY:
$table = $info['model']::getTable(); $table = $info['model']::getTable();
if (is_array($where)) { if (is_array($where)) {
@@ -907,7 +907,8 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
* @param array $alias 别名定义 * @param array $alias 别名定义
* @return \think\db\Query|string * @return \think\db\Query|string
*/ */
public function hasManyThrough($model,$through,$foreignKey='',$throughKey='',$localKey='',$alias=[]){ public function hasManyThrough($model, $through, $foreignKey = '', $throughKey = '', $localKey = '', $alias = [])
{
// 记录当前关联信息 // 记录当前关联信息
$model = $this->parseModel($model); $model = $this->parseModel($model);
$through = $this->parseModel($through); $through = $this->parseModel($through);
@@ -915,7 +916,7 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
$foreignKey = $foreignKey ?: Loader::parseName($this->name) . '_id'; $foreignKey = $foreignKey ?: Loader::parseName($this->name) . '_id';
$name = Loader::parseName(basename(str_replace('\\', '/', $through))); $name = Loader::parseName(basename(str_replace('\\', '/', $through)));
$throughKey = $throughKey ?: $name . '_id'; $throughKey = $throughKey ?: $name . '_id';
return $this->relation()->hasManyThrough($model, $through,$foreignKey,$throughKey, $localKey, $alias); return $this->relation()->hasManyThrough($model, $through, $foreignKey, $throughKey, $localKey, $alias);
} }
/** /**

View File

@@ -356,8 +356,8 @@ abstract class Connection
$result = $this->PDOStatement->execute(); $result = $this->PDOStatement->execute();
// 调试结束 // 调试结束
$this->debug(false); $this->debug(false);
$procedure = 0 === strpos(strtolower(substr(trim($sql),0,4)),'call'); $procedure = 0 === strpos(strtolower(substr(trim($sql), 0, 4)), 'call');
return $this->getResult($class,$procedure); return $this->getResult($class, $procedure);
} catch (\PDOException $e) { } catch (\PDOException $e) {
throw new PDOException($e, $this->config, $this->queryStr); throw new PDOException($e, $this->config, $this->queryStr);
} }
@@ -476,13 +476,13 @@ abstract class Connection
* @param bool $procedure 是否存储过程 * @param bool $procedure 是否存储过程
* @return mixed * @return mixed
*/ */
protected function getResult($class = '',$procedure=false) protected function getResult($class = '', $procedure = false)
{ {
if (true === $class) { if (true === $class) {
// 返回PDOStatement对象处理 // 返回PDOStatement对象处理
return $this->PDOStatement; return $this->PDOStatement;
} }
if($procedure){ if ($procedure) {
return $this->procedure($class); return $this->procedure($class);
} }
$result = $this->PDOStatement->fetchAll($this->fetchType); $result = $this->PDOStatement->fetchAll($this->fetchType);
@@ -512,11 +512,12 @@ abstract class Connection
* @param bool|string $class true 返回PDOStatement 字符串用于指定返回的类名 * @param bool|string $class true 返回PDOStatement 字符串用于指定返回的类名
* @return array * @return array
*/ */
protected function procedure($class){ protected function procedure($class)
{
$item = []; $item = [];
do { do {
$result = $this->getResult($class); $result = $this->getResult($class);
if($result){ if ($result) {
$item[] = $result; $item[] = $result;
} }
} while ($this->PDOStatement->nextRowset()); } while ($this->PDOStatement->nextRowset());

View File

@@ -21,11 +21,10 @@ class Relation
{ {
const HAS_ONE = 1; const HAS_ONE = 1;
const HAS_MANY = 2; const HAS_MANY = 2;
const HAS_MANY_THROUGH= 5; const HAS_MANY_THROUGH = 5;
const BELONGS_TO = 3; const BELONGS_TO = 3;
const BELONGS_TO_MANY = 4; const BELONGS_TO_MANY = 4;
// 父模型对象 // 父模型对象
protected $parent; protected $parent;
/** @var Model 当前关联的模型类 */ /** @var Model 当前关联的模型类 */
@@ -469,7 +468,7 @@ class Relation
* @param array $alias 别名定义 * @param array $alias 别名定义
* @return $this * @return $this
*/ */
public function hasManyThrough($model, $through,$foreignKey,$throughKey, $localKey, $alias) public function hasManyThrough($model, $through, $foreignKey, $throughKey, $localKey, $alias)
{ {
// 记录当前关联信息 // 记录当前关联信息
$this->type = self::HAS_MANY_THROUGH; $this->type = self::HAS_MANY_THROUGH;
@@ -654,7 +653,7 @@ class Relation
if ($this->model) { if ($this->model) {
$model = new $this->model; $model = new $this->model;
$db = $model->db(); $db = $model->db();
switch($this->type){ switch ($this->type) {
case self::HAS_MANY: case self::HAS_MANY:
if (isset($this->parent->{$this->localKey})) { if (isset($this->parent->{$this->localKey})) {
// 关联查询带入关联条件 // 关联查询带入关联条件
@@ -669,10 +668,10 @@ class Relation
$pk = (new $this->model)->getPk(); $pk = (new $this->model)->getPk();
$throughKey = $this->throughKey; $throughKey = $this->throughKey;
$modelTable = $this->parent->getTable(); $modelTable = $this->parent->getTable();
$result = $db->field($alias.'.*')->alias($alias) $result = $db->field($alias . '.*')->alias($alias)
->join($throughTable,$throughTable.'.'.$pk.'='.$alias.'.'.$throughKey) ->join($throughTable, $throughTable . '.' . $pk . '=' . $alias . '.' . $throughKey)
->join($modelTable,$modelTable.'.'.$this->localKey.'='.$throughTable.'.'.$this->foreignKey) ->join($modelTable, $modelTable . '.' . $this->localKey . '=' . $throughTable . '.' . $this->foreignKey)
->where($throughTable.'.'.$this->foreignKey, $this->parent->{$this->localKey}); ->where($throughTable . '.' . $this->foreignKey, $this->parent->{$this->localKey});
break; break;
} }
return call_user_func_array([$db, $method], $args); return call_user_func_array([$db, $method], $args);