gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 13:43:21 26 4
gpt4 key购买 nike

我需要对表单中的某些字段进行评论/提示。我的想法是在模型中描述它,就像attributeLabels一样。我该怎么做?

然后它会是理想的,如果 Gii 模型(和 Crud)生成器直接从 mysql 列的评论中获取它

最佳答案

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

  • 在模型中描述提示并在表单上显示。
  • 从 mysql 评论中获取提示

  • 显示来自模型的提示

    这是默认 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];
    }

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

    现在,回到 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 注释的密码提示。

    关于Yii:模型属性的注释/提示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12319659/

    26 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com