redbox
(Deepali Mardikar)
August 8, 2011, 6:29am
1
Hi All,
I have created simple CRUD pages for school and student. Now on the student page, i want to pull the data from the school table so while creating the student i can add that particular student into the school(want to insert the school_id)
SO i just wanted to ask you that what database structure i should write for the both table? and what kind of changes i need to do in the form.php(student) so i can see the other tables(school names) value as well?
Please i will appreciate if you will help me to start this two table relationship module.
Thanks in advance.
This is not a bug ;-).
But this should help:
<div class="row">
<?php echo $form->labelEx($model, 'user_id'); ?>
<?php echo $form->dropDownList($model, 'user_id', CHtml::listData(User::model()->findAll(), 'id', 'name')); ?>
<?php echo $form->error($model, 'user_id'); ?>
</div>
where user_id is the foreign key in a table referencing User. In your case it should be school_id, and you’d need to call School::model()->findAll().
Note that you really only want to SELECT the fields you actually use; the findlAll() call returns all DB-fields. But it works.
mdomba
(Maurizio Domba Cerin)
August 8, 2011, 7:17am
3
NOTE: moved to proper sub-forum (General discussion instead of Bug discussions)
redbox
(Deepali Mardikar)
August 8, 2011, 7:20am
4
Yes!! I am getting the values in the drop down box. thanks a lot.
Now while creating i want to insert that (school_id) in to student table as well.
Could you please tell me how i can get the id as hidden or something so i can insert that into the student table?
redbox
(Deepali Mardikar)
August 8, 2011, 9:06am
5
I got the values. Just check your model/yourfilename.php page and check the rules() function where we need to set the values.
redbox:
Yes!! I am getting the values in the drop down box. thanks a lot.
Now while creating i want to insert that (school_id) in to student table as well.
Could you please tell me how i can get the id as hidden or something so i can insert that into the student table?
redbox:
Yes!! I am getting the values in the drop down box. thanks a lot.
Now while creating i want to insert that (school_id) in to student table as well.
Could you please tell me how i can get the id as hidden or something so i can insert that into the student table?
Your code should be the following:
<div class="row">
<?php echo $form->labelEx($model, 'school_id'); ?>
<?php echo $form->dropDownList($model, 'school_id', CHtml::listData(School::model()->findAll(), 'id', 'name')); ?>
<?php echo $form->error($model, 'school_id'); ?>
</div>
correct?
If school_id appears as one of the validation-rules it is marked as safe for massive assignment. Basically this means that it should work.
Be sure to look at the tutorials and most definitely follow the Blog-tutorial. It’s a big help and explains a lot.
Yii is actually quite well documented, you just have to know where to look for what kind of information.
redbox
(Deepali Mardikar)
August 8, 2011, 9:12am
7
Thanks for your valueable reply ChessSpider,
yes thats exact i have done and its working.
Could you please suggest me some good Blog-tutorials on Yii?
ChessSpider:
Your code should be the following:
<div class="row">
<?php echo $form->labelEx($model, 'school_id'); ?>
<?php echo $form->dropDownList($model, 'school_id', CHtml::listData(School::model()->findAll(), 'id', 'name')); ?>
<?php echo $form->error($model, 'school_id'); ?>
</div>
correct?