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

I'm trying to create a new user but I'm having trouble trying to create the user because some of the values that are needed to create a user must be default values that I'm not quite sure how to set. I also need to input into a different table while the actual "create" happens from a different controller.

Here is my form code:

<?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 -->

As you can see, I've commented out the attributes that I don't want to use. I also fixed the SystemUser model attributes rules() to define which attributes won't be needed for user input here:

    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'),
    );
}

Finally, there's also a drop down list I included above from the form that is required to be inserted into a model of a different controller. How do I go about this?

The attributes that need default values are as follows:

date_last_login
status
date_created
date_modified

EDIT

I've uploaded a picture of what happens when I select "Create"

enter image description here

I decided not to add defaults try keeping them NULL just to see if the rules() would work. I

Any help?

Yii's model has methods such as:

  • beforeSave()
  • afterSave()
  • beforeValidate()
  • afterValidate()
  • and so on ...

which can be overridden into your model. If you want to set any default value before saving/validating you can use from mentioned methods in your model. Please take a look at the following example:

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;
    }
}

other methods such as afterSave and ... work like above. I hope it help :)

Try with this data type:

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

Defult Time stamp: current_timestamp

You can use the rules for it like

     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
        );
    }