Don't like ads? PRO users don't see any ads ;-)

Zend - New Record (DbAdapter)

By: yamcsha on May 7th, 2012  |  syntax: PHP  |  size: 1.57 KB  |  hits: 24  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. <?php
  2. $db          = Zend_Db_Table::getDefaultAdapter();
  3. $entityTable = new Model_{name}();
  4. $form        = new Backend_Form_{name}();
  5.  
  6. if ($this->_request->isPost()) {
  7.     if ($form->isValid($this->_request->getPost())) {
  8.         Zend_Db_Table::getDefaultAdapter()->beginTransaction();
  9.  
  10.         $entity = $entityTable->fetchNew();
  11.         $entity->field1 = $form->getValue('field_name');
  12.         .....
  13.         $entity->save(); // insert
  14.  
  15.         //
  16.         $entity->save(); // update
  17.  
  18.         Zend_Db_Table::getDefaultAdapter()->commit();
  19.     } else
  20.         $form->populate($this->_request->getPost());
  21. } else {
  22.     $form->populate ($entity->toArrayy());
  23. }
  24.  
  25. /**
  26.  * Entity ClassA
  27.  */
  28. class Model_EntityA extends Application_Db_Table_Abstract {
  29.     protected $_name     = 'table_name';
  30.     protected $_primary  = 'id';
  31.     protected $_rowClass = 'Application_Db_Table_Row';
  32.     protected $_dependentTables = array(
  33.         'Model_EntityA',
  34.         'Model_EntityM'
  35.     );
  36. }
  37.  
  38. /*
  39.  * Class to handle ManyToMany
  40.  */
  41. class Model_EntityM extends Application_Db_Table_Abstract {
  42.     protected $_name     = 'table_name';
  43.     protected $_primary  = 'id';
  44.     protected $_rowClass = 'Application_Db_Table_Row';
  45.     protected $_referenceMap = array(
  46.         'boutique' => array(
  47.         'columns'       => 'a_id',
  48.         'refTableClass' => 'Model_EntityA',
  49.         'refColumns'    => 'id'
  50.         ),
  51.         'produit' => array(
  52.             'columns'       => 'b_id',
  53.             'refTableClass' => 'Model_EntityB',
  54.             'refColumns'    => 'id'
  55.         )
  56.     );
  57. }