Jun 23 2009

Automagic Prefixes for Model Fields

Say we have a player model, and every field in playerstable is prepended with player_. For example, player_username, player_email, etc.

I’m personally not used to this database design, but I know plenty of people use it. When I work on projects that have this, I’m not particularly found of having to write:

$p = new Player;
echo $p->player_username;

I’d rather ditch the prepended part in all my PHP code.

echo $p->username;

Use Accessors

We can do this by writing some __get and __set functions:

public function __get($name) {    
	$prepend = 'player_'.$name;    
	if(isset($this->$prepend)) {        
	return $this->$prepend;    
	}    
	return parent::__get($name);
}

public function __set($name, $value) {    
	$prepend = 'player_'.$name;    
	if(isset($this->$prepend)) {        
		$this->$prepend = $value;    
	} else {        
		parent::__set($name, $value);        
		//if no parent, you might want the default:        
		//$this->$name = $value    
	}
}

Basically, stated before, these get called when you try to access a property that doesn’t exist on the object. So when we try to access username, we check if player_username exists, and if so, return that value.

MY_Model: Easily extendable

You could work this into a MY_Model class that extends Model, and then make all your models extend MY_Model. If you wanted to do this, I’d say make a property of MY_Model called ‘prefix’, and use prefix in the accesors. Then, in each sub-class, all you need to do is define the prefix.

class MY_Model extends Model {    
	protected $prefix;    
	public function __get($name) {        
		$prepend = $this->prefix.$name;        
		//...    
	}   
}

class Player_Model extends MY_Model {    
	protected $prefix = 'player_'; 
}
  • #php
  • #kohana