使用Yii设置默认值和创建到数据库

时间:2014-01-28 07:25:34

标签: php mysql yii

我正在尝试创建一个新用户,但我在尝试创建用户时遇到了麻烦,因为创建用户所需的某些值必须是默认值,我不太确定如何设置。我还需要输入一个不同的表,而实际的“创建”发生在不同的控制器上。

这是我的表单代码:

<?php
 /* @var $this SystemUserController */
 /* @var $model SystemUser */
 /* @var $form CActiveForm */
?>

<div class="form">

<?php $form=$this->beginWidget('CActiveForm', array(
    'id'=>'system-user-form',
    'enableAjaxValidation'=>false,
     )); ?>

<p class="note">Fields with <span class="required">*</span> are required.</p>

<?php echo $form->errorSummary($model); ?>

<!--
<div class="row">
    <?php echo $form->labelEx($model,'party_id'); ?>
    <?php echo $form->textField($model,'party_id',array('size'=>20,'maxlength'=>20)); ?>
    <?php echo $form->error($model,'party_id'); ?>
</div>
!-->
<div class="row" id="toshow" style="display:none" name="suppliers"> <?php $supplier      = SupplierHead::model()->findAll();
   $list = CHtml::listData($supplier ,'head_id','head_name'); 
   echo $form->DropDownList($model,'party_id', 
   $list, array('prompt'=>'Select Supplier'));  ?> 
</div>

<button id="abutton">Already a Supplier</button>

<script>
$(document).ready(function() {
   $("#abutton").click(function(e){
      e.preventDefault();
      $("#toshow").css('display', 'block');
   });
});
</script>



<div class="row">
    <?php echo $form->labelEx($model,'username'); ?>
    <?php echo $form->textField($model,'username',array('size'=>60,'maxlength'=>200)); ?>
    <?php echo $form->error($model,'username'); ?>
</div>

<div class="row">
    <?php echo $form->labelEx($model,'password'); ?>
    <?php echo $form->passwordField($model,'password',array('size'=>60,'maxlength'=>255)); ?>
    <?php echo $form->error($model,'password'); ?>
</div>

<script>
    $("#supplier").click(function () {
    $("#suppliers").show("slow");
    }); 

</script>
<!--
<div class="row">
    <?php echo $form->labelEx($model,'date_last_login'); ?>
    <?php echo $form->textField($model,'date_last_login'); ?>
    <?php echo $form->error($model,'date_last_login'); ?>
</div>

<div class="row">
    <?php echo $form->labelEx($model,'status'); ?>
    <?php echo $form->textField($model,'status',array('size'=>50,'maxlength'=>50)); ?>
    <?php echo $form->error($model,'status'); ?>
</div>

<div class="row">
    <?php echo $form->labelEx($model,'date_created'); ?>
    <?php echo $form->textField($model,'date_created'); ?>
    <?php echo $form->error($model,'date_created'); ?>
</div>

<div class="row">
    <?php echo $form->labelEx($model,'date_modified'); ?>
    <?php echo $form->textField($model,'date_modified'); ?>
    <?php echo $form->error($model,'date_modified'); ?>
</div>
 --!>



<div class="row">
    <?php echo $form->labelEx($model,'user_role'); ?>
    <?php echo $form->textField($model,'user_role',array('size'=>60,'maxlength'=>255)); ?>
    <?php echo $form->error($model,'user_role'); ?>
</div>

<!--
<div class="row">
    <?php echo $form->labelEx($model,'isLogin'); ?>
    <?php echo $form->textField($model,'isLogin'); ?>
    <?php echo $form->error($model,'isLogin'); ?>
</div>
--!>
<div class="row buttons">
    <?php echo CHtml::submitButton($model->isNewRecord ? 'Create' : 'Save'); ?>
</div>

<?php $this->endWidget(); ?>

</div><!-- form -->

正如您所看到的,我已经注释掉了我不想使用的属性。我还修复了SystemUser模型属性rules(),以定义用户输入不需要哪些属性:

    public function rules()
{
    // NOTE: you should only define rules for those attributes that
    // will receive user inputs. 
    return array(
        array('party_id, username, password', 'required'),
        //array('isLogin', 'numerical', 'integerOnly'=>true),
        array('party_id', 'length', 'max'=>20),
        array('username', 'length', 'max'=>200),
        array('password, user_role', 'length', 'max'=>255),

        // The following rule is used by search().
        // Please remove those attributes that should not be searched.
        array('party_id, username' 'on'=>'search'),
    );
}

最后,还有一个下面列表,我在表格中包含了需要插入到不同控制器模型中的下拉列表。我该怎么做?

需要默认值的属性如下:

date_last_login
status
date_created
date_modified

修改

我上传了一张当我选择“创建”时会发生什么的图片

enter image description here

我决定不添加默认值尝试将它们保持为NULL只是为了查看规则()是否有效。我

任何帮助?

3 个答案:

答案 0 :(得分:3)

Yii的模型有以下方法:

  • beforeSave()
  • afterSave()
  • beforeValidate()
  • afterValidate()
  • 等......

可以覆盖到您的模型中。如果要在保存/验证之前设置任何默认值,可以使用模型中提到的方法。请看下面的例子:

public function beforeSave() {

    if (parent::beforeSave()) {
        //Example
        $this->date_modified=new New CDbExpression('NOW()');
        //ANOTHER EXAMPLE
        $this->date=date('Y-m-d',time());
        // YOU CAN EVEN CALLING A WEBSERVICE
        // ANYTHING THAT YOU WANT TO DO BEFORE SAVING INTO DATABASE
        return true;
    }
}

其他方法,例如afterSave和......之类的工作方式。 我希望它有所帮助:)

答案 1 :(得分:2)

您可以使用

之类的规则
     public function rules()
    {

        return array(
    // your other rules
           array('myField','default','value'=>'my Name'),

// for date type use  new CDbExpression('NOW()')

       array('date_modified','default',
              'value'=>new CDbExpression('NOW()'),
              ),

    // rest of your rules
        );
    }

答案 2 :(得分:0)

尝试使用此数据类型:

date_last_login : timestamp
status : enum('active','inactive')
date_created : timestamp
date_modified : timestamp

默认时间戳:current_timestamp