Help With The Searching

I have 3 tables like this:


User: id

Skill: id

UserSkill: id, user_id, skill_id

So UserSkill table links User and Skill table together with the relationship: User can has many Skills. It means User has many UserSkill and Skill has many UserSkill.

I would like to do something like: when I view User id=2, it must display the user detail of course, and list all the skills that link to this user. I dont know how to do it. Please so me the solution for both controller and view. Thanks :)

First off, be sure you’ve read through and understood most of this: http://www.yiiframew…en/database.arr. Particularly, everything that describes the relationship between posts and categories in the example scenario.

Your words seem to only describe the need for a one to many relationship, but your tables show a many to many structure, so I will assume you have need for a many to many relationship.

In your models/User.php file you should have a relationship defined for joining users and skills with a join table, similar to this:


'skills' => array(self::MANY_MANY, 'Skill', 'UserSkill(user_id, skill_id)'),

Then the opposite relationship defined in your models/Skill.php file.

In your controller a default generated view action will load the requested users data into the views/view.php view file:




public function actionView($id)

{

	$this->render('view',array(

		'model'=>$this->loadModel($id), // $model will be the user in your views.view.php file

	));

}



Then in your user/view.php file you can do something very simple, like below, to show a users first name and a list of their related "skills" names below:




<h2><?php echo $model->first_name ?></h2>


<ul class='user-skills'>

<?php

	$skills = $model->skills; // "$model->skills" is getting an array of "skills" objects, through the relationship you defined in your models/User.php file above

	foreach($skills as $skill)

	{

		echo '<li>' . $skill->name . '</li>';

	}

?>

</ul>



NOTE: You will need to add some test records to your join table for the view file to actually display a users skills. So, don’t forget to create a couple of test users, some test skills, and add some records to your join table, effectively completing the relationship between users and skills.

Hopefully that helps clear things up, but let me know if I missed something that you’re stuck on.

Awesome, it worked like charm, thank you so much. But I have other question is: what are the type of the $skill now? if it is Object, can I assign it to a variable. Should be like that:


               $test =$this->loadModel($id);$test1 = new Skill; $test1 = $test1->skills; 

[size=2]Yes, the $skill variable inside of the foreach loop contains a single Skill object: but, I can’t think of any reason you would want to [/size]re-[size=2]assign the $skill variable to another variable as that would just be redundant?[/size]

Please clarify the goal you want to accomplish if you need more help.

Thank you, I will think about it and get back to you later :))

Im thinking about how the relationship work. Does it work with one to many or one to one. This mean if I have only User and UserSkill so it must be one to many which declare as: .


'userskill' => array(self::HASMANY, 'UserSkill', 'user_id');

So when I run:$test =$this->loadModel($id) in the User controller, could the $test has attribute called userskill which contain array of UserSkill Objects?

Hi Beninback, can you help me one more? Now it becomes a little bit more complex. The user needs to be checked their skill that it is verified, onAssessment or notVerified. It means there must be some status attributes to deal with this. Im thinking about put it in the UserSkill table, but not sure that it will break the MANY-MANY relationship before or not? Is there any solution to solve this issue?

Alright, I’m back from a little break from my computer.

Yes, that is correct.

Unfortunately I’m not very familiar with building that kind of scenario personally: but, I think the yii blog tutorial Comment section could be relevant reading material for you. If you haven’t already, I highly recommend running through building the blog system, at least a few times over.

http://www.yiiframew…1/en/post.model <<< shows setting "status" conditions in model relations, and defining status constants in model

http://www.yiiframew.../comment.create // shows setting status for comments being added,

Essentially, the blog system makes use of constants defined in the respective model classes [size="2"]to define post and comment status(i.e. pending or approved) and those values are stored in the database. I think this is similar to what you want to do?[/size]

Thank you very much, ben. Your help very much appreciated.