activeCheckBoxList error

Hi there,

I'm currently stuck with creating a functional activeCheckBoxList in my form view.

The fallowing code does work for the create view, but unfortunately, it doesn't work for the update view. Let me explain a little about the setup: I want to show a 'checkboxlist' so users can select more than one category. Yii threw me this error when i navigated to the 'update' action:

Quote

Object of class Category could not be converted to int

Model (Article.php)



<?php


public function relations()


{


	return array(


		'author'=>array(self::BELONGS_TO, 'User', 'authorId'),


		'comments'=>array(self::HAS_MANY, 'Comment', 'articleId',


			'order'=>'??.createdOn ASC'),


		'categories'=>array(self::MANY_MANY, 'Category','news_article_category(articleId, categoryId)',


			'order'=>'name'),


		'categoryFilter'=>array(self::MANY_MANY, 'Category', 'news_article_category(articleId,categoryId)',


			'together'=>true,


			'joinType'=>'INNER JOIN',


			'condition'=>'??.name=:category'),


	);


}


?>


View (_form.php)



<ul><?php echo CHtml::activeCheckboxList(


	$article,


	'categories',


	CHtml::listData(


		Category::model()->findAll(),


		'id',


		'name'


	),


	array(


		'template'=>'<li>{input} {label}</li>',


	)


); ?></ul>


As you can see, i used listData to create the array for the label and value of checkboxes. This works in article/create, but it doesnt in article/update

Does someone have a clue for this? Thank you

You cannot use "categories" here because it is an array of related objects. activeCheckboxList expects it to be an array of strings or integers. You may consider defining a property named "categoryIDs" in the model which returns/stores the selected category IDs.

Quote

You cannot use "categories" here because it is an array of related objects. activeCheckboxList expects it to be an array of strings or integers. You may consider defining a property named "categoryIDs" in the model which returns/stores the selected category IDs.

Thanks,

Do you have a little example on how to do this? It's not that i am lazy but i'm just a newbe. Should i take a look at the way 'tags' are being handled in the blog example?

In your Article model add a public field which does not already exist in you Article table.

<?php


class Article extends CActiveRecord


{


    public $categoryId=array();





    public function afterFind()


    {


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


        {


            foreach($this->category as $n=>$category)


                $this->category[]=$category->id;


        }


    }





...

In the afterFind method we are checking if our MANY_MANY join is populated at all. If it is then we iterate over each value and append the ID to our internal $categoryId array.

Now update your _form.php view to the following

<ul><?php echo CHtml::activeCheckboxList(


   $article,


   'categoryId',


   CHtml::listData(


      Category::model()->findAll(),


      'id',


      'name'


   ),


   array(


      'template'=>'<li>{input} {label}</li>',


   )


); ?></ul>

Realise this is a bit late in a reply and no doubt you figured this out, but I hope it helps others with a similar problem.

wonderful! now how can I get relations saved when I submit the form? I mean - following the upper example - what shall I do with ‘categoryId’ in the update or create actions?

Hello, I was wondering if you get the problem solved. If so, can you explain hoe, please? I have the same issue right now.

Hi. I’m facing the same issue here.

After I check the HTML it looks like this :




<div class="row">


<input id="ytMember_categoryIds" type="hidden" value="" name="Member[categoryIds]" />


<input id="Member_categoryIds_0" value="1" type="checkbox" name="Member[categoryIds][]" /> 

<label for="Member_categoryIds_0">Food & Beverages</label><br/>


<input id="Member_categoryIds_1" value="2" checked="checked" type="checkbox" name="Member[categoryIds][]" /> <label for="Member_categoryIds_1">Health & Beauty</label><br/>


<input id="Member_categoryIds_2" value="3" type="checkbox" name="Member[categoryIds][]" /> 

<label for="Member_categoryIds_2">Fashion & Appearel</label><br/>


<input id="Member_categoryIds_3" value="4" type="checkbox" name="Member[categoryIds][]" /> 

<label for="Member_categoryIds_3">Electronic</label><br/>


<input id="Member_categoryIds_4" value="5" type="checkbox" name="Member[categoryIds][]" /> 

<label for="Member_categoryIds_4">Travel & Hotel</label><br/>


<input id="Member_categoryIds_5" value="6" type="checkbox" name="Member[categoryIds][]" /> 

<label for="Member_categoryIds_5">Others</label><br/>


<input id="Member_categoryIds_6" value="7" checked="checked" type="checkbox" name="Member[categoryIds][]" /> <label for="Member_categoryIds_6">All</label>	

	                

</div>




I dont know why, but there is extra "[ ]" after the text field name.

So, in my controller, when i do this :





echo $member['categoryIds'];




it still shows the old value, not the ones that I just ticked.

Compare with this :





<div class="row">


<label for="Member_member_gender">Gender</label>  

              

<div class="compactRadioGroup">


<input id="ytMember_member_gender" type="hidden" value="" name="Member[member_gender]" />   

    

<input id="Member_member_gender_0" value="1" checked="checked" type="radio" name="Member[member_gender]" /> <label for="Member_member_gender_0">Male</label> 


<input id="Member_member_gender_1" value="0" type="radio" name="Member[member_gender]" /> 

<label for="Member_member_gender_1">Female</label>                


</div>


</div>




There is no extra “[ ]” after the field name. Any idea how to correct this? Thanks :)

I believe there is a typo here. It should be $this->categoryId[]=$category->id.

As for saving and updating, I found this article useful: