Page 1 of 1

Homemade ORM HowTo's

Posted: Sun Aug 19, 2012 4:41 am
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!

Re: Homemade ORM HowTo's

Posted: Sun Aug 19, 2012 12:09 pm
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.

Re: Homemade ORM HowTo's

Posted: Mon Aug 20, 2012 11:14 am
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?

Re: Homemade ORM HowTo's

Posted: Mon Aug 20, 2012 9:35 pm
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).