我发现 Kohanas 关于如何定义缺少的列的文档,我在这里和那里收集了一些东西
'type' => 'int', // Data type - these seem to represent PHP types, (string,int,boolean,etc.)
'default' => 1, // Default value
编写自己的自动生成规则相当容易,以下函数为 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;
}