drpkcwwav20524605 2013-02-01 15:29
浏览 35
已采纳

Kohana 3.2规则失败

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.

  • 写回答

1条回答 默认 最新

  • doumei3828 2013-02-02 14:34
    关注

    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');
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 前端echarts坐标轴问题
  • ¥15 CMFCPropertyPage
  • ¥15 ad5933的I2C
  • ¥15 请问RTX4060的笔记本电脑可以训练yolov5模型吗?
  • ¥15 数学建模求思路及代码
  • ¥50 silvaco GaN HEMT有栅极场板的击穿电压仿真问题
  • ¥15 谁会P4语言啊,我想请教一下
  • ¥15 这个怎么改成直流激励源给加热电阻提供5a电流呀
  • ¥50 求解vmware的网络模式问题 别拿AI回答
  • ¥24 EFS加密后,在同一台电脑解密出错,证书界面找不到对应指纹的证书,未备份证书,求在原电脑解密的方法,可行即采纳