Вопрос

I've got a MySQL table which contains a 'status' column of type tinyint(1). (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).)

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)

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.

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?

Thanks!

Это было полезно?

Решение

I find Kohanas documentation on how to define columns lacking, there are some things I picked up here and there

'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:

/**
 * 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 с атрибуция
Не связан с StackOverflow
scroll top