我是 Yii 框架的初学者,我用 Yii 小部件 CActiveForm 创建了_form.php。我已经序列化了操作创建的复选框值并保存到数据库表中。实际上的问题是,当我调用 actionupdate 时,我选中的值没有显示我已使用相同的表单_form.php在 actioncreate.actioncreate 和 actionupdate 上插入。
我在下面写了我的代码
-
操作创建
公共函数 actionCreate(){ $model=新客户帐户;
if(isset($_POST['CustomerAccounts'])) { $model->attributes=$_POST['CustomerAccounts']; $model->DateCreated = date('Y-m-d H:i:s'); $model->DeliveryDay = serialize($_POST['CustomerAccounts']['DeliveryDay']); if($model->save()) { Yii::app()->user->setFlash('success', 'Customer Account create successfully.'); $this->redirect(array('admin')); } else { Yii::app()->user->setFlash('danger','An error occurred. Please try again.'); } } $this->render('create',array( 'model'=>$model, ));
}
-
操作更新
公共函数操作更新($id){ $model=$this->负载模型($id);
if(isset($_POST['CustomerAccounts'])) { $model->attributes=$_POST['CustomerAccounts']; $model->DeliveryDay = serialize($_POST['CustomerAccounts']['DeliveryDay']); $model->DateCreated = date('Y-m-d H:i:s'); if($model->save()) { Yii::app()->user->setFlash('success', 'Customer Account update successfully.'); $this->redirect(array('admin')); } else { Yii::app()->user->setFlash('danger','An error occurred. Please try again.'); } } $this->render('update',array( 'model'=>$model, ));
}
-
_form.php
<?php $form=$this->beginWidget('CActiveForm', array(
'id'=>'customer-accounts-form',
'enableAjaxValidation'=>false,
<div class="row">
<div class="col-lg-12" style="padding-left: 34px;">
<?php echo $form->labelEx($model, 'DeliveryDay'); ?>
<?php echo $form->error($model, 'DeliveryDay'); ?>
<div id="checkbox" style="padding-left: 90px;">
<?php
$htmlOptions = array('template' => '<tr><td >{input}</td> <td> {label}</td> </tr', 'multiple' => true, 'checked' => 'checked');
echo $form->checkBoxList($model, 'DeliveryDay', Yii::app()->params['WeekDays'], $htmlOptions);
?>
</div></div></div>
<?php $this->endWidget(); ?>
这是我的模型
类客户帐户扩展了CActiveRecord{
public function tableName()
{
return 'customer_accounts';
}
public function rules()
{
return array(
array('DeliveryDay, Status, CustomerID, Employee_ID, PaymentModeID', 'required'),
array('CustomerID, Employee_ID, PaymentModeID, PcBottle, Bottle6Ltr, Bottle1500Ml, Bottle500Ml, TabStand, Pump, DispensirStatus, PCBottlesQuantity, PCBottleSecurity, DispensirQuantity, DispensirSerialNo, DispensirSecurity, TotalAmount, Status', 'numerical', 'integerOnly'=>true),
array('DateJoin', 'length', 'max'=>250),
array('Description', 'safe'),
array('CustomerAccountID, CustomerID, Employee_ID, PaymentModeID, DateJoin, PcBottle, Bottle6Ltr, Bottle1500Ml, Bottle500Ml, TabStand, Pump, DispensirStatus, PCBottlesQuantity, PCBottleSecurity, DispensirQuantity, DispensirSerialNo, DispensirSecurity, TotalAmount, DeliveryDay, Description, Status, DateCreated', 'safe', 'on'=>'search'),
);
}
public function relations()
{
return array(
'customer' => array(self::BELONGS_TO, 'Customer', 'CustomerID'),
'employee' => array(self::BELONGS_TO, 'Employee', 'Employee_ID'),
'paymentMode' => array(self::BELONGS_TO, 'PaymentMods', 'PaymentModeID'),
);
}
public function attributeLabels()
{
return array(
'CustomerAccountID' => 'Customer Account',
'CustomerID' => 'Customer',
//'Employee_ID' => 'Employee',
'Employee_ID' => 'Sale Person',
'PaymentModeID' => 'Payment Mode',
'DateJoin' => 'Date Join',
'PcBottle' => 'Pc Bottle',
'Bottle6Ltr' => 'Bottle6 Ltr',
'Bottle1500Ml' => 'Bottle1500 Ml',
'Bottle500Ml' => 'Bottle500 Ml',
'TabStand' => 'Tab Stand',
'Pump' => 'Pump',
'DispensirStatus' => 'Dispensir Status',
'PCBottlesQuantity' => 'Pcbottles Quantity',
'PCBottleSecurity' => 'Pcbottle Security',
'DispensirQuantity' => 'Dispensir Quantity',
'DispensirSerialNo' => 'Dispensir Serial No',
'DispensirSecurity' => 'Dispensir Security',
'TotalAmount' => 'Total Amount',
'DeliveryDay' => 'Delivery Day',
'Description' => 'Description',
'Status' => 'Status',
'DateCreated' => 'Date Created',
);
}
public function search()
{
$criteria=new CDbCriteria;
$criteria->compare('CustomerAccountID',$this->CustomerAccountID);
$criteria->compare('CustomerID',$this->CustomerID);
$criteria->compare('Employee_ID',$this->Employee_ID);
$criteria->compare('PaymentModeID',$this->PaymentModeID);
$criteria->compare('DateJoin',$this->DateJoin,true);
$criteria->compare('PcBottle',$this->PcBottle);
$criteria->compare('Bottle6Ltr',$this->Bottle6Ltr);
$criteria->compare('Bottle1500Ml',$this->Bottle1500Ml);
$criteria->compare('Bottle500Ml',$this->Bottle500Ml);
$criteria->compare('TabStand',$this->TabStand);
$criteria->compare('Pump',$this->Pump);
$criteria->compare('DispensirStatus',$this->DispensirStatus);
$criteria->compare('PCBottlesQuantity',$this->PCBottlesQuantity);
$criteria->compare('PCBottleSecurity',$this->PCBottleSecurity);
$criteria->compare('DispensirQuantity',$this->DispensirQuantity);
$criteria->compare('DispensirSerialNo',$this->DispensirSerialNo);
$criteria->compare('DispensirSecurity',$this->DispensirSecurity);
$criteria->compare('TotalAmount',$this->TotalAmount);
$criteria->compare('DeliveryDay',$this->DeliveryDay,true);
$criteria->compare('Description',$this->Description,true);
$criteria->compare('Status',$this->Status);
$criteria->compare('DateCreated',$this->DateCreated,true);
return new CActiveDataProvider($this, array(
'criteria'=>$criteria,
));
}
public static function model($className=__CLASS__)
{
return parent::model($className);
}
}
当我调用操作更新时,任何人都可以告诉我如何从数据库表中显示选中的值?
您必须取消序列化的复选框值afterFind()
模型文件(即模型/客户帐户.php)中起作用,如下所示:
public function afterFind() {
$this->DeliveryDay = unserialize($this->DeliveryDay);
parent::afterFind();
}