Yii:模型属性';s的评论/提示

Yii:模型属性';s的评论/提示,yii,Yii,我需要对表单中的某些字段提供注释/提示。我的想法是用模型来描述它,就像属性标签一样。我怎么做 然后,如果Gii模型(和Crud)生成器直接从mysql专栏的评论中获取它,这将是理想的,因此我在这里看到两个问题: 描述模型中的提示并显示在表单上 从mysql注释中获取提示 显示来自模型的提示 下面是对由Yiic <div class="form"> <?php $form=$this->beginWidget('CActiveForm', array( 'id'=&

我需要对表单中的某些字段提供注释/提示。我的想法是用模型来描述它,就像属性标签一样。我怎么做


然后,如果Gii模型(和Crud)生成器直接从mysql专栏的评论中获取它,这将是理想的,因此我在这里看到两个问题:

  • 描述模型中的提示并显示在表单上
  • 从mysql注释中获取提示
  • 显示来自模型的提示

    下面是对由
    Yiic

    <div class="form">
    <?php $form=$this->beginWidget('CActiveForm', array(
        'id'=>'login-form',
        'enableClientValidation'=>true,
        'clientOptions'=>array(
            'validateOnSubmit'=>true,
        ),
    )); ?>
    
        <p class="note">Fields with <span class="required">*</span> are required.</p>
    
        <div class="row">
            <?php echo $form->labelEx($model,'username'); ?>
            <?php echo $form->textField($model,'username'); ?>
            <?php echo $form->error($model,'username'); ?>
        </div>
    
        <div class="row">
            <?php echo $form->labelEx($model,'password'); ?>
            <?php echo $form->passwordField($model,'password'); ?>
            <?php echo $form->error($model,'password'); ?>
            <p class="hint">
                Hint: You may login with <kbd>demo</kbd>/<kbd>demo</kbd> or <kbd>admin</kbd>/<kbd>admin</kbd>.
            </p>
        </div>
    
        <div class="row rememberMe">
            <?php echo $form->checkBox($model,'rememberMe'); ?>
            <?php echo $form->label($model,'rememberMe'); ?>
            <?php echo $form->error($model,'rememberMe'); ?>
        </div>
    
        <div class="row buttons">
            <?php echo CHtml::submitButton('Login'); ?>
        </div>
    
    <?php $this->endWidget(); ?>
    </div><!-- form -->
    
    正如你所看到的,我们;我已将提示文本从视图移动到模型中,并添加了访问它的方法

    现在,回到
    login.php
    ,让我们根据模型中的数据添加提示标记

    <div class="row">
        <?php echo $form->labelEx($model,'password'); ?>
        <?php echo $form->passwordField($model,'password'); ?>
        <?php echo $form->error($model,'password'); ?>
        <?php echo CHtml::tag('p', array('class'=>'hint'), $model->getHint('password')); ?>
    </div>
    
    因此,让我们将注释添加到密码字段

    ALTER TABLE  `tbl_user` 
    CHANGE  `password`  `password` VARCHAR( 256 ) 
    CHARACTER SET latin1 COLLATE latin1_swedish_ci NULL DEFAULT NULL 
    COMMENT  'Hint: You may login with <kbd>demo</kbd>/<kbd>demo</kbd> or <kbd>admin</kbd>/<kbd>admin</kbd>.';
    
    我们现在有两种方法来设置注释,通过mySQL注释或通过硬编码语句

    让我们快速更新
    login.php
    文件,以包含这两种类型的提示

    <div class="row">
        <?php echo $form->labelEx($model,'username'); ?>
        <?php echo $form->textField($model,'username'); ?>
        <?php echo $form->error($model,'username'); ?>
        <?php echo CHtml::tag('p', array('class'=>'hint'), $model->getHint('username')); ?>
    </div>
    
    <div class="row">
        <?php echo $form->labelEx($model,'password'); ?>
        <?php echo $form->passwordField($model,'password'); ?>
        <?php echo $form->error($model,'password'); ?>
        <?php echo CHtml::tag('p', array('class'=>'hint'), $model->getHint('password')); ?>
    </div>
    
    
    
    就这样


    现在,我们的登录页面将如下所示,其中包含来自模型的用户名提示和来自mySQL注释的密码提示。

    从Yii-1.1.13开始,一个新属性已添加到父级
    CDbColumnSchema
    中定义的
    CMysqlColumnSchema

    本专栏的评论。默认值为空字符串,表示未为列设置注释。Null值表示RDBMS根本不支持列注释(SQLite),或者框架还不支持对活动RDBMS的注释检索

    所以我们可以用它来访问评论。有点像这样:

    $modelObject->tableSchema->columns['column_name']->comment
    
    // or if you are doing this within your model use
    $this->tableSchema->columns['column_name']->comment
    

    请注意,在使用任何
    CActiveRecord
    或即使使用时,都已设置了,因此在访问属性时不会执行新的查询


    模型中的示例实现:

    class MyModel extends CActiveRecord {
        // hints array will store the hints
        public $hints=array();
    
        public function init() {
            parent::init();
            $this->hints=self::initHints($this);
        }
    
        public static function initHints($model) {
            $comments=array();
            foreach ($model->tableSchema->columns as $aColumn){
                if(!empty($aColumn->comment))
                    $comments["$aColumn->name"]=$aColumn->comment; 
            }
            return $comments;
        }
    
        public static function model($className=__CLASS__) {
            $model=parent::model($className);
            $model->hints=self::initHints($model);
            return $model;
        }
    
    }
    
    现在,您可以通过以下方式访问提示:

    $model = new MyModel;
    $single_column_hint=$model->hints['column_name'];
    
    $model = MyModel::model();
    $single_column_hint=$model->hints['column_name'];
    

    为了通过gii模板实现这一点,您只需要将上述代码复制到您的自定义模型代码模板,以了解如何实现这一点

    我建议将代码放在自定义基类中,从中可以派生活动记录

    $modelObject->tableSchema->columns['column_name']->comment
    
    // or if you are doing this within your model use
    $this->tableSchema->columns['column_name']->comment
    
    class MyModel extends CActiveRecord {
        // hints array will store the hints
        public $hints=array();
    
        public function init() {
            parent::init();
            $this->hints=self::initHints($this);
        }
    
        public static function initHints($model) {
            $comments=array();
            foreach ($model->tableSchema->columns as $aColumn){
                if(!empty($aColumn->comment))
                    $comments["$aColumn->name"]=$aColumn->comment; 
            }
            return $comments;
        }
    
        public static function model($className=__CLASS__) {
            $model=parent::model($className);
            $model->hints=self::initHints($model);
            return $model;
        }
    
    }
    
    $model = new MyModel;
    $single_column_hint=$model->hints['column_name'];
    
    $model = MyModel::model();
    $single_column_hint=$model->hints['column_name'];