public function join($join, $condition = null, $type = 'INNER')
{
if (empty($condition)) {
// 如果为组数,则循环调用join
foreach ($join as $key => $value) {
if (is_array($value) && 2 <= count($value)) {
$this->join($value[0], $value[1], isset($value[2]) ? $value[2] : $type);
}
}
} else {
// 传入的表名为数组
if (is_array($join)) {
if (0 !== ($key = key($join))) {
// 设置了键名则键名为表名,键值作为表的别名
$table = [$key => array_shift($join)];
$this->alias($table);
} else {
$table = array_shift($join);
}
} else {
$table = trim($join);
if (strpos($table, ' ') && !strpos($table, ')')) {
list($table, $alias) = explode(' ', $table);
$table = [$table => $alias];
$this->alias($table);
}
}
$this->options['join'][] = [$table, strtoupper($type), $condition];
}
return $this;
}
/** * 附加查询表达式 * @access protected * @param \think\db\Query $query 查询对象 * @return \think\db\Query */ protected static function attachQuery($query) { $master = basename(str_replace('\\', '/', get_called_class())); $class = new static(); $fields = self::getModelField($master, '', $class->mapFields); $query->alias($master)->field($fields); foreach (static::$relationModel as $key => $model) { $name = is_int($key) ? $model : $key; $table = is_int($key) ? self::db()->name($name)->getTable() : $model; $query->join($table . ' ' . $name, $name . '.' . $class->fk . '=' . $master . '.' . $class->getPk()); $fields = self::getModelField($name, $table, $class->mapFields); $query->field($fields); } return $query; }