Yii:模型属性的注释/提示

时间:2012-09-07 14:01:47

标签: yii

我需要在表单中的某些字段中添加注释/提示。我的想法是在模型中描述它,就像attributeLabels一样。我该怎么办?

然后,如果Gii Model(和Crud)生成器直接从mysql专栏的评论中获取它,那将是理想的

2 个答案:

答案 0 :(得分:10)

所以我在这里看到两个问题:

  1. 描述模型中的提示并在表单上显示。
  2. 从mysql评论中获取提示
  3. 显示模型中的提示

    以下是login.php

    生成的默认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 -->
    

    让我们通过向attributeHints()模型添加getHint()方法和LoginForm.php方法,将密码提示移到模型中。

        /**
         * Declares attribute hints.
         */
        public function attributeHints()
        {
            return array(
                    'password'=>'Hint: You may login with <kbd>demo</kbd>/<kbd>demo</kbd> or <kbd>admin</kbd>/<kbd>admin</kbd>.',
            );
        }
    
        /**
         * Return a hint
         */
        public function getHint( $attribute )
        {
            $hints = $this->attributeHints();
    
            return $hints[$attribute];
        }
    

    如您所见,我们已将提示文本从视图移动到模型中,并添加了访问它的方法。

    现在,回到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>
    

    所以现在我们已经将硬编码的提示更改为使用模型中的数据填充的生成元素。

    现在,继续讨论第二个问题。

    从mySQL评论中获取提示

    不幸的是,我并不熟悉Gii,知道如何从mySQL注释中自动生成提示。但是,将mySQL注释数据放入模型中相当容易。

    为此,我们可以使用以下mySQL查询

    SHOW FULL COLUMNS FROM `tbl_user`
    

    所以我们将注释添加到密码字段

    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>.';
    

    让我们添加代码以将其提取到我们的attributeHints()方法中。

        /**
         * Declares attribute hints.
         */
        public function attributeHints()
        {
            $columns= Yii::app()->db->createCommand('SHOW FULL COLUMNS FROM `tbl_user`')->queryAll();
    
            $comments=array();
            foreach($columns as $column){
                if( isset( $column['Comment'] ) )
                {
                    $comments[ $column['Field'] ] = $column['Comment'];
                }
    
            }
    
            //Add any hardcoded hints here
            $hints = array(
                    'username'=>'Enter username above',
            );
    
            //Return merged array
            return array_merge( $comments, $hints );
        }
    

    我们现在有两种方法可以通过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>
    

    就是这样!

    Login page

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

答案 1 :(得分:2)

从Yii-1.1.13开始,新的属性comment已添加到CMysqlColumnSchema中,该属性在父CDbColumnSchema中定义:

  

本专栏的评论。默认值为空字符串,表示没有为列设置注释。空值意味着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

请注意,tableSchema已设置CActiveRecord model is constructed或使用static model时已设置read the guide,因此访问该属性时没有执行新查询


模型中的示例实现:

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模板执行此操作,您只需将上述代码复制到自定义模型代码模板,{{3}}即可查看如何执行此操作。

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