2016-02-19 70 views
0

我是Yii Framework的初学者。我创建了带有Yii小部件的_form.php CActiveForm.I已经制作了复选框列表for days days.I已经在actioncreate上的序列化复选框值并保存到数据库表。其实问题是,当我调用actionupdate然后我的检查值没有显示我已经插入actioncreate.actioncreate和actionupdate使用相同的形式_form.php。如何在Yii 1.6中选中checkboxlist?

我写我的代码如下

  1. actioncreate

    公共职能actionCreate(){ $ 模型=新CustomerAccounts;

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

    }

  2. actionupdate

    公共函数actionUpdate($ ID) { $模型= $这 - > loadModel($ 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, 
    )); 
    

    }

  3. _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>&nbsp;&nbsp;<td> {label}</td>&nbsp;&nbsp;</tr', 'multiple' => true, 'checked' => 'checked'); 
      echo $form->checkBoxList($model, 'DeliveryDay', Yii::app()->params['WeekDays'], $htmlOptions); 
      ?> 
     </div></div></div> 

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

这里我的模型

类CustomerAccounts扩展的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); 
} 

}

请问谁能告诉我,当我打电话actionupdate如何检查的值将会从数据库表显示?

+0

你反序列化值之前显示编辑表单?展示你的模型。 – SiZE

+0

我有更新模型在我的问题。我没有未显示的值之前显示编辑窗体。请你告诉我如何我可以显示编辑窗体上的值? –

+0

例如,进入'afterFind':'public function afterFind(){parent :: afterFind(); $ this-> DeliveryDay = unserialize($ this-> DeliveryDay); }' – SiZE

回答

0

你必须序列化的复选框,在模型文件中的值afterFind()功能(即型号/ CustomerAccounts.php)如下:

public function afterFind() { 
    $this->DeliveryDay = unserialize($this->DeliveryDay); 
    parent::afterFind(); 
} 
相关问题