hasWhere关联没有joinType参数

浏览:1058 发布日期:2017/04/10 分类:ThinkPHP5专区
现在主表与子表都有数据才显示数据,但是有些单据默认还没有子表数据,添加后查询不到,DEBUG之后发现用的是INNER JOIN,如何设置其他模式 public function hasWhere($where = [])
    {
        $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[$relation . '.' . $key] = $val;
                    unset($where[$key]);
                }
            }
        }
        return $this->parent->db()->alias($model)
            ->field($model . '.*')
            ->join($table . ' ' . $relation, $model . '.' . $this->localKey . '=' . $relation . '.' . $this->foreignKey)
            ->where($where);
    }
只需要在join里面加个参数应该就好了,has就有这样的功能,但是我用的是闭包 public static function has($relation, $operator = '>=', $count = 1, $id = '*')
    {
        $relation = (new static())->$relation();
        if (is_array($operator) || $operator instanceof \Closure) {
            return $relation->hasWhere($operator);
        }
        return $relation->has($operator, $count, $id);
    }
Model里面直接还是用hasWhere
最佳答案
评论( 相关
后面还有条评论,点击查看>>