I searched all the documentation over Yii but not got the answer of it.So I came here finally.
I have the following schema
Table Schools
+------------------+--------------+------+-----+---------------------+----------------+
| Field | Type | Null | Key | Default | Extra |
+------------------+--------------+------+-----+---------------------+----------------+
| id | int(10) | NO | PRI | NULL | auto_increment |
| school_name | varchar(100) | NO | | | |
+------------------+--------------+------+-----+---------------------+----------------+
Table Students
+------------------+--------------+------+-----+---------------------+----------------+
| Field | Type | Null | Key | Default | Extra |
+------------------+--------------+------+-----+---------------------+----------------+
| id | int(10) | NO | PRI | NULL | auto_increment |
| school_id | int(10) | NO | FK | | |
| student_name | varchar(100) | NO | | | |
| roll_no | varchar(80) | NO | | | |
| class | varchar(20) | NO | | | | |
| subjects | varchar(100) | NO | | | |
+------------------+--------------+------+-----+---------------------+----------------+
I have made models and CRUD for the both models.In models my relation is like this
In Students.php the relation is like
public function relations()
{
// NOTE: you may need to adjust the relation name and the related
// class name for the relations automatically generated below.
return array(
'School' => array(self::BELONGS_TO,'Schools','school_id'),
);
}
In Schools.php the relation is like
public function relations()
{
// NOTE: you may need to adjust the relation name and the related
// class name for the relations automatically generated below.
return array(
'student' => array(self::HAS_MANY, 'Students', 'school_id'),
);
}
Now I made the two models rendered in a single page so that I can enter all the respective fields in a single form.
In the _form.php file of Students I have made some change in student_name like this
<div class="row">
<?php echo $form->labelEx($model,'student_name'); ?>
<?php echo $form->dropdownList($model,'student_name', CHtml::listData(Students::model()->findAll(), 'id', 'student_name'), array('empty'=>array('Select'=>'--Select One---'))); ?>
<?php echo $form->error($model,'student_name'); ?>
Now for this piece of code I got all the student name from the Student model.
So my problem is when I am getting the student name from the dropdown list and going to select a student it will also fetch all the respective values of the student to be rendered in the _form.php without click on save button.So that user don't have to put it again manually.
I think ajax and json encode will work here but don't know how to make them work here.
[Update]
Here is StudentsController code
public function actionDisCoor() {
$model = School::model()->findByPk($_POST['Students']['student_id']);
$data=CHtml::listData($data,'id','name');
foreach($data as $value=>$name)
{
echo CHtml::tag('option',array('value'=>$value),CHtml::encode($name),true);
}
}
Here is _form.php code for Students
<div class="row">
<?php echo $form->labelEx($model,'student_name'); ?>
<?php $List = CHtml::listData(Students::model()->findAll(), 'id', 'student_name');
?>
<?php echo $form->dropdownList($model,'student_name',$List,
array('onChange'=>CHtml::ajax(array(
'url' => CController::createUrl('DisCoor'),
'type' => 'POST',
'update'=>'#school_id',
)),'style'=>'width:180px;'
)
)?>
<?php echo $form->error($model,'student_name'); ?>
</div>
After all that when I saw in firebug I got the error.Here is the screen shot