Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
Custom observer creates method for model
Hello there! Is it possible for a custom observer to generate/define a method for the model, it's applied to?
I mean I have an observer called observer_custom. If I add this to Model_Document, i could call a custom method defined in the observer:
$document = Model_Document::forge();
Is this possible, or should I implement and inherit from a custom model instead of an observer?
No, an observer is a triggered event.
There is a common misconception about Orm's, and how models (in an MVC context) should be used when using Orm's.
Most people assume that since an Orm introduces standard methods on a model, these are the methods you should use. And Controllers get littered by direct Orm method calls, creating a large dependency between the controller and the database. So you're losing the abstraction the model should provide.
Instead, all storage backend I/O (in this case database) should be done in the model, according to the MVC principles, and your controllers should just contain generic calls to the model, like read or write operations, but on a functional level.
For example something like:
What the definition of "a draft from today" is, and how you should get it, it abstracted in the model method. The controller should not be bothered with selects and where clauses, it should only be able to influence selection related data, like for example passing a key, or output related things, like limit, offset or order:
Model_Document::get_todays_drafts(array('order' => array('field' => 'ASC'), 'limit' => 25));
Model methods that operate on the model definition as a whole should be defined static (like in this example), methods that operate on a specific model instance should not be static.
$docs = Model_Document::get_todays_drafts();
$first = reset($docs);
Add a Comment
It looks like you're new here. If you want to get involved, click one of these buttons!
Apply for Membership
↳ Job Board
↳ Installation & Setup
↳ Tips and Tutorials
↳ Code share
In this Discussion