zend framework and doctrine (getters and setters)
Is it possible to use doctrine on existing entity models like:
class user{
protected $_id;
protected $_name;
public function set_id($_id){}
public function get_id(){}
public function set_name($_name){}
public function get_name(){}
}
or to generate or use models with hard-coded getters开发者_运维百科 and setters.
I dont want to use
$user->name
$user['name']
$user->get('name')
can this be done with doctrine?
Thanx
For all Versions up to Doctrine 1.2: Your entity models class needs to extend the Doctrine_Record class.
The definition of the model fields needs to be done the "doctrine way" too. See: http://www.doctrine-project.org/documentation/manual/1_2/en/defining-models
example:
public function setTableDefinition() {
$this->hasColumn('username', 'string', 255); $this->hasColumn('password', 'string', 255);
}
If you want to use your own custom hard coded setters/getters you can override the magic getters/setters. See: http://www.doctrine-project.org/documentation/manual/1_2/en/introduction-to-models example:
class User extends BaseUser {
public function setPassword($password) { return $this->_set('password', md5($password)); }
}
One last remark/question: Using the magic setter/getter methods is good practise.. You should only use custom methods, if you need to manipulate the data in some way.
精彩评论