many to many with checkboxes

Hi Friends,

I am working on CRUD part in admin side. I have 3 models in hand (many-many relations).

I was referring http://www.yiiframework.com/forum/index.php/topic/16936-many-to-many/ thread. It helps me a lot.From this thread I am successfully able to save selected checkboxes value in my table. Now when admin edit that record old checkbozes should remain selected, but it is not working.

Below are my files. I have installed CAdvancedArBehavior extension.

Model: User.php


public $loantypeIds = array();

public function behaviors()

    {

        return array(

            'CAdvancedArBehavior' => array(

                'class' => 'application.extensions.CAdvancedArBehavior'));

    }

	public function afterFind()

    {

        if(!empty($this->services))

        {

            foreach ($this->loantypes as $n => $loantype)

                $this->loantypeIds[] = $loantype->loantypeId;

        }

        parent::afterFind();

    }

// rules

array('loantypeIds', 'safe'),


// relations

'loantypes' => array(self::MANY_MANY, 'UserLoanRequestType', 'tbl_user_loan_request_type(userId, loanTypeId)'),




Model: LoanType.php




// relations

'users' => array(self::MANY_MANY, 'User', 'tbl_user_loan_request_type(loanTypeId, userId)'),



Model: UserLoanRequestType.php




// relations

'user' => array(self::BELONGS_TO, 'User', 'userId'),

			'loanType' => array(self::BELONGS_TO, 'LoanType', 'loanTypeId'),



View: _form.php




<div class="row">

        <?php echo $form->labelEx($model, 'loantypes'); ?>

        <?php echo $form->checkBoxList($model, 'loantypeIds',

            CHtml::listData(LoanType::model()->findAll(), 'loanTypeId', 'value'),

            array('checkAll' => 'Check All')); ?>

        <?php echo $form->error($model, 'loantypes'); ?>

    </div>	



Thanks,

Vibha





public function afterFind()

{

    if(!empty($this->services))

    {

    foreach ($this->loantypes as $n => $loantype)

        $this->loantypeIds[] = $loantype->loantypeId;

    }

    parent::afterFind();

}



should be





public function afterFind()

{

    if(!empty($this->loantypes))

    {

    foreach ($this->loantypes as $n => $loantype)

        $this->loantypeIds[] = $loantype->loantypeId;

    }

    parent::afterFind();

}



Ohh, thanks, It was typo.

I have changed that and now when i print this array, it still return blank.




public function afterFind()

    {

        if(!empty($this->loantypes))

        {

            foreach ($this->loantypes as $n => $loantype)

                $this->loantypeIds[] = $loantype->loantypeId;

        }

		print_r($this->loantypes);

        parent::afterFind();

    }

Do i need to make any changes in actionUpdate function ??

Even if i am printing loantype by below code, it is giving blank array.




$user = User::model()->findByPk(5);	

print_r($user->loantypes);



Yes - you need to set the Ids like so:





public function actionUpdate($id)

    {

        $model = $this->loadModel($id);


        if (isset($_POST['Portfolio']))

        {

            $model->attributes = $_POST['Portfolio'];

            $model->services = $model->serviceIds; // Add Ids here


            if ($model->save())

                $this->redirect(array('view', 'id' => $model->id));

        }


        $this->render('update', array(

            'model' => $model,

        ));

    }

You have added code for updating in database.

But i have issue to fetch data and make checkboxes selected before clicking on update button.

View file:


<?php echo $form->checkBoxList($model, 'loantypeIds',

            CHtml::listData(LoanType::model()->findAll(), 'loanTypeId', 'value'),

            array('checkAll' => 'Check All')); ?>

With CAdvancedArBehavior, you don’t need the UserLoanRequestType model - just the two parent models; User & LoanType.

Also, your M-M relationship should point to LoanType and not UserLoanRequestType.

[color=#008800]‘loantypes’[/color][color=#000000] [/color][color=#666600]=>[/color][color=#000000] array[/color][color=#666600]([/color][color=#000088]self[/color][color=#666600]::[/color][color=#000000]MANY_MANY[/color][color=#666600],[/color][color=#000000] [/color][color=#008800]‘LoanType’[/color][color=#666600],[/color][color=#000000] [/color][color=#008800]‘tbl_user_loan_request_type(userId, loanTypeId)’[/color][color=#666600]),[/color]

It Works… I was unnecessarily struggling with UserLoanRequestType.

Thanks for your time, you have checked all my code and helped me. Appreciated ;)