简体   繁体   English

Kohana 3.3 ORM返回实际数据类型,而不是字符串

[英]Kohana 3.3 ORM return actual data type, not string

I've got a MySQL table which contains a 'status' column of type tinyint(1). 我有一个MySQL表,其中包含类型为tinyint(1)的'status'列。 (Actually this is a boolean column, but as far as I know MySQL does not support this type and auto-converts it to tinyint(1).) (实际上,这是一个布尔列,但是据我所知,MySQL不支持此类型,而是将其自动转换为tinyint(1)。)

If I perform an ORM::factory('Model', $id) on this table and check 'status' I get: a) NULL, if there is no entry for $id b) or (depending on the value stored in this field) 如果我在此表上执行ORM :: factory('Model',$ id)并检查'status',我将得到:a)NULL,如果没有$ id的条目b)或(取决于此表中存储的值)领域)

I'd like to be able to use those three different possiblities as 3 different status options and therefore perform a strict comparison - but this is not possible because of b)'s datatype STRING. 我希望能够将这三种不同的可能性用作3种不同的状态选项,从而执行严格的比较-但这是不可能的,因为b)的数据类型为STRING。

I tried to set 我试图设定

protected $_table_columns = array(
      'user_id' => array(
            'data_type' => 'integer'            
            ),
        'status' => array(
            'data_type' => 'boolean',
            'default' => FALSE,
        ),
    );

but this also doesn't work (and according to what I found so far only the keys matter). 但这也行不通(根据我到目前为止发现的内容,密钥很重要)。

So my question is: Do I really have to cast the value to boolean myself or is there any possibility to specify the data type for an auto-cast done by Kohana? 所以我的问题是:我是否真的必须将值转换为布尔值自己,还是有可能为Kohana进行的自动广播指定数据类型?

Thanks! 谢谢!

I find Kohanas documentation on how to define columns lacking, there are some things I picked up here and there 我发现Kohanas文档缺少有关如何定义列的信息,我在这里和那里都发现了一些问题。

'type'        => 'int',  // Data type - these seem to represent PHP types, (string,int,boolean,etc.)
'default'     => 1,      // Default value

It is fairly easy to write your own auto-generated rules, the following function adds some extra stuff to Kohana ORM: 编写自己的自动生成的规则相当容易,以下函数为Kohana ORM添加了一些额外的东西:

/**
 * Extra stuff
 * 'is_nullable' => TRUE,   // Column is nullable
 * 'max_length'  => 128,    // Maximum column length
 * 'min_length'  => 8,      // Minimum value length
 */
public function rules()
{
    // Return rules array when already set
    if ( ! empty($this->_rules))
    {
        return $this->_rules;
    }

    // Create default rules for each field
    foreach ($this->_table_columns as $name => $properties)
    {
        // Skip the primary key
        if ($name == $this->_primary_key)
        {
            continue;
        }

        // When field is a created/updated column
        // Note: this is some internal stuff we always use
        if (in_array($name, $this->_created_updated_columns))
        {
            $this->_rules[$name][] = array('digit');
        }

        // When field is of type int
        if (Arr::get($properties, 'type') == 'int' AND ! in_array($name, $this->_created_updated_columns))
        {
            $this->_rules[$name][] = array('digit');
        }

        // When field is of type string
        if (Arr::get($properties, 'type') == 'string')
        {
            $this->_rules[$name][] = array('min_length', array(':value', 1));
            $this->_rules[$name][] = array('max_length', array(':value', Arr::get($properties, 'character_maximum_length', 255)));
         }

         // When field is of type binary
         if (Arr::get($properties, 'type') == 'binary')
         {
             $this->_rules[$name][] = array('regex', array(':value', '/[01]/'));
         }

         // Add not empty rule when not nullable
         if (Arr::get($properties, 'is_nullable', FALSE) === FALSE)
         {
             $this->_rules[$name][] = array('not_empty');
         }
     }

     // Return the rules array
     return $this->_rules;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM