Loading Data From Another Table In Yii

I am totally new in Yii framework and quite frankly do not know everything about it. I have an ongoing project in which I am stuck in a problem. I have 3 tables in the project. Here are the table names and the rows:

Users (id, name, email, username, password, currentTIme) Uses for storing user information.





Groups (id, name, currentTIme) Uses for storing Group Information.





UserGroup (id, userId, groupId, currentTime) Uses for keeping the relation between user and group.

Now, what I have to do is after creating 3 models, Users and Groups model will be used for all users. Different groups was added like:

  1. CSE 02. EEE 03. BBA 04. Architecture etc. etc.

Now, when ‘User create’ portion 2 things have to happen. One, the group names (CSE, EEE, BBA, Architecture etc.) will be shown in a listbox to choose along with the user’s other information (name, password etc.). Now question is How I can show the name of Groups in a listbox in the model Users where there is no relationship.

Second problem is, when I will click ‘submit’ it will update 2 tables ‘Users’ and ‘UserGroup’. Name, password,currentTime will be saved in ‘User’ and userid(of current user), groupid(id of the groups user will be selected like 1 for CSE etc.) will be saved in ‘UserGroup’ table.

Thank you for tolerating my horrible English and any kind of comment/feedback will be a big help for me…Thanks in advance

I found out answer of the first part which shows the list from another model. For anybody like me, it can be useful i am posting it here.

in the /models/users.php:

class Users extends CActiveRecord { public $Groups; … … }

in the /view/users/_form.php:

<div class="row">

<?php echo $form->labelEx($model,‘Group’); ?>

<?php

echo $form->dropDownList($model, ‘Groups’,

CHtml::listData(Groups::model()->findAll(),‘id’,‘name’));

//echo $form->;dropDownList($model,‘Groups’, CHtml::listData(Groups::model()->findAll(), ‘id’, ‘value’), array(‘empty’=>;’–please select–’));

?>

<?php echo $form->error($model,‘password’); ?>

</div>

Hello there,

The thing is, since the table should have some relationship, I see the table UserGroup is related to both users and groups; while creating the model, it will define relationship in the class itself. If not, then please define the relationship between tables. Please see the documentation for the relationship. Once, you have relationship defined, everything else should be pretty simple.

Thanks.

At first, Thanks for your kind reply. Here, I have relationship between user and usergroup and group and usergroup. But I do not know how to set the relationship in the models(I tried, it did not work). and what else besides relationship I have to do.

again thanks for giving this novice your feedback

If anybody needs help, thats why I am posting the answer. Solved it while ago but because I was busy, I could not give the answer. Sorry for that.

ANSWER:

all you have to do, need to create a variable model2 and load the posted value in your expected model through that variable (model2). Sorry for bad English 8)

UserController.php

public function actionCreate()

{


	&#036;model=new Users;





	// Uncomment the following line if AJAX validation is needed


	// &#036;this-&gt;performAjaxValidation(&#036;model);





	if(isset(&#036;_POST['Users']))


	{	





		&#036;model-&gt;attributes=&#036;_POST['Users'];


		


		if(&#036;model-&gt;save())


		{


			//do not need for you, i needed to do it for upload purpose


			// &#036;userid = &#036;model-&gt;primaryKey;


			


			foreach (&#036;_POST['Users']['groupId'] as &#036;k=&gt;&#036;v)


				{





					&#036;model2 = new Usergroup;


					&#036;model2-&gt;userId = &#036;userid ;


					&#036;model2-&gt;groupId = &#036;v;


					&#036;model2-&gt;save();


				}


			//do not need for you, i needed to do it for upload purpose


			//	mkdir (&quot;./Upload/&#036;userid&quot;, 0700);


				


			&#036;this-&gt;redirect(array('view','id'=&gt;&#036;model-&gt;id));


			}


	}





	&#036;this-&gt;render('create',array(


		'model'=&gt;&#036;model,


	));


}

@Saqib,

First you create relations on database level. then run the gii for CRUD. it will automatically create your relations among models(tables).

so by using relations u can easily manager your data and dropdown and everything u want to related relational active record.

First you create CRUD then let me know what needed you???