Create now model variable

Hello, I just want to add a new class variable for my model Users, called “fullName”, which combines the attributes first_name and last_name. I did this before but I can’t remember how…any help is appreciated, thanks.




CHtml::listData(Users::model()->findAllByAttributes(array('pid'=>$pid)),'id','fullName')) //fullName is the first name and last name together for the Users model



Add a function called ‘getFullName’ to your model. IIRC. :)

Something like:




<?php

public function getFullName()

{

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

}




how to combine FullName with search method ?




$criteria->compare($this->FullName,$this->firstname); // <img src='http://www.yiiframework.com/forum/public/style_emoticons/default/huh.gif' class='bbc_emoticon' alt='???' />?<img src='http://www.yiiframework.com/forum/public/style_emoticons/default/huh.gif' class='bbc_emoticon' alt='???' />?<img src='http://www.yiiframework.com/forum/public/style_emoticons/default/huh.gif' class='bbc_emoticon' alt='???' />???



thanks

Hi,

You can add variables to your model by just adding a member variable to your model class like so:




class MyModel extends CActiveRecord

{

       public $myNewVariable;


	/**

	 * Returns the static model of the specified AR class.

	 * @return TestType the static model class

	 */

	public static function model($className=__CLASS__)

	{

		return parent::model($className);

	}


	/**

	 * @return string the associated database table name

	 */

	public function tableName()

	{

		return 'myModel';

	}

.

.

.



After you do that you can override the afterFind() and beforeValidate() functions in CActiveRecord to set the variable and then split the variable up to push it into your database.




        protected function afterfind() 

        {

            parent::afterfind(); 

            $this->myNewVariable = $this->first. ' ' . $this->last;

            return;

        } 

        

        protected function beforeValidate() 

        {

            parent::beforeValidate(); 

            if (isset($_POST['MyModel']))

            {

                $this->first = trim(explode($_POST['MyModel']['myNewVariable'], ' ')[0]);

                $this->last = trim(explode($_POST['MyModel']['myNewVariable'], ' ')[1]);

            }

            return true;

        } 



(not sure if I used explode correctly there but you get the idea)

Then in your view you could reference it just like any other variable in the model like so:




$model->myNewVariable;



Thats how I would do what you asked for but if you only want to retrieve it I would do what MrSoundLess said and just create a get function like so:




public function getFullName()

{

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

}



Theres less overhead and less work that way.

Enjoy,

-Nazum

thanks,

but i get error at this line:




 $this->fistname = trim(explode($_POST['TCustomer']['fullname'], ' ')[0]);

 $this->lastname = trim(explode($_POST['TCustomer']['fullname'], ' ')[1]);



Parse error: syntax error, unexpected ‘[’ in D:\AppServ\www\yii\testdrive\protected\models\TCustomer.php on line 61

the problem is to find the data from the combined(firstname & lastname) in one search field.

My bad, try $_POST[‘TCustomer_fullname’] instead. The multidimensional array worked in my case but this should work for yours.

-Nazum