理论默认值和关系 [英] doctrine default values and relations

查看:122
本文介绍了理论默认值和关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在mysql中,我可以为列设置默认值:

In mysql I can set default value for column like this:

ALTER TABLE <Table> CHANGE <Column> DEFAULT <NEW_DEFAULT_VALUE>

我可以检索默认值:

SELECT DEFAULT(<Column>) FROM <Table> LIMIT 1

是否可以用Doctrine实现这个概念?

Is it possible to achieve this concept with Doctrine?

我实际需要的是在我的表类中的这种方法:

What I actually need is such methods in my table class:

class UserTable extend Doctrine_Table {
    /* ... */

    /** 
     * @return Doctrine_Record
     */
    public function getDefaultProperty() {
        return ???;
    }
    public function setDefaultProperty($value) {
        /* $value can be either integer or Doctrine_Record */
    }
}


推荐答案

我们来看看我们可以弄清楚这一点。

Let's see if we can figure this out.

Record.php 的第567行,部分 assignDefaultValues()方法是在创建对象时填充默认值,说:

Line 567 of Record.php, part of the assignDefaultValues() method which populates defaults as the object is created, says:

$default = $this->_table->getDefaultValueOf($column);

所以你实际上不需要添加一个 getDefaultProperty() code>,您可以使用:

So you don't actually need to add a getDefaultProperty(), you can use:

$userTable = Doctrine::getTable('User');
$defaultName = $userTable->getDefaultValueOf('name');

然而,您似乎想要 getDefaultProperty()返回一个 Doctrine_Record 。如果您只是希望得到一个完整的对象,其中设置了所有的默认值,我相信你可以使用:

However, you seem to want getDefaultProperty() to return a Doctrine_Record. If you just want to get back a whole object that has all the default values set on it, I'm pretty sure you can just use:

$ defaultUser = new User(); (请参阅默认值的原则手册

但请确保您的默认值在模式中定义.yml,不只是在数据库中。 Doctrine不会从数据库中读取默认值,而是依赖模式来告诉它该怎么做。

But be sure that your defaults are defined in the schema.yml, not just in the database. Doctrine won't read defaults out of the database, and relies on the schema to tell it what to do.

让我知道这是如何工作的。

Let me know how that works.

干杯,
〜J

Cheers, ~J

这篇关于理论默认值和关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆