Kohana 3.2 ORM - 跨多个数据库关联模型?

发布于 2024-12-11 10:48:26 字数 1368 浏览 0 评论 0原文

我有一个非标准的数据库设置可以使用,我试图让这个测试用例在 Kohana 3.2 中工作,但没有任何运气。场景:

  • 我有一个使用数据库 A 的课程模型
  • 我有一个使用数据库 B 的会员模型
  • 我在数据库中有一个 course_members 联接表 A

课程模型

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',
    ),
);

}

会员模型

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'
    ),
);

}

现在在我的控制器中尝试,我试图回显一些测试数据

$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/>';

}

,但是 $ member->username 结果是一个空对象。空荡荡的物体让我感到悲伤。

有想法吗? Kohana ORM 可以在多个数据库中以这种方式工作吗?

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:

  • I have a courses model using database A
  • I have a members model using database B
  • I have a courses_members join table in database A

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

牵强ㄟ 2024-12-18 10:48:27

这可以使用 LEAP ORM for Kohana 轻松完成。在LEAP中,您所要做的就是在模型中指定要使用的数据源(即数据库配置组),ORM将处理数据库之间的切换,因为它利用数据库连接池。 Leap 还允许您在数据库方言之间切换,因为该模块具有 DB2、Firebird、MariaDB、MS SQL、MySQL、Oracle、PostgreSQL 和 SQLite 的驱动程序。您可以从 github 下载 LEAP ORM https://github.com/spadefoot/kohana-orm-飞跃

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.

薄荷港 2024-12-18 10:48:27

现在回答有点晚了,但这是解决方案。更改

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

为:

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

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/>';
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文