Out of pure curiosity, I try to understand how relational Active Records work.
For example, there are two tables in different databases.
The object table from DB-1 has a field device_id.
The table device from DB-2 has an id field.
There is an Object model, it has a connection with the Device model.

public function relations() { return array( 'device' => array(self::HAS_ONE, 'Device', 'id')) } 

Upon request

 var_dump($object->device); 

everything is working.
The question is: how does yii determine which particular record to pull from the database if the tables do not have foreign keys and they lie in different databases?
How or where in the code is it determined that Object :: device_id matches Device :: id ?

Ps Theory is also good, but the code is still better. Just stick your nose where to look. I myself have not figured it out yet.

    1 answer 1

    As I remember by default

     'device' => array(self::HAS_ONE, 'Device', 'id')) 

    From object will take id and will be found by id in device

    You can clearly indicate that yes where to look

     'device'=>array(self::BELONGS_TO,'Device',array('device_id'=>'id')), 

    of type, from the object table, the device_id field and look for matches in the device table in the id field

    • @Shrek Thank you, but I just want to find that piece of code where it turns out that the device_id field corresponds to the id field - zenith
    • Um, open your eyes, I wrote you a field from which table. - Artem
    • @Shrek Yes, I seem to be beginning to understand. It looks like the primary key of the first table is grabbed and its match is searched in the specified field. In general, I repeated your answer in my own words. If I find a piece of code that interests me, I'll post it. - zenith