Issues

ZF-2360: Additions to Row class

Issue Type: Improvement Created: 2007-12-28T13:46:45.000+0000 Last Updated: 2013-03-13T20:55:26.000+0000 Status: Open Fix version(s): - Next Mini Release ()

Reporter: Brandon Parise (bparise) Assignee: Ralph Schindler (ralph) Tags: - Zend_Db_Table

Related issues: Attachments:

Description

h3. Test for whether or not a Row object isNew I have run into a few situations where I needed to know whether or now the Row object passed already existed in the database. I think the following "isNew" method would be helpful in determining if a Row object is new or a reference to an existing row in the database:

<pre class="highlight">
public function setTimestamp(Zend_Db_Table_Row_Abstract $row)
{
    if ($row->isNew()) {
        $row->created = date('Y-m-d H:i:s');
    }
    $row->updated = date('Y-m-d H:i:s');    
}

\

h3. Getting a Row Object's Modified Data This also comes out of a use-case scenario where I required an email to be sent when the status of a row table.status was modified. This check was OUTSIDE of the row object so I didn't have direct access to whether or not $Row->status has changed:

<pre class="highlight">
$modifiedData = $Row->getModifiedData();
if (in_array('status', $modifiedData)) {
    // Send email with the new status
}

\

h3. Code for the examples above

<pre class="highlight">
abstract class Zend_Db_Table_Row_Abstract
{
    /**
     * Returns true if the row is new (no clean/db data)
     *
     * @return bool
     */
    public function isNew()
    {
        return empty($this->_cleanData);
    }

    /**
     * Returns the diff of the data from the cleanData.
     *
     * @return array
     */
    public function getModifiedData()
    {
        return array_diff_assoc($this->_data, $this->_cleanData);
    }
}

Comments

Posted by Wil Sinclair (wil) on 2008-03-25T20:43:56.000+0000

Please categorize/fix as needed.

Posted by Teemu Välimäki (cred) on 2008-04-01T14:21:12.000+0000

1st case could be dealt with current code

http://framework.zend.com/manual/en/…

2nd case seems to be asking for a trigger and if I'm not too deep in the fantasy land I think there's plugins coming for tapping into triggers or something.

In general I don't think it would be a good idea to implement asked features as proposed.

Posted by Wil Sinclair (wil) on 2008-04-18T13:12:00.000+0000

This doesn't appear to have been fixed in 1.5.0. Please update if this is not correct.

Posted by Coen Hyde (coenhyde) on 2008-07-15T21:54:24.000+0000

Yes this would be good. I have a common parent in all my row classes that implements the function isNewRow( ). I feel 'isNewRow' would be a better name than just i'isNew'.

Re: Teemu Yes but not all custom logic can be done when the row is saved. What happens if you want to apply custom logic when the row is created? The 'isNewRow' comes in handy eg.

<pre class="highlight">
public function init()
{
    if ($this->isNewRow()) {
        // do stuff
    } else{
        // Do stuff
    }
}

Have you found an issue?

See the Overview section for more details.

Copyright

© 2006-2016 by Zend, a Rogue Wave Company. Made with by awesome contributors.

This website is built using zend-expressive and it runs on PHP 7.

Contacts