ORM relationship, PHP and SQL join
•
Java
I'm developing a small PHP framework for a school project. I'm learning how to build the ORM part
However, I can't figure out how to map object relationships from SQL queries with joins, or I just don't know the right word to search for:(
t1 id title text
–
t2 id name description
I try to do a simple thing: select * from T1 left join T2 on T1 id = t2. t1_ id
What I get is a simple array in which all fields from both tables and ID columns are overwritten because it exists in both
[ "id" => "2" "title" => "Lorem" "text" => "Ipsum" "name" => "Tomato" "description" => "Tomato are nice" ]
So my question is, is there a simple way to get such a connection?
[ "t1" => [ "id" => 2 "title" => "Lorem" "text" => "Tomato" "t2" => [ "id" => 3 "name" => "Tomato" "description" => "Tomato are nice" ] ]
Solution
No, join is used to create a table view with two tables side by side But you can do this:
SELECT t1.id,t1.title,t1.next,t2.name as "t2_name",t2.description as "t2_description" FROM t1 LEFT JOIN t2 ON t1.id = t2.t1_id
This will give you the following prefix:
[ "t1" => [ "id" => 2 "title" => "Lorem" "text" => "Tomato" "id" => 3 "t2_name" => "Tomato" "t2_description" => "Tomato are nice" ] ]
This solves the same problem you are trying to solve
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码