0

我在数据库中有以下表:

  • 团队:
    • ID
    • 姓名
  • 火柴:
    • ID
    • team1_id
    • team2_id

我在Kohana v2.3.4 应用程序中定义了以下ORM模型:

class Match_Model extends ORM {
  protected $belongs_to = array('team1_id' => 'team', 'team2_id' => 'team');
}

class Team_Model extends ORM {
  protected $has_many = array('matches');
}

控制器中的以下代码:

$match = ORM::factory('match',1);
echo $match->team1_id->name; /* <-- */

在标有 的链接上抛出以下错误/* <--- */

试图获取非对象的属性

该框架正在产生外键的值,而不是对 Match_Model 实例的引用(给出所说明的 has_many 和 belongs_to 属性)。

我错过了什么吗?

注意:以防万一,我'match' => 'matches'在 application/config/inflector.php 中添加了不规则复数

4

1 回答 1

0

解决了!Kohana 社区给了我答案

$belongs_to 属性的正确值为:

class Match_Model extends ORM {
  protected $belongs_to = array('team1' => 'team', 'team2' => 'team');
}

文档指出:

class Blog_Post_Model extends ORM { 
    protected $belongs_to = array('author' => 'user', 'editor' => 'user'); 
}

blog_posts 数据库表现在将有 2 列,blog_posts.author_id 和 blog_posts.editor_id,并且两者的值都存在于 users.id 中。

看来我错过了那条线,:)

于 2010-05-11T22:03:00.507 回答