dousi1961 2014-01-28 07:25
浏览 27
已采纳

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

  • 写回答

3条回答 默认 最新

  • dpp89959 2014-01-28 08:51
    关注

    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 :)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应
  • ¥15 matlab基于pde算法图像修复,为什么只能对示例图像有效
  • ¥100 连续两帧图像高速减法
  • ¥15 组策略中的计算机配置策略无法下发
  • ¥15 如何绘制动力学系统的相图
  • ¥15 对接wps接口实现获取元数据
  • ¥20 给自己本科IT专业毕业的妹m找个实习工作
  • ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?
  • ¥30 我的代码按理说完成了模型的搭建、训练、验证测试等工作(标签-网络|关键词-变化检测)