Overriding Model Getters in Doctrine

Overriding Model Getters in Doctrine

When I was working on my current project using Symfony 1.4 with Doctine 1.2, I needed to override Doctrine’s get method to return a date format of “Y-m-d” rather than default ‘Y-m-d H:i:s’ format for timestamp value. It was simply the matter of doing:
public function getStart()
{
	return date('Y-m-d', strtotime(parent::getStart()));
}
in Propel, however, Doctrine will complain:
Maximum function nesting level of ’100′ reached, aborting!
Somehow the call to its parent gets resolved somewhere back to the subclass, very weird. After some googling, I found out that it is because Doctrine doesn’t really have a method called “getStart()”, it was done through a magic __call method that looks for getters and setters and forwards them to a geneic get or set method in Doctrine_Record class. So the call to parent::getStart() will essentially calls to itself, and causes the error mentioned above. The solution is to do the following:
public function getStart()
{
	return date('Y-m-d', strtotime(parent::_get('start')));
}
I am still new to Doctrine as I have been using Propel for years, but I really want to taste Doctrine as there are so many positives about them on the net. I think once I am used to Doctrine, it will certainly be more powerful than Propel.

Loading

One comment

Leave a Reply

Your email address will not be published. Required fields are marked *

My new Snowflake Blog is now live. I will not be updating this blog anymore but will continue with new contents in the Snowflake world!