代码规范

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,14 +740,14 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
$model = new static();
$info = $model->$relation()->getRelationInfo();
$table = $info['model']::getTable();
switch($info['type']){
switch ($info['type']) {
case Relation::HAS_MANY:
return $model->db()->alias('a')
->join($table . ' b', 'a.' . $info['localKey'] . '=b.' . $info['foreignKey'])
->group('b.' . $info['foreignKey'])
->having('count(' . $id . ')' . $operator . $count);
->having('count(' . $id . ')' . $operator . $count);
case Relation::HAS_MANY_THROUGH:
// TODO
// TODO
}
}
@@ -763,7 +763,7 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
{
$model = new static();
$info = $model->$relation()->getRelationInfo();
switch($info['type']){
switch ($info['type']) {
case Relation::HAS_MANY:
$table = $info['model']::getTable();
if (is_array($where)) {
@@ -777,9 +777,9 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
return $model->db()->alias('a')
->field('a.*')
->join($table . ' b', 'a.' . $info['localKey'] . '=b.' . $info['foreignKey'])
->where($where);
->where($where);
case Relation::HAS_MANY_THROUGH:
// TODO
// TODO
}
}
@@ -907,7 +907,8 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
* @param array $alias 别名定义
* @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);
$through = $this->parseModel($through);
@@ -915,7 +916,7 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
$foreignKey = $foreignKey ?: Loader::parseName($this->name) . '_id';
$name = Loader::parseName(basename(str_replace('\\', '/', $through)));
$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();
// 调试结束
$this->debug(false);
$procedure = 0 === strpos(strtolower(substr(trim($sql),0,4)),'call');
return $this->getResult($class,$procedure);
$procedure = 0 === strpos(strtolower(substr(trim($sql), 0, 4)), 'call');
return $this->getResult($class, $procedure);
} catch (\PDOException $e) {
throw new PDOException($e, $this->config, $this->queryStr);
}
@@ -476,13 +476,13 @@ abstract class Connection
* @param bool $procedure 是否存储过程
* @return mixed
*/
protected function getResult($class = '',$procedure=false)
protected function getResult($class = '', $procedure = false)
{
if (true === $class) {
// 返回PDOStatement对象处理
return $this->PDOStatement;
}
if($procedure){
if ($procedure) {
return $this->procedure($class);
}
$result = $this->PDOStatement->fetchAll($this->fetchType);
@@ -512,11 +512,12 @@ abstract class Connection
* @param bool|string $class true 返回PDOStatement 字符串用于指定返回的类名
* @return array
*/
protected function procedure($class){
protected function procedure($class)
{
$item = [];
do {
$result = $this->getResult($class);
if($result){
if ($result) {
$item[] = $result;
}
} while ($this->PDOStatement->nextRowset());

View File

@@ -19,12 +19,11 @@ use think\model\Pivot;
class Relation
{
const HAS_ONE = 1;
const HAS_MANY = 2;
const HAS_MANY_THROUGH= 5;
const BELONGS_TO = 3;
const BELONGS_TO_MANY = 4;
const HAS_ONE = 1;
const HAS_MANY = 2;
const HAS_MANY_THROUGH = 5;
const BELONGS_TO = 3;
const BELONGS_TO_MANY = 4;
// 父模型对象
protected $parent;
@@ -37,7 +36,7 @@ class Relation
// 关联表外键
protected $foreignKey;
// 中间关联表外键
protected $throughKey;
protected $throughKey;
// 关联表主键
protected $localKey;
// 数据表别名
@@ -469,7 +468,7 @@ class Relation
* @param array $alias 别名定义
* @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;
@@ -654,25 +653,25 @@ class Relation
if ($this->model) {
$model = new $this->model;
$db = $model->db();
switch($this->type){
switch ($this->type) {
case self::HAS_MANY:
if (isset($this->parent->{$this->localKey})) {
// 关联查询带入关联条件
$db->where($this->foreignKey, $this->parent->{$this->localKey});
}
}
break;
case self::HAS_MANY_THROUGH:
$through = $this->middle;
$model = $this->model;
$alias = Loader::parseName(basename(str_replace('\\', '/', $model)));
$throughTable = $through::getTable();
$pk = (new $this->model)->getPk();
$throughKey = $this->throughKey;
$modelTable = $this->parent->getTable();
$result = $db->field($alias.'.*')->alias($alias)
->join($throughTable,$throughTable.'.'.$pk.'='.$alias.'.'.$throughKey)
->join($modelTable,$modelTable.'.'.$this->localKey.'='.$throughTable.'.'.$this->foreignKey)
->where($throughTable.'.'.$this->foreignKey, $this->parent->{$this->localKey});
$through = $this->middle;
$model = $this->model;
$alias = Loader::parseName(basename(str_replace('\\', '/', $model)));
$throughTable = $through::getTable();
$pk = (new $this->model)->getPk();
$throughKey = $this->throughKey;
$modelTable = $this->parent->getTable();
$result = $db->field($alias . '.*')->alias($alias)
->join($throughTable, $throughTable . '.' . $pk . '=' . $alias . '.' . $throughKey)
->join($modelTable, $modelTable . '.' . $this->localKey . '=' . $throughTable . '.' . $this->foreignKey)
->where($throughTable . '.' . $this->foreignKey, $this->parent->{$this->localKey});
break;
}
return call_user_func_array([$db, $method], $args);