Yii Framework Forum: PHP Error: "Object of class Group could not be converted to int" WHEN i try to use CHtml - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • This topic is locked

PHP Error: "Object of class Group could not be converted to int" WHEN i try to use CHtml Rate Topic: -----

#1 User is offline   oalexandrino 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 16
  • Joined: 11-June 09

Posted 29 July 2009 - 09:33 AM

people, i have been tryed to use CHtml to generate HTML componentes in a scenario that I have a MANY_TO_MANY relation.

it works
    	<?php
    	 foreach($model->groups as $index => $oGroup)
    	 {
    	 	print( $oGroup->name ) . "<br>";
    	 }

    	?>


it doesn't work

<?php
$data = CHtml::listData($model->groups, 'id','name');
print_r($data);

echo CHtml::activeListBox($model,'groups',$data );
?>  


and I get this error

PHP Error
Description

Object of class Group could not be converted to int


bellow is User's relations method

	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'posts'  => array ( self::HAS_MANY, 'Post', 'authorId'),
			'groups' => array ( self::MANY_MANY, 'Group', 'UserGroup(userId, groupId)',
				'together'=>true,
				'joinType'=>'INNER JOIN'),		

		);
	}


im not being able to show in an activeCheckBoxList all groups of a User. that's the question.

Any ideas?
"no one person is smart enough or has enough knowledge to figure out everything without assistance"
0

#2 User is offline   oalexandrino 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 16
  • Joined: 11-June 09

Posted 29 July 2009 - 03:37 PM

does anybody have any solution, plz?

View Postoalexandrino, on 29 July 2009 - 09:33 AM, said:

people, i have been tryed to use CHtml to generate HTML componentes in a scenario that I have a MANY_TO_MANY relation.

it works
    	<?php
    	 foreach($model->groups as $index => $oGroup)
    	 {
    	 	print( $oGroup->name ) . "<br>";
    	 }

    	?>


it doesn't work

<?php
$data = CHtml::listData($model->groups, 'id','name');
print_r($data);

echo CHtml::activeListBox($model,'groups',$data );
?>  


and I get this error

PHP Error
Description

Object of class Group could not be converted to int


bellow is User's relations method

	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'posts'  => array ( self::HAS_MANY, 'Post', 'authorId'),
			'groups' => array ( self::MANY_MANY, 'Group', 'UserGroup(userId, groupId)',
				'together'=>true,
				'joinType'=>'INNER JOIN'),		

		);
	}


im not being able to show in an activeCheckBoxList all groups of a User. that's the question.

Any ideas?

"no one person is smart enough or has enough knowledge to figure out everything without assistance"
0

#3 User is offline   qiang 

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

Posted 29 July 2009 - 06:56 PM

It doesn't work because in activeListBox(), the second parameter should be an attribute name, not a relation name. In your case, you may need to declare a new property to store the selections.
0

#4 User is offline   oalexandrino 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 16
  • Joined: 11-June 09

Posted 31 July 2009 - 01:48 PM

hi qiang.

im sorry, I could let it works but i still not being able to get the result what i expect.

I did what you said, that is i've put the following fragment inside User model

	
private $groups;
	
	/**
	 * @return string the associated database table name
	 */
	public function getGroups()
	{
		return $this->groups;
	}


this is the action's code

	public function actionAdmin()
	{
		$this->processAdminCommand();

		$criteria=new CDbCriteria;

		$pages=new CPagination(User::model()->count($criteria));
		$pages->pageSize=self::PAGE_SIZE;
		$pages->applyLimit($criteria);

		$sort=new CSort('User');
		$sort->applyOrder($criteria);

		$models=User::model()->findAll($criteria);

		$this->render('admin',array(
			'models'=>$models,
			'pages'=>$pages,
			'sort'=>$sort,
		));
	}


this is a view's fragment code
<table class="dataGrid">
<?php foreach($models as $n=>$model): ?>
   <tr class="<?php echo $n%2?'even':'odd';?>">    
    <th><?php echo $sort->link('Groups'); ?></th>
    <td>
   
		<?php
		$data = CHtml::listData($model->groups, 'id','name');
		//$arrGroups  = Group::model()->CategoryActive();
		
		$arrGroups = array('checked'=>CHtml::listData($model->groups,'id','name'));
		//print_r($arrGroups);
		print_r($data);
		//$allowedCategories  = array('key1'=>'val1', ...);
		echo CHtml::activeCheckBoxList(Group::model(),'id',$data );
		echo CHtml::activeCheckBoxList($model,Group::model()->findAll(),$data );
		?>    	
	</td>
  </tr>

<?php endforeach; ?>
</table>


both of the echo above work, but I didn't understand yet how to apply the simple requirement.
I'd like to show all available Groups, and I wanna check just the groups that the current user belongs to.



View Postqiang, on 29 July 2009 - 06:56 PM, said:

It doesn't work because in activeListBox(), the second parameter should be an attribute name, not a relation name. In your case, you may need to declare a new property to store the selections.

"no one person is smart enough or has enough knowledge to figure out everything without assistance"
0

#5 User is offline   oalexandrino 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 16
  • Joined: 11-June 09

Posted 02 August 2009 - 02:09 PM

hi Qiang,

i have fixed the problem, but to do that i had to take a look at the framework code in order to understand better how the component works.

but i was unable to make activeCheckBoxList working, so i chose CheckBoxList instead because it was enough to me.

that's why i posted the following item in the forum.


http://www.yiiframew...__1


View Postoalexandrino, on 31 July 2009 - 01:48 PM, said:

hi qiang.

im sorry, I could let it works but i still not being able to get the result what i expect.

I did what you said, that is i've put the following fragment inside User model

	
private $groups;
	
	/**
	 * @return string the associated database table name
	 */
	public function getGroups()
	{
		return $this->groups;
	}


this is the action's code

	public function actionAdmin()
	{
		$this->processAdminCommand();

		$criteria=new CDbCriteria;

		$pages=new CPagination(User::model()->count($criteria));
		$pages->pageSize=self::PAGE_SIZE;
		$pages->applyLimit($criteria);

		$sort=new CSort('User');
		$sort->applyOrder($criteria);

		$models=User::model()->findAll($criteria);

		$this->render('admin',array(
			'models'=>$models,
			'pages'=>$pages,
			'sort'=>$sort,
		));
	}


this is a view's fragment code
<table class="dataGrid">
<?php foreach($models as $n=>$model): ?>
   <tr class="<?php echo $n%2?'even':'odd';?>">    
    <th><?php echo $sort->link('Groups'); ?></th>
    <td>
   
		<?php
		$data = CHtml::listData($model->groups, 'id','name');
		//$arrGroups  = Group::model()->CategoryActive();
		
		$arrGroups = array('checked'=>CHtml::listData($model->groups,'id','name'));
		//print_r($arrGroups);
		print_r($data);
		//$allowedCategories  = array('key1'=>'val1', ...);
		echo CHtml::activeCheckBoxList(Group::model(),'id',$data );
		echo CHtml::activeCheckBoxList($model,Group::model()->findAll(),$data );
		?>    	
	</td>
  </tr>

<?php endforeach; ?>
</table>


both of the echo above work, but I didn't understand yet how to apply the simple requirement.
I'd like to show all available Groups, and I wanna check just the groups that the current user belongs to.

"no one person is smart enough or has enough knowledge to figure out everything without assistance"
0

#6 User is offline   mikelimassol 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 8
  • Joined: 08-January 10

Posted 19 January 2010 - 06:39 PM

View Postqiang, on 29 July 2009 - 06:56 PM, said:

It doesn't work because in activeListBox(), the second parameter should be an attribute name, not a relation name. In your case, you may need to declare a new property to store the selections.


i have the same issue here, can you please post a sample code on how to do this, will this property return an array with the associated keys??
0

#7 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 - 10:17 AM

View Postoalexandrino, on 02 August 2009 - 02:09 PM, said:

i have fixed the problem


how? could you post it?
Extension yii-jpegcam: embed a webcam and take photos
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • This topic is locked

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