Junaid Usmani
Junaid Usmani

Reputation: 139

How to checkboxlist checked in Yii 1.6?

I am beginner in Yii Framework.I have created the _form.php with Yii widget CActiveForm.I have made the Checkbox list for week of days.I have serialized checkbox values on actioncreate and save into database table.Actually problem is that When i calling the actionupdate then my checked values are not showing that i have insert on actioncreate.actioncreate and actionupdate using the same form _form.php.

I have written my code below

  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) { $model=$this->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(); ?>

Here my model

class CustomerAccounts extends 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);
}

}

Would anyone Can tell me when i call the actionupdate How the checked values will be shows from database table ?

Upvotes: 0

Views: 534

Answers (1)

AddWeb Solution Pvt Ltd
AddWeb Solution Pvt Ltd

Reputation: 21681

You have to unserialized checkbox values afterFind() function in your model file (i.e. models/CustomerAccounts.php) as follows:

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

Upvotes: 0

Related Questions