Question

How can I load the children entities of a parent in the kohana ORM?

Models:

class Model_User extends ORM
{   
    protected $_primary_key = 'UserId';

    protected $_has_many = array(
        'rides' => array(
            'model' => 'ride',
            'foreign_key'=> 'RideId'
        )
    );
}

class Model_Ride extends ORM
{   
    protected $_primary_key = 'RideId';     

    protected $_belongs_to = array(
        'user' => array(
            'model'       => 'user',
            'foreign_key' => 'UserId',
    ),
    );   
}

How would I write the query to get a user and all their Ride?

This is what I have so far:

$members = ORM::factory('user');

$members->where('FirstName', '=', 'Peter')->find_all();

How can I do a FirstOrDefault on all those peters and then list all the Rides of that user?

Was it helpful?

Solution

Something like this:

$members = ORM::factory('user');
$members_list = $members->where('FirstName', '=', 'Peter')->find_all();

foreach($members_list as $member) {
    $rides = $member->rides->find_all();
    foreach($rides as $ride) {
        // Work with ride model
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top