form with multiple relations

I’m new to Yii and in trouble with relations and a special view for editing and creating.

My entities:








My relations:


devices => array(self::HAS_MANY, Device, customer_id),

profiles => array(self::HAS_MANY, Profile, customer_id),


devices => array(self::HAS_MANY, Device, profile_id),

customer => array(self::BELONGS_TO, Customer, customer_id),


customer => array(self::BELONGS_TO, Customer, customer_id),

profile => array(self::BELONGS_TO, Profile, profile_id),

apps => array(self::MANY_MANY, App, profile_app(profile_id, app_id)),


appLabels => array(self::HAS_MANY, AppLabel, app_id),

devices => array(self::MANY_MANY, ‘Device’, device_app(app_id, device_id)),

profiles => array(self::MANY_MANY, Profile, profile_app(app_id, profile_id)),


no relations


no relations


app => array(self::BELONGS_TO, App, app_id),

I can create/edit customers, devices so far. But now i have a problem: when creating a profile it should be possible to associate apps to it. I tried to solve this with CActiveForm and a checkBoxList and ran in two major problems. On the one hand is such a list not very usefull, because there are about hundreds of apps in this list and on the other hand i didn’t ‘find’ the right code for checked oder unchecked boxes. Code from this(_form in profile):

$form->checkBoxList($model,‘id’, CHtml::listData(App::model()->findAll(array(‘order’=>‘package_name’)), ‘id’,‘package_name’),array(‘multiple’=>‘multiple’,‘template’=>’<li>{input} {label}</li>’,‘labelOptions’=>array(‘style’=>‘display:inline’),))

This will compare the id of apps with id of profile… :frowning:

I think about a better way to make a view with CGridView, where you can see all active apps first and afterwards all other apps with checkboxes and a routine to save them.

Any ideas or advices much appreciated!

Dear friend,

I am not able to comprehend the exact problem you are facing.

Here I am setting a scenario.

Kindly check whether it suits you.

Take the blog example.

There are number of comments remain unapproved.

I want all the unapproved comments listed in a page along with title of the post,content of comment, name and email of person who made the comment.There is also radiobutton to check for making the comment approved.


public function actionApproval() 


        //status1=unapproved; status2=approved

	$dataProvider=new CActiveDataProvider('Comment',array(



                        'order'=>'create_time DESC',




	{   $this->render('approval',array(







		foreach($_POST['status'] as $i=>$j) 


			if($j==1) //boolean selected from radiobutton











This is approval.php utilising the power of CListView.



	'Manage Comments',



	array('label'=>'Create Comment', 'url'=>array('create')),

	array('label'=>'Manage Comment', 'url'=>array('admin')),




<?php if(count($dataProvider->getData())>0) {?>

<h1>Comments Not Approved</h1>

<?php } else echo "<div class=\"flash-success\"><h4>All the comments are approved</h4></div>"; ?>

<div class="form">

<?php echo CHtml::beginForm(); ?>

<?php $this->widget('zii.widgets.CListView', array(



)); ?>

<?php if(count($dataProvider->getData())>0) echo CHtml::submitButton('Save'); ?>

<?php echo CHtml::endForm(); ?>


This is _approval.php to dispaly the form.

<div class="view">

<div style="float:right;">

<?php echo CHtml::encode('approve');?>

<?php echo CHtml::checkBox("status[$data->id]",false,array('separator'=>'')); ?>


<b><?php echo CHtml::encode($data->getAttributeLabel('author')); ?>:</b>

<?php echo CHtml::encode($data->author); ?>

<br />

<b><?php echo CHtml::encode($data->getAttributeLabel('email')); ?>:</b>

<?php echo CHtml::encode($data->email); ?>

<br />

<b><?php echo CHtml::encode($data->getAttributeLabel('content')); ?>:</b>

<?php echo CHtml::encode($data->content); ?>

<br />

<b><?php echo CHtml::encode($data->getAttributeLabel('post.title')); ?>:</b>

<?php echo CHtml::encode($data->post->title); ?>

<br />


In a similar manner I can manage posts by using radiobutton list classifying them into draft,published or archived.

If you have a select box list or checkbox list with LOTS of choices it makes more sense to use one of the jquery multiselects people have built. They usually have a built-in filter.

You can also try ‘multicomplete’ which is an autocomplete extension that lets you save multiple instances.