员工模型的查询记录
public static function get_emp_list($where = null, $with = null, $order = null) {
$model = new Emp();
// 下面的 $model->with('dept,dept.company') 同样效果
// 当 模型中的关联 预查询 设置为 ->setEagerlyType(1) 时正确
// 设置为->setEagerlyType(0)时报错
$list = collection($model->with(['dept', 'dept' => ['company']])->where($where)->order('weigh desc,id desc')->select())->toArray();
return $list;
}<?php
namespace app\admin\model\app;
use think\Model;
员工模型
class Emp extends Model {
// 表名
protected $table = 'app_emp';
public function dept() {
// setEagerlyType(1) 能出现正确结果,但是setEagerlyType(0),会报错
return $this->belongsTo('Dept', 'dept_id', 'id', [], 'LEFT')
->setEagerlyType(1);
}
}<?php
namespace app\admin\model\app;
use think\Model;
class Dept extends Model {
// 表名
protected $table = 'app_dept';
/**
* 公司记录
* @return $this
*/
public function company() {
return $this->belongsTo('Company', 'company_id', 'id', [], 'LEFT')
->setEagerlyType(1);
}
/**
* 上级部门
* @return $this
*/
public function parentDept() {
return $this->belongsTo('Dept', 'parent_id', 'id', [], 'LEFT')
->setEagerlyType(1);
}
}报错信息:[ sql ] [ SQL ] SHOW COLUMNS FROM `app_dept` [ RunTime:0.002171s ]
[ error ] [10501]SQLSTATE[42000]: Syntax error or access violation: 1066 Not unique table/alias: 'dept'[D:\Web\fastadmin\thinkphp\library\think\db\Connection.php:385]调试跟踪了一下。在Query.php 中的 with方法中
* 设置关联查询JOIN预查询
* @access public
* @param string|array $with 关联方法名称
* @return $this
*/
public function with($with)
{
if (empty($with)) {
return $this;
}
if (is_string($with)) {
$with = explode(',', $with);
}
$first = true;
/** @var Model $class */
$class = $this->model;
foreach ($with as $key => $relation) {
$subRelation = '';
$closure = false;
if ($relation instanceof \Closure) {
// 支持闭包查询过滤关联条件
$closure = $relation;
$relation = $key;
$with[$key] = $key;
} elseif (is_array($relation)) {
$subRelation = $relation;
$relation = $key;
} elseif (is_string($relation) && strpos($relation, '.')) {
$with[$key] = $relation;
list($relation, $subRelation) = explode('.', $relation, 2);
}
/** @var Relation $model */
$relation = Loader::parseName($relation, 1, false);
$model = $class->$relation();
if ($model instanceof OneToOne && 0 == $model->getEagerlyType()) {
$model->eagerly($this, $relation, $subRelation, $closure, $first);
$first = false;
} elseif ($closure) {
$with[$key] = $closure;
}
}
$this->via();
if (isset($this->options['with'])) {
$this->options['with'] = array_merge($this->options['with'], $with);
} else {
$this->options['with'] = $with;
}
return $this;
}生成执行的SQL语句不正确。SELECT
`emp`.`id`,
`emp`.`name`,
`emp`.`easy_code`,
`emp`.`dept_id`,
`emp`.`post_id`,
`emp`.`email`,
`emp`.`mobile`,
`emp`.`msn_no`,
`emp`.`other_im_no`,
`emp`.`qq_no`,
`emp`.`telephone`,
`emp`.`weigh`,
`emp`.`status`,
dept.id AS dept__id,
dept. NAME AS dept__name,
dept.company_id AS dept__company_id,
dept.dept_kind AS dept__dept_kind,
dept.weigh AS dept__weigh,
dept.parent_id AS dept__parent_id,
dept.std_num AS dept__std_num,
dept. STATUS AS dept__status,
dept.note_info AS dept__note_info
FROM
`app_emp` `emp`
LEFT JOIN `app_dept` `dept` ON `emp`.`dept_id` = `dept`.`id`
LEFT JOIN `app_dept` `dept` ON `emp`.`dept_id` = `dept`.`id`
ORDER BY
`weigh` DESC,
`id` DESC中间会有2个app_dept,不知道是什么原因。 最佳答案