PHP Developers Network

Homemade ORM HowTo's
Page 1 of 1

Author:  MindOverBody [ Sun Aug 19, 2012 4:41 am ]
Post subject:  Homemade ORM HowTo's

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?


Author:  Christopher [ Sun Aug 19, 2012 12:09 pm ]
Post subject:  Re: Homemade ORM HowTo's

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.

Author:  MindOverBody [ Mon Aug 20, 2012 11:14 am ]
Post subject:  Re: Homemade ORM HowTo's

Author:  Christopher [ Mon Aug 20, 2012 9:35 pm ]
Post subject:  Re: Homemade ORM HowTo's

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).

Page 1 of 1 All times are UTC - 5 hours
Powered by phpBB® Forum Software © phpBB Group