Customize Field In Yii Model

Dear all,

I’m a new comer in Yii development, and I have a question. please help me.

I have a table’s called student in mysql and there are 5 fields:

tbl_student:

  • id (int)

  • firstname (varchar)

  • lastname (varchar)

  • phone (vachar)

  • dob (datetime)

so in just want only 3 field in Yii Model such as bellow:

  • id (int)

  • name (varcahar)

  • phone (varchar)

Please let me know how can I do?

I’m really looking for your reply, Thanks





 class Student extends CActiveRecord

{

	public static function model($className=__CLASS__) {

		return parent::model($className);

	}


        public function tableName() {

               return 'tbl_student'; // or return '{{student}}';if you are useing tablePrefix

        }    


       ...

     //--------------above can be generated by gii/giix-------------------------------------


     // below is what you should do :

    

     

    

     /**

     * this method is a getter method for virtrual attribute "name"

     */

     public function getName(){

          return  $this->firstname.$this->lastName;

     }

}




 then  you have a  readable attribute name and you can access it as 


 $model = Student::model()->findByPk(1);  


 echo $model->name ;

if you want write your name attribute you should design a setter for name:





     public function setName($name){

         // splite the $name into firstName and lastName

          $this->firstName = $part1;

          $this->lastName = $part2 ;

    }



more info you can refer here :Understanding Virtual Attributes and get/set methods