eloquent-power-joins icon indicating copy to clipboard operation
eloquent-power-joins copied to clipboard

Shouldn't PowerJoinClause always qualify the conditions?

Open nickfls opened this issue 3 years ago • 0 comments
trafficstars

This is from PowerJoinClause:136:143

public function where($column, $operator = null, $value = null, $boolean = 'and')
    {
        if ($this->alias && is_string($column) && Str::contains($column, $this->tableName)) {
            $column = str_replace("{$this->tableName}.", "{$this->alias}.", $column);
        }

        return parent::where($column, $operator, $value, $boolean);
    }

this line if ($this->alias && is_string($column) && Str::contains($column, $this->tableName)) { gets repeated a lot.

Recently i hit a case when i have two joins on the same table and while the first one is aliased, the scope on the second produces an issue, as the scope becomes ambiguous.

Would you consider an addition of two methods: qualify and requalify, something to the effect of:

/**
     * @param string $column
     * @return string
     */
    public function qualify(string $column): string
    {
        $prefix = $this->alias ?? $this->tableName;
        
        return $this->requalify($column, $prefix);
    }

    /**
     * @param string $column
     * @param string $prefix
     * @return string
     */
    public function requalify(string $column, string $prefix): string
    {
        if (Str::contains($column, $this->tableName)) {
            return str_replace("{$this->tableName}.", "{$prefix}.", $column);    
        }
        
        return "{$prefix}.". $column;
    }

and so the resulting PowerJoinClause::where() becomes:

    public function where($column, $operator = null, $value = null, $boolean = 'and')
    {
        if (is_string($column)) {
            $column = $this->qualify($column);
        }
        
        return parent::where($column, $operator, $value, $boolean);
    }

nickfls avatar Jun 01 '22 15:06 nickfls