2

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>

Here is the code for accessRules()

  public function accessRules()
  {
    return array(
      array('allow',  // allow all users to perform 'index' and 'view' actions
        'actions'=>array('index','view'),
        'users'=>array('*'),
      ),
      array('allow', // allow authenticated user to perform 'create' and 'update' actions
        'actions'=>array('create','update'),
        'users'=>array('@'),
      ),
      array('allow', // allow authenticated user to perform 'create' and 'update' actions
        'actions'=>array('create','update','DisCoor'),
        'users'=>array('@'),
      ),
      array('allow', // allow admin user to perform 'admin' and 'delete' actions
        'actions'=>array('admin','delete'),
        'users'=>array('admin'),
      ),
      array('deny',  // deny all users
        'users'=>array('*'),
      ),
    );
  }

After all that when I saw in firebug I got the error.Here is the screen shot enter image description here

1 Answer 1

1

Its easy. Please read Creating a dependent dropdown . Hopefully it would answer all your queries.

you can also do something like in following example it is critical to see that i have called onChange and it makes form submit

     <?php echo
 $List = CHtml::listData(Students::model()->findAll(), 'id', 'student_name');
 $form->dropdownList($model,'student_name',$List,
                                array('onChange'=>
                                CHtml::ajax(array(
                                'url' => CController::createUrl('DisCoor'),
                                'type' => 'POST',                     
                               'update'=>'#school_id',
                                )),'style'=>'width:180px;'        
                                    )
                                )?>

and in my controller i did something like

$model = School::model()->findByPk($_POST['Jobs']['student_id']);
                 $data=CHtml::listData($data,'id','name');
foreach($data as $value=>$name)
{
    echo CHtml::tag('option',
               array('value'=>$value),CHtml::encode($name),true);
}
            

now #school_id is the dropDown that needs to be updated.

I have given you almost everything that you need Best of Luck

Sign up to request clarification or add additional context in comments.

9 Comments

thank you for your quick response.I have ssen that wiki.That one is for two drop-down dependent on each other.But I want the value of that certain record to be fetched which is made selected in dropdown list in _form.php page.
can you edit your answer with both my models name as required by there.sorry to say you I am little bit confused here.
another thing the code that you have mentioned in Controller should it be actionCreate or I have to again create a function for that.
you have to create another action that would respond to AJAX calls. I have done most with present knowledge. Now it is upto you to try and post your effort. We all are busy but we want to help so you have to do your part on your own. Ask questions if something is unclear
I used your code with the _form.php file and in controller I used actionDisCoor() to use the codes that to be used in controller.When I made select the option[student name]it showed an error which can be seen through console of firefox.The error was like "NetworkError: 500 PHP Error - http://localhost/SchoolApps/index.php?r=students/DisCoor".The error is for there is no such file has been created in that link I think.So how to solve this one?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.