Esempio n. 1
0
 /**
  * Salva os dados na tabela
  *
  * @param array|ZendT_Db_Mapper $data
  * @param string $event @example update,insert,delete
  * @param array|ZendT_Db_Where $where
  */
 public function save($data, $event = 'insert', $where = null)
 {
     $this->_setAdapter($this->_adapter);
     $event = strtolower($event);
     $rowPrimary = null;
     if (!is_array($this->_unique) && count($this->_primary) > 1) {
         $this->_unique = $this->_primary;
     }
     if (is_array($this->_unique)) {
         $wherePrimary = new ZendT_Db_Where();
         $existUnique = false;
         foreach ($this->_unique as $column) {
             if ($column) {
                 if ($data instanceof ZendT_Db_Mapper) {
                     $valueUnique = $data->getData($column);
                 } else {
                     $valueUnique = $data[$column];
                 }
                 $wherePrimary->addFilter($column, $valueUnique, '=', $this->_mapper, true);
                 $existUnique = true;
             }
         }
         if ($existUnique) {
             $rowPrimary = $wherePrimary;
         }
     }
     /**
      * Valida se o registro que está sendo inserido ou alterado
      * contém um dos dados cadastrados no banco de dados
      * caso tenha não permite a alteração ou inclusão 
      */
     if ($rowPrimary instanceof ZendT_Db_Where && in_array($event, array('insert', 'update'))) {
         $wherePrimary = new ZendT_Db_Where();
         if ($event == 'update') {
             if (is_string($this->_primary) && $this->_primary != '') {
                 $wherePrimary->addFilter($this->_primary, $data[$this->_primary], '<>');
             } else {
                 if ($where == null) {
                     foreach ($this->_primary as $column) {
                         $valuePrimary = '';
                         if ($data instanceof ZendT_Db_Mapper) {
                             $valuePrimary = $data->getData($column);
                         } else {
                             $valuePrimary = $data[$column];
                         }
                         $wherePrimary->addFilter($column, $valuePrimary, '=', $this->_mapper, true);
                     }
                 } else {
                     $wherePrimary = $where;
                 }
             }
         }
         $binds = $wherePrimary->getBinds();
         $binds += $rowPrimary->getBinds();
         $sql = 'SELECT 1 FROM ' . $this->getTableName() . ' ' . $this->getAlias() . ' WHERE ' . $rowPrimary->getSqlWhere();
         if ($event == 'update') {
             $sql .= ' AND NOT ' . $wherePrimary->getSqlWhere();
         }
         if ($this->getAdapter()->fetchOne($sql, $binds)) {
             throw new ZendT_Exception_Business('Registro existente no cadastro!');
         }
     }
     /**
      * Caso existe sequencia, configura a coluna de Primary Key para receber a nova sequencia
      */
     if (isset($this->_sequence) && $this->_sequence != '' && $event == 'insert') {
         $id = '';
         if (is_string($this->_primary)) {
             $id = $this->getAdapter()->nextSequenceTable($this->_name, $this->_primary, $this->_sequence);
             //$this->getAdapter()->nextSequenceId(strtoupper($this->_sequence));
         } else {
             if (count($this->_primary) == 1) {
                 //$id = $this->getAdapter()->nextSequenceId(strtoupper($this->_sequence));
                 if (isset($this->_primary[0])) {
                     $columnName = $this->_primary[0];
                 } elseif (isset($this->_primary[1])) {
                     $columnName = $this->_primary[1];
                 }
                 $id = $this->getAdapter()->nextSequenceTable($this->_name, $columnName, $this->_sequence);
             } else {
                 //throw new ZendT_Exception_Business('Não é possível atribuir sequencia para uma chave primária multipla!');
             }
         }
         if ($id != '') {
             foreach ($this->_primary as $column) {
                 if ($data instanceof ZendT_Db_Mapper) {
                     $data->setData($id, $column);
                 } else {
                     $data[$column] = $id;
                 }
             }
         }
     }
     if ($event == 'insert') {
         return $this->insert($data);
     } elseif ($event == 'update') {
         return $this->update($data, $where);
     } elseif ($event == 'delete') {
         return $this->delete($where);
     }
 }