Yii Framework Forum: How To Do Change With Property Of Model Which Called - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

How To Do Change With Property Of Model Which Called Rate Topic: -----

#1 User is offline   Termit132 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 14
  • Joined: 14-September 12

Posted 24 January 2013 - 12:22 PM

Hello everyone !
Now i have to do translit function with a property (set of properties) of model.
It's obvious that easy and clear way is insert translit logic in a model 'cause it's not comfortable to write in each place where doing something with this property (set of properties) (in actions, views).
It's may look something like:
//model
...
public $name;
...
getName()
{
  return HelpClass::translit($this->name);
}


But mentioned in top code doesn't work.

Thank you in advance !
0

#2 User is offline   seenivasan 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 620
  • Joined: 17-June 12
  • Location:Chennai,TamilNadu,India.

Posted 25 January 2013 - 02:39 AM

Dear Friend

I am not able to see the exact problem you are facing.

I perceive that getter function is not functioning in Models.

If a model directly derived from a CComponent, then there is not much problem.

The only thing we have to ensure that property has lesser degree of access than the getter and setter function.

class MyModel extends CComponent
{
	protected $name="seeni";	//protected or private
	
	public function getName()
	{
		return strrev($this->name);
	
	}	
    
        public function setName($name)
	{
		$this->name=$name;
	
	}	
}


Now
$seeni=new MyModel;

echo $seeni->name;//returns inees;

$seeni->name="vasan";

echo $seeni->name;//returns nasav


The situation is not straight forward in CActiveRecord.
Here we have to override the __get and __set methods.

class User extends CActiveRecord
{   
	protected $native="India";
	
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	
	public function tableName()
	{
		return 'user';
	}


    public function __get($name) 
    {   
                $getter='get'.$name;
		if(method_exists($this,$getter))
			return $this->$getter();
                else return parent::__get($name);
    }
        
    public function __set($name,$value) 
    { 
                $setter='set'.$name;
		if(method_exists($this,$setter))
			return $this->$setter($value);
                else parent::__set($name,$value);
    }

    public function getNative()
   {
                 return strrev($this->native);
   }

   public function setNative($native)
   {
                 $this->native=$native;

   }
...............................................
...............................................


Now

$user=new User;
echo $user->native;// returns aidnI
$user->native="USA";
echo $user->native;//returns ASU


Regards.
1

#3 User is offline   Termit132 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 14
  • Joined: 14-September 12

Posted 25 January 2013 - 02:58 PM

Thank you, but i already found solution of my problem.
protected function afterFind()
{
    $this->name = Helper::funcName($this->name);
    return parent::afterFind();
}

0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users