Kohana ORM 别名和“尝试获取非对象的属性”

发布于 2024-08-31 23:56:58 字数 963 浏览 7 评论 0原文

我在数据库中有以下表格:

  • 团队:
    • ID
    • 姓名
  • 匹配:
    • ID
    • team1_id
    • team2_id

我在我的 ORM 模型中定义了以下="http://kohanaphp.com/" rel="nofollow noreferrer">Kohana v2.3.4 应用程序:

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' =>; application/config/inflector.php 中的“匹配”

I have the following tables in the database:

  • teams:
    • id
    • name
  • matches:
    • id
    • team1_id
    • team2_id

I've defined the following ORM models in my Kohana v2.3.4 application:

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

The following code in a controller:

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

Is throwing the following error on the linke marked with /* <--- */:

Trying to get property of non-object

The framework is yielding the value of the foreign key instead of a reference to a Match_Model instance as it should (giving the has_many and belongs_to properties stated).

Am I missing something?

Note: Just in case, I've added the irregular plural 'match' => 'matches' in application/config/inflector.php

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

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

发布评论

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

评论(1

青衫儰鉨ミ守葔 2024-09-07 23:56:58

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

$belongs_to 属性的正确值是:

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

文档 指出:

class Blog_Post_Model 扩展 ORM { 
    protected $belongs_to = array('作者' => '用户', '编辑者' => '用户'); 
}

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

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

SOLVED! The Kohana community gave me the answer:

The correct value for the $belongs_to property is:

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

The documentation states it:

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

The blog_posts database table would
have 2 columns now,
blog_posts.author_id and
blog_posts.editor_id, and both would
have values that exist in users.id.

It seems that I've missed that line, :)

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