- 首页
- PHP
- 使用 Yii 设置默认值并创建到数据库中
使用 Yii 设置默认值并创建到数据库中
[英] Setting default values and Creating into Database using Yii
本文介绍了使用 Yii 设置默认值并创建到数据库中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试创建一个新用户,但我在尝试创建用户时遇到了问题,因为创建用户所需的某些值必须是我不太确定如何设置的默认值.我还需要输入不同的表,而实际的创建"发生在不同的控制器中.
这是我的表单代码:
<div class="form"><?php $form=$this->beginWidget('CActiveForm', array('id'=''system-user-form','enableAjaxValidation'=>false,));?><p class="note">带有 <span class="required">*</span> 的字段</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 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'));?>
<button id="abutton">已经是供应商</button><脚本>$(document).ready(function() {$("#abutton").click(function(e){e.preventDefault();$("#toshow").css('display', 'block');});});<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 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');?>
<脚本>$("#supplier").click(function () {$("#suppliers").show("slow");});<!--<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 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 class="row"><?php echo $form->labelEx($model,'date_created');?><?php echo $form->textField($model,'date_created');?><?php echo $form->error($model,'date_created');?>
<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 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 class="row"><?php echo $form->labelEx($model,'isLogin');?><?php echo $form->textField($model,'isLogin');?><?php echo $form->error($model,'isLogin');?>
--!><div class="行按钮"><?php echo CHtml::submitButton($model->isNewRecord ?'Create' : 'Save');?>
<?php $this->endWidget();?></div><!-- 形式-->
如您所见,我已经注释掉了不想使用的属性.我还修复了 SystemUser 模型属性 rules() 以定义用户输入不需要哪些属性:
公共函数规则(){//注意:你应该只为那些属性定义规则//将接收用户输入.返回数组(数组('party_id,用户名,密码','必需'),//array('isLogin', 'numerical', 'integerOnly'=>true),数组('party_id', 'length', 'max'=>20),数组('用户名','长度','最大'=> 200),数组('密码,用户角色','长度','最大'=> 255),//以下规则由 search() 使用.//请删除那些不应搜索的属性.数组('party_id,用户名''on'='搜索'),);}
最后,还有一个下拉列表,我在上面包含的表单中需要插入到不同控制器的模型中.我该怎么办?
需要默认值的属性如下:
date_last_login地位创建日期修改日期
编辑
我上传了一张选择创建"时发生的事情的图片
我决定不添加默认值,尝试将它们保持为 NULL 只是为了看看 rules() 是否有效.我
有什么帮助吗?
解决方案
Yii 的模型有如下方法:
- beforeSave()
- afterSave()
- beforeValidate()
- afterValidate()
- 等等......
可以覆盖到您的模型中.如果您想在保存/验证之前设置任何默认值,您可以使用模型中提到的方法.请看下面的例子:
public 函数 beforeSave() {如果(父母::beforeSave()){//例子$this->date_modified=new New CDbExpression('NOW()');//另一个例子$this->date=date('Y-m-d',time());//你甚至可以调用网络服务//在保存到数据库之前您想做的任何事情返回真;}}
其他方法,例如 afterSave 和 ... 与上面的工作类似.我希望它有帮助:)
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"
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 :)
这篇关于使用 Yii 设置默认值并创建到数据库中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文