
Zend - New Record (DbAdapter)
By:
yamcsha on
May 7th, 2012 | syntax:
PHP | size: 1.57 KB | hits: 24 | expires: Never
<?php
$db = Zend_Db_Table::getDefaultAdapter();
$entityTable = new Model_{name}();
$form = new Backend_Form_{name}();
if ($this->_request->isPost()) {
if ($form->isValid($this->_request->getPost())) {
Zend_Db_Table::getDefaultAdapter()->beginTransaction();
$entity = $entityTable->fetchNew();
$entity->field1 = $form->getValue('field_name');
.....
$entity->save(); // insert
//
$entity->save(); // update
Zend_Db_Table::getDefaultAdapter()->commit();
} else
$form->populate($this->_request->getPost());
} else {
$form->populate ($entity->toArrayy());
}
/**
* Entity ClassA
*/
class Model_EntityA extends Application_Db_Table_Abstract {
protected $_name = 'table_name';
protected $_primary = 'id';
protected $_rowClass = 'Application_Db_Table_Row';
protected $_dependentTables = array(
'Model_EntityA',
'Model_EntityM'
);
}
/*
* Class to handle ManyToMany
*/
class Model_EntityM extends Application_Db_Table_Abstract {
protected $_name = 'table_name';
protected $_primary = 'id';
protected $_rowClass = 'Application_Db_Table_Row';
protected $_referenceMap = array(
'boutique' => array(
'columns' => 'a_id',
'refTableClass' => 'Model_EntityA',
'refColumns' => 'id'
),
'produit' => array(
'columns' => 'b_id',
'refTableClass' => 'Model_EntityB',
'refColumns' => 'id'
)
);
}