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 急matlab编程仿真二阶震荡系统
  • ¥20 TEC-9的数据通路实验
  • ¥15 ue5 .3之前好好的现在只要是激活关卡就会崩溃
  • ¥50 MATLAB实现圆柱体容器内球形颗粒堆积
  • ¥15 python如何将动态的多个子列表,拼接后进行集合的交集
  • ¥20 vitis-ai量化基于pytorch框架下的yolov5模型
  • ¥15 如何实现H5在QQ平台上的二次分享卡片效果?
  • ¥15 python爬取bilibili校园招聘网站
  • ¥30 求解达问题(有红包)
  • ¥15 请解包一个pak文件