Issues

ZF-4759: Default row values are not saved to the database

Issue Type: Bug Created: 2008-10-31T03:28:22.000+0000 Last Updated: 2012-08-01T06:10:11.000+0000 Status: Open Fix version(s): Reporter: Stanislav Anisimov (satanistlav) Assignee: Ralph Schindler (ralph) Tags: - Zend_Db_Table

Related issues: - ZF-9061

Attachments:

Description

When saving new table row to the database default values are filtered and not saved to the database. IMHO, we should not check for modified fields when inserting a new row Replacing two lines

<pre class="highlight">
$data = array_intersect_key($this->_data, $this->_modifiedFields);
$primaryKey = $this->_getTable()->insert($data);

with

<pre class="highlight">
$primaryKey = $this->_getTable()->insert($this->_data);

should fix the problem

Comments

Posted by Michelangelo van Dam (dragonbe) on 2008-11-08T06:00:48.000+0000

fix bug for #bughuntday

Posted by Stanislav Anisimov (satanistlav) on 2008-12-02T04:49:10.000+0000

As I see the issue is not resolved yet. To avoid the problem I followed this way. I put this lines in pre-insert logic

<pre class="highlight">
protected function _insert()
{
    foreach ($this->_table->getDefaultValues() as $k=>$v) {
        $this->_modifiedFields[$k] = true;
    }
}

Posted by sebastien crocquesel (cortex) on 2009-02-15T12:43:27.000+0000

I think that the issue comes when defaultSource is set to self::DEFAULT_CLASS. Then, the default value must be passed to the insert statement and thus, must be touched to become a modifiedFields.

In the Zend_Db_Table_Abstract::createRow, i would merge the $data from argument with the defaults value before the call to SetFromArray

Change:

} elseif ($defaultSource == self::DEFAULT_CLASS && $this->_defaultValues) {

        foreach ($this->_defaultValues as $defaultName => $defaultValue) {
            if (array_key_exists($defaultName, $defaults)) {
                $defaults[$defaultName] = $defaultValue;
            }
        }

}

with something like

} elseif ($defaultSource == self::DEFAULT_CLASS && $this->_defaultValues) {

        foreach ($this->_defaultValues as $defaultName => $defaultValue) {
            if (array_key_exists($defaultName, $defaults)) {
                $defaults[$defaultName] = $defaultValue;
            }
        }
       // add the defaults value to the data, values specified in $data override defaults one 
       $data = array_merge($defaults, $data);
    }

Posted by sebastien crocquesel (cortex) on 2009-02-19T05:22:14.000+0000

Be aware that with this fix, you can no longer mix default value from the database and from the class. But I think it's not really recommended and was not the purpose of this functionnality.

Posted by Sergei Izvorean (uniqueculture) on 2009-04-21T12:10:53.000+0000

The problem occurs due to default values aren't flagged in $_modifiedFields array. They are just assigned to $_data. Other fields that you change go through __set method and 'true' is set for each changed field in $_modifiedFields.

I would create a method to set initial data:

protected function _setData(array $data) { $this->_data = $data; $this->_modifiedFields = array_combine(array_keys($data), array_fill(0, count($data), true)); }

thanks.

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