Creating a Model

A Model in PhpBURN must:

  • extends PhpBURN_Core
  • have public $_tablename attribute
  • have public $_package attribute
  • have a public function _mapping() {} method

That’s it, that’s all we need to have a PhpBURN Model working… well not so fast we also need to MAP in fact our model like this example:

class Users extends PhpBURN_Core {
	public $_package = 'webinsys';
	public $_tablename = 'users';

//  For auto-completion usage only
	public $id_user;
	public $email;
	public $first_name;
	public $last_name;
	public $password;
	public $gender;
	public $birthday;
	public $type;
	public $created_at;
	
	public function _mapping() {
		$this->getMap()->addField('id_user','id_user','int',10,array('autoincrement' => true, 'primary' => true));
		$this->getMap()->addField('email','email','varchar',255, array() );
		$this->getMap()->addField('first_name','first_name','varchar',255, array() );
		$this->getMap()->addField('last_name','last_name','varchar',255, array() );
		$this->getMap()->addField('password','password','varchar',255, array() );
		$this->getMap()->addField('gender','gender','enum', 'male,female', array() );
		$this->getMap()->addField('birthday','birthday','timestamp',null, array('null' => true) );
		$this->getMap()->addField('type','type','enum','admin,user', array('not_null' => true, 'default_value' => 'user') );
		$this->getMap()->addField('created_at','created_at','timestamp', null, array('default_value' => 'CURRENT_TIMESTAMP') );
	}
}

Pretty simple uh? Now let’s break this example and explain each part and what it does:

class Users extends PhpBURN_Core {

That tells PhpBURN that class is a Model.

	public $_package = 'webinsys';
	public $_tablename = 'users';

That says wich package and database table the model will use.

	public $id_user;
	public $email;
	public $first_name;
	public $last_name;
	public $password;
	public $gender;
	public $birthday;
	public $type;
	public $created_at;

That is pretty much to use with Netbeans/PDT for example because they auto-complete the attributes for you, it’s VERY useful but not mandatory.

public function _mapping() {
		...
}

The _mapping() function works like a magic method in PhpBURN because it is automatic loaded when you instance a new Model(); It is a VERY IMPORTANT function because it contains all mapping information to build a Model

$this->getMap()->addField('id_user','id_user','int',10,array('autoincrement' => true, 'primary' => true));

$this->getMap()->addField add’s a new field to our Mapping and the parms is:
alias, collumn, type, lenght, optionsArray

Well, that’s it now we can go to our controller and use it:

$user = new Users();
$user->where('last_name','=','Smith');
$user->find();

while($user->fetch()) {
	print_r( $user->toArray() );
}