A clean way to implement active loading in ORM Active Record?

I am in the process of writing a very easy implementation of ActiveRecord in PHP. I have the basics of work, but I want to implement active loading of at least one-to-one relationships. I brainstorm in a clean way to do this.

If I want to load one one-to-one relationship, I will need to know the columns for both tables and you will have to alias the columns following some conventions that will allow me to map the results back to the correct objects.

I am looking for suggestions on how to use the column aliases from each table so that comparing them with their respective objects is so painless.

My initial thoughts are to alias the columns of the base table as "base_column_name" and the corresponding columns of the tables as "user_email" (if "User" is the name of the associated object). Is there a better way to do this that I am missing?

The second option that I examined is to remove all objects from the base table, and then assemble the related objects in one "WHERE IN" using the keys from the base table. But will this be a performance issue?

+4
source share
1 answer

CakePHP uses flexible php associative arrays in its ActiveRecord implementation. Thus, a one-to-many relationship may be

array('Tablename'=>array('columnname'=>'columnvalue'), 'AssociatedTable'=>array('0', array('columnname'=>'columnvalue'))); 

It creates another layer in everything, so you need to do $ data ['tablename'] ['columnname'];

0
source

Source: https://habr.com/ru/post/1345184/


All Articles