Modifying Data Before Saving

Hi all,

I have a form with 4 fields to insert a subject to a class.

For convenience i want the grade to be selected by the user, then for all the classes that exist in that selected grade i want to insert the same set of data to the database.

I tried this logic in my beforeSave() function :-




 public function beforeSave()

        {

            $selected_grade=  $this->taughtinClass;

            $selected_master=$this->masterIncharge;

            $selected_stream=$this->stream;

            $selected_subjectName=$this->subjectName;

            

         $sql='SELECT classId 

         FROM `schoolclasses` 

         WHERE grade='.$selected_grade.'';//getting all the classId's on selected grade

                        

                  $connection=Yii::app()->db; 

                  $command=$connection->createCommand($sql);//creating the command

                  $classIds=$command->queryAll();//array of classId's

                        

                  foreach ($classIds as $class) //accessing each classId

                  {

                            $this->subjectId=$class;

                  }

            

        }



but this doesnt seem to work, can someone tell why this happens? i mean the theory. Where can i actually insert this logic, action create too it doesnt work…:(

Thanks a lot for your time!

Cheerz!