catfan / Medoo

The lightweight PHP database framework to accelerate the development.
https://medoo.in
MIT License
4.84k stars 1.15k forks source link

raw() for table-join #679

Closed alaniso1 closed 3 years ago

alaniso1 commented 6 years ago

Hello catfan,

thanks for the great work within medoo! I like it very much and use it in different projects.

I've on question: Is it possible for you to extend medoo functionality to have raw()-function also in a table-join?

Here is a example:

$rows = $database->select ( 'tab_activity', [ '[>]tab_activity_text' => [ 'tab_activity.record_id' => 'activity_id', 'tab_activity_text.lang' => Medoo::raw("'de'") ] ], '*', ['tab_activity.record_id[>]' => 10 ] );

Background is: To be able to set a constant value (in this case the language 'de') in the join statement!

Thanks in advance, André

catfan commented 6 years ago

Join is identified the relationship between two column. The value here will be the column name only. Why you need to use raw for JOIN? It will be failed.

sourabhbajaj commented 6 years ago

Consider this usecase, I have posts which can be upvoted or downvoted and I want to fetch what's the vote of the current logged in user for each post in the feeds. The query would look something like this, lets say the current logged in user has id 5:

select id, text, post_votes.vote as myvote from posts left join post_votes on post_votes.post_id=posts.id and post_votes.user_id=5

I could not find any way to do this with Medoo except raw with join. For any application with voting, this seem to be an important usecase. I have added support for raw in join and would like to make a pull request if you approve? @catfan

Thank you

ghost commented 5 years ago

Consider postgresql as the database backend. Having table_a with a column of primaryKey and table_b with a column of primaryKeys[] in whitch table_b's primaryKeys[] column is a array of integers referencing primaryKey from table_a (not enforced). At the moment you cannot create the following valid query in Medoo:

SELECT * FROM table_a a LEFT JOIN table_b b ON a."primaryKey" = ANY(b."primaryKeys")

In this situation I believe the raw function would prove useful $medooObj->select('table_a', [ "[>]table_b" => ['primaryKey' => Medoo::raw('ANY(primaryKeys)')] ], "*");