Benjamin
Benjamin

Reputation: 261

Kohana 3.2 ORM - relating models across multiple databases?

I have a non-standard database setup to work with and i'm trying to get this test case to work in Kohana 3.2 but not having any luck. Scenario:

Model for courses

class Model_Course extends ORM {

// Select the DB
protected $_db_group = 'default';

// override primary key
protected $_primary_key = 'courseid';

// Relationship
protected $_has_many = array(
    'members' => array(
        'model'   => 'member',
        'foreign_key' => 'memberID',
        'through' => 'courses_members',
    ),
);

}

Model for members

class Model_Member extends ORM {

// Select the DB
protected $_db_group = 'alternate';

// override primary key
protected $_primary_key = 'memberID';

// Relationship
protected $_has_many = array(
    'courses' => array(
      'model'   => 'course',
      'foreign_key' => 'courseid',
      'through' => 'courses_members'
    ),
);

}

Now in my controller trying i'm trying to echo out some test data

$courses = ORM::factory('course')->find_all();

foreach ($courses as $course) 
{
  echo $course->coursename . '<br/>';

  foreach ($course->members as $member) 
  {
    echo '-' . $member->username . '<br/>';
  }

  echo '<hr/>';

}

but $member->username results in an empty object. Empty objects make me sad.

Ideas? Can the Kohana ORM work this way across multiple databases?

Upvotes: 3

Views: 2102

Answers (2)

nicowernli
nicowernli

Reputation: 3348

It's a little late for the answer, but here is the solution. Change

foreach ($course->members as $member) 
{
    echo '-' . $member->username . '<br/>';
}

For:

foreach ($course->members->find_all() as $member) 
{
    echo '-' . $member->username . '<br/>';
}

Upvotes: 0

Matthew
Matthew

Reputation: 223

This can be easily accomplished using the LEAP ORM for Kohana. In LEAP, all you have to do is specify in your models the data source (i.e. database config group) you want to use and the ORM will handle the switching between databases because it utilizes a database connection pool. Leap will also allow you to switch between database dialects since the module has drivers for DB2, Firebird, MariaDB, MS SQL, MySQL, Oracle, PostgreSQL, and SQLite. You can download the LEAP ORM from github at https://github.com/spadefoot/kohana-orm-leap.

Upvotes: 1

Related Questions