Exemplo n.º 1
0
 /**
  * Update Db object
  * @param Db_Object $object
  * @param boolean $log - optional, log changes
  * @param boolean $transaction - optional, use transaction if available
  * @return boolean
  */
 public function update(Db_Object $object, $log = true, $transaction = true)
 {
     if ($object->getConfig()->isReadOnly()) {
         if ($this->_log) {
             $this->_log->log('ORM :: cannot update readonly object ' . $object->getConfig()->getName());
         }
         return false;
     }
     /*
      * Check object id
      */
     if (!$object->getId()) {
         return false;
     }
     /*
      * Check for updates
      */
     if (!$object->hasUpdates()) {
         return $object->getId();
     }
     /*
      * Fire "BEFORE_UPDATE" Event if event manager exists
      */
     if ($this->_eventManager) {
         $this->_eventManager->fireEvent(Db_Object_Event_Manager::BEFORE_UPDATE, $object);
     }
     /*
     * Validate unique values
     *
     $values = $object->validateUniqueValues();
     
     if(!empty($values))
     {
       if($this->_log)
       {
         $errors = array();
         foreach($values as $k => $v)
         {
           $errors[] = $k . ':' . $v;
         }
         $this->_log->log($object->getName() . '::update ' . implode(', ' , $errors));
       }
       return false;
     }
     */
     /*
      * Check if DB table support transactions
      */
     $transact = $object->getConfig()->isTransact();
     /*
      * Get Database connector for object model;
      */
     $db = $this->_getDbConnection($object);
     if ($transact && $transaction) {
         $db->beginTransaction();
     }
     $success = $this->_updateOperation($object);
     if (!$success) {
         if ($transact && $transaction) {
             $db->rollBack();
         }
         return false;
     } else {
         if ($transact && $transaction) {
             $db->commit();
         }
     }
     /*
      * Save history if required
      * @todo удалить жесткую связанность
      */
     if ($log && $object->getConfig()->get('save_history')) {
         Model::factory($this->_historyObject)->log(User::getInstance()->id, $object->getId(), Model_Historylog::Update, $object->getTable());
     }
     /*
      * Fire "AFTER_UPDATE" Event if event manager exists
      */
     if ($this->_eventManager) {
         $this->_eventManager->fireEvent(Db_Object_Event_Manager::AFTER_UPDATE, $object);
     }
     return $object->getId();
 }