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?
does anybody have any solution, plz?
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?
qiang
(Qiang Xue)
July 29, 2009, 11:56pm
3
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.
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.
qiang:
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.
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.yiiframework.com/forum/index.php?/topic/3543-a-suggestion-for-yii-documentation/page__p__19200__fromsearch__1&#entry19200
oalexandrino:
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.
qiang:
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??