Skip to content
This repository has been archived by the owner on Jun 11, 2021. It is now read-only.

Subclassing Shanty_Mongo_Document to Add Default Properties #68

Open
valeeum opened this issue Mar 17, 2012 · 7 comments
Open

Subclassing Shanty_Mongo_Document to Add Default Properties #68

valeeum opened this issue Mar 17, 2012 · 7 comments

Comments

@valeeum
Copy link

valeeum commented Mar 17, 2012

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?

@gwagner
Copy link

gwagner commented Mar 19, 2012

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

@valeeum
Copy link
Author

valeeum commented Mar 19, 2012

Can you give me an example of your preinsert method?

On Mar 19, 2012, at 7:39 AM, "gwagner" reply@reply.github.com wrote:

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


Reply to this email directly or view it on GitHub:
#68 (comment)

@gwagner
Copy link

gwagner commented Mar 19, 2012

<?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

@coen-hyde
Copy link
Owner

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?

@gwagner
Copy link

gwagner commented Mar 19, 2012

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().

@coen-hyde
Copy link
Owner

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.

@tholder
Copy link
Collaborator

tholder commented Mar 19, 2012

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants