-
Notifications
You must be signed in to change notification settings - Fork 52
Subclassing Shanty_Mongo_Document to Add Default Properties #68
Comments
There are a multitude of ways to go about this. The best ways are going to be through preInsert() preUpdate() and preSave(). I have Shanty_Mongo_Document subclassed to Project_Model_Mongo which has a common function of _createHistory() that does exactly what your trying to do by using preInsert(), preUpdate(), and preSave(). All of my Models extend Project_Model_Mongo instead of Shanty_Mongo_Document |
Can you give me an example of your preinsert method? On Mar 19, 2012, at 7:39 AM, "gwagner" [email protected] wrote:
|
<?php
class Project_Model_Mongo extends Shanty_Mongo_Document
{
protected function preUpdate()
{
$this->_create_history();
parent::preUpdate();
}
protected function preSave()
{
$this->_create_history();
parent::preSave();
}
protected function preInsert()
{
$this->_create_history();
parent::preInsert();
}
private function _create_history()
{
/* if we dont have an active flag, set one */
if(!isset($this->active))
$this->active = 1;
/* this will only get set once on new documents */
if(!isset($this->created_date))
$this->created_date = time();
/* set the time last modified on */
$this->last_modified_on = (int)time();
$this->last_modified_by_ip = $_SERVER['REMOTE_ADDR'];
$this->last_modified_by_ua = $_SERVER['HTTP_USER_AGENT'];
if(!isset($this->version)) /* make sure a version is set */
$this->version = 1;
else /* otherwise increment */
$this->version++;
}
} All of the models i make extend Project_Model_Mongo instead of Shanty_Mongo_Document to make sure i am managing the active, last_modified_date, etc |
Yes you can use the as gwagner has show above. Though I'm surprised Shanty Mongo has gotten this far before someone asked for defaults. As well as regular defaults. eg. a string, int etc, it would be good to be able to pass a function that returns a value. eg. protected static $_requirements = array(
'name' => array('Default' => 'Bob'),
'created_at' => array('Default' => function() {
time();
})
); It should be reasonably easy to implement but i'm not sure php allows anonymous functions like that in class properties. Anyone know? |
I just ran a simple test using the function you provided above, and php pukes when it sees a function there. It looks like you can add anon functions to arrays (php 5.3+), just not in the way you have it there, but there is no reason why it shouldn't be doable through an init function with addRequirement(). |
I guessed that might be the case. It just seemed like something php wouldn't like. Yeah it would have to be done with an init function and addRequirement, in which case you may as well call time() then and there. |
I'm trying to extend Shanty_Mongo_Document to create a property called "date_created" by default with a value of time() every time a document is created and update time of the property "date_updated" every time a document is updated. How can I go about doing that?
The text was updated successfully, but these errors were encountered: