Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BUG: Table->join method is not using alias. #19

Open
b-hayes opened this issue Apr 5, 2022 · 2 comments
Open

BUG: Table->join method is not using alias. #19

b-hayes opened this issue Apr 5, 2022 · 2 comments

Comments

@b-hayes
Copy link
Contributor

b-hayes commented Apr 5, 2022

When adding an alias to ->join() the table breaks with an error:

PicoDb\SQLException: SQL Error: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'phone.contact_id' in 'on clause'

image

See the image. ->join() is missing the AS statement that the ->left() method has.

recommendation:
Given that any fix will be a breaking change, and that ->join is functionally the same as ->left,
I would just make join a wrapper for left.
I'd also change the interface to be nicer to use and let you specify the alias without the local table param.
like so:

public function join($table, $foreign_column, $local_column, $alias = '', $local_table = '')
    {
        $local_table = $local_table ?: $this->name;
        return $this->left($table, $alias,$foreign_column, $local_table,$local_column);
    }

and then you can use it like this:

->join('cats_table','id','cat_id', 'catalias');

NTH
Would also be nice to auto prepend the alias name to the columns when provided as the common situation for needing an alias is that columns with the same name are replacing each other. So you introduce an alias, and then as soon as you do this you now have to manually write out every single column and specify an alias for each column.

Would be great if this was taken care of.

@b-hayes b-hayes changed the title Table->join method is not using alias. BUG: Table->join method is not using alias. Apr 5, 2022
@jaicarlton
Copy link

jaicarlton commented Apr 5, 2022

So the "hack" around this bug has been to append "AS $alias" to $table.

I think this also should be fixed, but it will be a breaking change for some. So heads up.

@b-hayes
Copy link
Contributor Author

b-hayes commented Apr 5, 2022

added a recommendation

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

2 participants