如何在Yii 1.6中选中复选框?

时间:2016-02-19 21:30:38

标签: php checkbox yii

我是Yii Framework的初学者。我用Yii小部件CActiveForm创建了_form.php。我已经制作了一周的复选框列表。我已经在actioncreate上序列化了复选框值并保存到数据库表中。实际上问题是当我调用actionupdate时,我的检查值没有显示我在actioncreate.actioncreate和actionupdate上使用相同的表单_form.php进行插入。

我在下面编写了我的代码

  1. actioncreate

    public function actionCreate() {     $ model = new 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

    public function 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这个

  4. <?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时,是否有人可以告诉我如何从数据库表中显示选中的值?

1 个答案:

答案 0 :(得分:0)

您必须在模型文件中使用未序列化的复选框值afterFind()函数(例如models / CustomerAccounts.php),如下所示:

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