Homemade ORM HowTo's

Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.

Moderator: General Moderators

Post Reply
User avatar
MindOverBody
Forum Commoner
Posts: 96
Joined: Fri Aug 06, 2010 9:01 pm
Location: Osijek, Croatia

Homemade ORM HowTo's

Post by MindOverBody »

Hi everyone!

I am currently developing my own ORM out of boredom. There is a mapping file which maps database tables to my classes, and columns to class properties. For the momement, I'd like to support only basic CRUD, no custom filters, etc.; e.g. create, readByPK, update, delete, readAll. This CRUD functionality is implemented in abstract entity class which all of my entities should extend. I managed to implement this with no problem, but... I am currently stuck with foreign keys. I'd like to e.g. on readByPK if table has foreign key reference, to read referenced table and fetch object of that reference instead only it's id.

What is best way to do it? I know i can read my mapping and for each foreign key make additional query, but i am wondering is there another way? Is it possible to make it in only one query using JOIN?

Cheers!
User avatar
Christopher
Site Administrator
Posts: 13592
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Homemade ORM HowTo's

Post by Christopher »

Yes, you can certainly do a JOIN. However it may complicate your code instead of keeping the loading of the second table(s) modular. Define you interface and implement it simply -- then refactor later for performance as necessary.
(#10850)
User avatar
MindOverBody
Forum Commoner
Posts: 96
Joined: Fri Aug 06, 2010 9:01 pm
Location: Osijek, Croatia

Re: Homemade ORM HowTo's

Post by MindOverBody »

Christopher wrote:Yes, you can certainly do a JOIN. However it may complicate your code instead of keeping the loading of the second table(s) modular. Define you interface and implement it simply -- then refactor later for performance as necessary.
I was able to implement recursive foreign key resolving, it works perfectly, but now for each foreign key, additional query is done on database.

Just wondering, what would take to do it with JOIN? What would be the most simple workflow?
User avatar
Christopher
Site Administrator
Posts: 13592
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Homemade ORM HowTo's

Post by Christopher »

If you force every field to have the table specified the adding the JOINs should be fairly simple. You may have problems with name clashes and will need to come up with some aliasing scheme for the field names in the joined tables (and then de-alias when creating dependent objects).
(#10850)
Post Reply