This commit is contained in:
thinkphp
2017-01-18 22:01:55 +08:00
6 changed files with 32 additions and 16 deletions

View File

@@ -14,6 +14,7 @@
//-------------------------
use think\Cache;
use think\Collection;
use think\Config;
use think\Cookie;
use think\Db;
@@ -23,7 +24,6 @@ use think\exception\HttpResponseException;
use think\Lang;
use think\Loader;
use think\Log;
use think\model\Collection;
use think\Request;
use think\Response;
use think\Session;
@@ -330,7 +330,7 @@ if (!function_exists('cookie')) {
Cookie::clear($value);
} elseif ('' === $value) {
// 获取
return 0 === strpos($name, '?') ? Cookie::has(substr($name, 1), $option) : Cookie::get($name);
return 0 === strpos($name, '?') ? Cookie::has(substr($name, 1), $option) : Cookie::get($name, $option);
} elseif (is_null($value)) {
// 删除
return Cookie::delete($name);

View File

@@ -1386,7 +1386,7 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
protected function getForeignKey($name)
{
if (strpos($name, '\\')) {
$name = basename(str_replace('\\', '/', $model));
$name = basename(str_replace('\\', '/', $name));
}
return Loader::parseName($name) . '_id';
}

View File

@@ -245,7 +245,7 @@ class HasMany extends Relation
}
return $this->parent->db()->alias($model)
->field($model . '.*')
->join($table . ' ' . $relation, $model . '.' . $this->localKey . '=' . $relation . '.' . $this->foreignKey, $this->joinType)
->join($table . ' ' . $relation, $model . '.' . $this->localKey . '=' . $relation . '.' . $this->foreignKey)
->where($where);
}

View File

@@ -40,7 +40,7 @@ class HasOne extends OneToOne
* @param \Closure $closure 闭包查询条件
* @access public
*/
public function getRelation($subRelation = '', $colsure = null)
public function getRelation($subRelation = '', $closure = null)
{
// 执行关联定义方法
$localKey = $this->localKey;
@@ -54,24 +54,25 @@ class HasOne extends OneToOne
/**
* 根据关联条件查询当前模型
* @access public
* @param Model $model 模型对象
* @param mixed $where 查询条件(数组或者闭包)
* @return Query
*/
public function hasWhere($model, $where = [])
public function hasWhere($where = [])
{
$table = $this->query->getTable();
$table = $this->query->getTable();
$model = basename(str_replace('\\', '/', get_class($this->parent)));
$relation = basename(str_replace('\\', '/', $this->model));
if (is_array($where)) {
foreach ($where as $key => $val) {
if (false === strpos($key, '.')) {
$where['b.' . $key] = $val;
$where[$relation . '.' . $key] = $val;
unset($where[$key]);
}
}
}
return $model->db()->alias('a')
->field('a.*')
->join($table . ' b', 'a.' . $this->localKey . '=b.' . $this->foreignKey, $this->joinType)
return $this->parent->db()->alias($model)
->field($model . '.*')
->join($table . ' ' . $relation, $model . '.' . $this->localKey . '=' . $relation . '.' . $this->foreignKey, $this->joinType)
->where($where);
}

View File

@@ -160,6 +160,7 @@ class MorphMany extends Relation
* @param array $where 关联预查询条件
* @param string $relation 关联名
* @param string $subRelation 子关联
* @param \Closure $closure 闭包
* @return array
*/
protected function eagerlyMorphToMany($where, $relation, $subRelation = '', $closure = false)

View File

@@ -19,13 +19,25 @@ use think\model\Relation;
abstract class OneToOne extends Relation
{
// 预载入方式
protected $eagerlyType = 0;
// 预载入方式 0 -JOIN 1 -IN
protected $eagerlyType = 1;
// 当前关联的JOIN类型
protected $joinType;
// 要绑定的属性
protected $bindAttr = [];
/**
* 设置join类型
* @access public
* @param string $type JOIN类型
* @return $this
*/
public function joinType($type)
{
$this->joinType = $type;
return $this;
}
/**
* 预载入关联查询JOIN方式
* @access public
@@ -67,8 +79,7 @@ abstract class OneToOne extends Relation
if ($closure) {
// 执行闭包查询
call_user_func_array($closure, [ & $query]);
//指定获取关联的字段
//需要在 回调中 调方法 withField 方法,如
// 使用withField指定获取关联的字段,如
// $query->where(['id'=>1])->withField('id,name');
if ($query->getOptions('with_field')) {
$field = $query->getOptions('with_field');
@@ -256,6 +267,9 @@ abstract class OneToOne extends Relation
// 预载入关联查询 支持嵌套预载入
if ($closure) {
call_user_func_array($closure, [ & $model]);
if ($field = $model->getOptions('with_field')) {
$model->field($field)->removeOption('with_field');
}
}
$list = $model->where($where)->with($subRelation)->select();