Constructors in Yii Model

I want to pass the parameters while creating the new model object. So that created model object will have those parameter values.

Eg:




$student_obj = new Student($student_name);


class Student extends CActiveRecord() {

    public function __construct($student_name){

        parent::__construct();

        if($student_name == "")

            throw Exception;

        $this->student_name = $student_name;

    }

}

Tried with constructors. Doesn’t work. While loading the model objects gives error as we need to pass those constructor parameters.(findByPk(1)). How can i do this.

Hi Y!!,

Why you don’t do like this?




$student_obj = new Student;

$student_obj->name = $student_name;



Or something like this constructor method inside Student.php model,




...

   public static function createStudent($student_name) {

      $student_obj = new Student;

      if($student_name == "")

            throw Exception;

      $student_obj->student_name = $student_name;


      return $student_obj;

   }

...



You can call it using




Student::createStudent("Daniel");