When defining rules, does the field name have to match the column name in the database?
I'm having a problem where if the field name and the column name don't match, the rules are displayed and nothing is saved to the database.
What works:
Database
user_id | username
Field
<?php echo Form::input('username', $username, array('id' => 'username')); ?><?php echo Arr::get($errors, 'username');?>
Model
class Model_User extends ORM {
protected $_primary_key = 'user_id';
public function rules()
{
return array(
'username' => array(
array('not_empty'),
),
);
}
}
Messages
return array(
'username' => array(
'not_empty' => 'You must provide a username.',
),
);
What doesn't work:
Database
user_id | username
Field
<?php echo Form::input('membername', $username, array('id' => 'username')); ?><?php echo Arr::get($errors, 'membername');?>
Model
class Model_User extends ORM {
protected $_primary_key = 'user_id';
public function rules()
{
return array(
'membername' => array(
array('not_empty'),
),
);
}
}
Messages
return array(
'membername' => array(
'not_empty' => 'You must provide a username.',
),
);
It seems if I change the field name to something other than username the rules function doesn't work correctly.
If I don't apply any rules, changing username to something else doesn't cause any problems with saving data to the database.
Mapping column names from your database to different internal names is not possible. The same column name is also used for setting rules, labels etc. I don't see why you would want to change the column names, it would be bad for a developers expectations. My advise is to rename your table columns in that case.
The only possibility Kohana ORM provides to control your table columns is to whitelist an array of column names that is to be used. Use this variable in your ORM module:
protected $_table_columns = array('column1','column2');