Yii Framework Forum: activeCheckBoxList error - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

activeCheckBoxList error Rate Topic: -----

#1 User is offline   Nique 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 41
  • Joined: 10-May 09
  • Location:the Netherlands

Posted 21 May 2009 - 07:08 PM


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
0

#2 User is offline   qiang 

  • Yii Project Lead
  • Yii
  • Group: Yii Dev Team
  • Posts: 5,902
  • Joined: 04-October 08
  • Location:DC, USA

Posted 21 May 2009 - 08:11 PM

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.
1

#3 User is offline   Nique 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 41
  • Joined: 10-May 09
  • Location:the Netherlands

Posted 22 May 2009 - 02:28 PM

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?
0

#4 User is offline   wyldie 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 29
  • Joined: 22-May 09

Posted 16 June 2009 - 10:37 AM

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.
2

#5 User is offline   Attilio 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 98
  • Joined: 10-May 11
  • Location:Bari, Italy and Guatemala, Guatemala

Posted 08 June 2011 - 01:24 PM

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?
Extension yii-jpegcam: embed a webcam and take photos
0

#6 User is offline   Hernan Garcia 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 4
  • Joined: 16-July 11

Posted 31 July 2011 - 05:26 PM

View PostAttilio, on 08 June 2011 - 01:24 PM, said:

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.
0

#7 User is offline   C.S.Putera 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 92
  • Joined: 18-May 11

Posted 28 September 2011 - 01:59 AM

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 :)
0

#8 User is offline   jwinn 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 17
  • Joined: 09-July 11
  • Location:Florida, USA

Posted 16 November 2011 - 10:40 PM

View Postwyldie, on 16 June 2009 - 10:37 AM, said:

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

<pre class='prettyprint'><?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;
        }
    }

...</pre>



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

As for saving and updating, I found this article useful:
http://www.larryullm...s-in-yii-forms/
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users