有关在 Kohana ORM 中声明模型的一些问题

发布于 2024-11-17 02:22:51 字数 459 浏览 6 评论 0原文

所以我刚刚开始学习使用 ORM,当我第一次开始学习 Ko3 时就使用 Jelly,并遇到了一些问题......它们可能看起来很愚蠢,所以请耐心等待。

首先,我注意到声明模型足以开始使用它,而无需分解字段。这是我不知道的自动魔法吗?如果是这样......它会导致性能问题吗?应该避免吗?

我还注意到声明规则的两种不同方式......这两种方法有什么区别?

public function rules() {
    return array(
        'categoryname' => array(
            array('not_empty'),
        ),
    );
}

protected $_rules = array(
    'categoryname' => array(
        'not_empty' => true,
    ),
);

So I just started learning to use ORM, went with Jelly when I first started learning Ko3, and have run into a few questions... they may seem stupid so please bear with me.

First I noticed that declaring the Model is enough to start using it, without having to break down the fields. Is this some automagic I'm unaware of? If so... does it cause performance issues and should it be avoided?

I've also noticed two different ways of declaring rules... what's the difference between these two methods?

public function rules() {
    return array(
        'categoryname' => array(
            array('not_empty'),
        ),
    );
}

protected $_rules = array(
    'categoryname' => array(
        'not_empty' => true,
    ),
);

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

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

发布评论

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

评论(1

马蹄踏│碎落叶 2024-11-24 02:22:51
  1. 是的,ORM 会自动在空模型上调用 SHOW COLUMNS。但您可以简单地缓存它
  2. rules() 已在 Kohana 3.1 中添加(3.0 使用 $_rules 属性)。现在您可以在返回规则列表之前应用一些逻辑。并且它更适合模型扩展,例如:
公共函数规则()
{
    返回父级::rules() + array('foo' => 'bar');
}
  1. Yes, ORM will automatically call SHOW COLUMNS on empty model. But you can simply cache it.
  2. rules() was added in Kohana 3.1 (3.0 uses $_rules property). Now you can apply some logic before return rule list. And its better for model extending, for example:
public function rules()
{
    return parent::rules() + array('foo' => 'bar');
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文