Overwrite insert and update of Zend_Db_Table
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
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
- Edited:
-
satrun77
- 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)
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