Zend Framework Source Code Snippets

Overwrite insert and update of Zend_Db_Table

Bookmark and Share
1
2
3
4
56
7
8
9
1011
12
13
14
1516
17
18
19
2021
22
23
24
25
/**
         * @copyright  2008, S. Mohammed Alsharaf
         * @license    http://www.gnu.org/licenses/gpl-2.0.html GNU Public License
         * @author     S. Mohammed Alsharaf (satrun77@hotmail.com)
         * @link       http://jamandcheese-on-phptoast.com/         * @version        1.1
         * @credit thanks for IgorN
         */
    abstract class My_Model_Table extends Zend_Db_Table_Abstract
    {        public function insert ( array $data )
        {
       return parent::insert( $this->dataFilter( $data ) );
        }
          public function update ( array $data, $where )
        { 
          return parent::update( $this->dataFilter( $data ), $where );
        }
          protected function dataFilter( $data )
          {
           return array_intersect_key( $data, array_combine($this->_getCols(), $this->_getCols()));
           }
    }

Comments

IgorN IgorN
2 years ago

public function insert ( array $data )
{
return parent::insert( $this->dataFilter( $data ) );
}

public function update ( array $data, $where )
{
return parent::update( $this->dataFilter( $data ), $where );
}

protected function dataFilter( $data )
{
return array_intersect_key( $data, array_combine($this->_getCols(), $this->_getCols()));
}

It seems so simple

satrun77 satrun77
2 years ago

you are right.
thanks

You must login before commenting on a snippet. If you do not have an account, please register.

Snippet description

This abstract class extends Zend_Db_Table_Abstract to overwrite the methods insert and update.

Before inserting or updating any data. It retrieves the table metadata information. Assigning the columns names of the table to a property in the My_Model_Table class. Then the method cleanData loops the array that has been passed to the method insert or update to filter any data that is invalid, not equal to a column in the table.

Snippet details

Created:
satrun77 satrun77
2 years ago
Edited:
satrun77 satrun77
1 year ago
Revision Id:
164
Edit Message:
Changed author url
ZF Version
1.9.0
Tags:
zend_db Zend_Db_Table insert update
Comments:
2
Views:
1638
Points:
0 (0 votes)

History

r164

Changed author url

satrun77 satrun77
1 year ago
diff
r97

upgrade Release

satrun77 satrun77
2 years ago
diff
r96

Initial Release

satrun77 satrun77
2 years ago
diff
r27

Initial Release

satrun77 satrun77
2 years ago