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