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:
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)
'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>
$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>';
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.
[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.
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
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]