/** * Get object by unique field * * @param string $fieldName * @param string $value * @param array $fields - optional * @throws Exception * @return array */ public function getItemByUniqueField($fieldName, $value, $fields = '*') { if (!$this->_objectConfig->isUnique($fieldName)) { $eText = 'getItemByUniqueField field "' . $fieldName . '" [' . $this->_objectConfig->getName() . '] should be unique'; $this->logError($eText); throw new Exception($eText); } $sql = $this->_dbSlave->select()->from($this->table(), $fields); $sql->where($this->_dbSlave->quoteIdentifier($fieldName) . ' = ?', $value); return $this->_dbSlave->fetchRow($sql); }
/** * Remove object * * @param string $name * @return boolean */ public function remove() { if ($this->_objectConfig->isLocked() || $this->_objectConfig->isReadOnly()) { $this->_errors[] = 'Can not remove locked object table ' . $this->_objectConfig->getName(); return false; } try { $model = Model::factory($this->_objectName); if (!$this->tableExists()) { return true; } $sql = 'DROP TABLE `' . $model->table() . '`'; $model->getDbConnection()->query($sql); $this->_logSql($sql); return true; } catch (Exception $e) { $this->_errors[] = $e->getMessage() . ' <br>SQL: ' . $sql; return false; } }
/** * Save changes * @param boolean $log - log changes * @param boolean $useTransaction — using a transaction when changing data is optional. * If data update in your code is carried out within an external transaction * set the value to false, * otherwise, the first update will lead to saving the changes * @return boolean; */ public function save($log = true, $useTransaction = true) { if ($this->_acl) { try { $this->_checkCanEdit(); } catch (Exception $e) { $this->_errors[] = $e->getMessage(); if (self::$_log) { self::$_log->log($e->getMessage()); } return false; } } $store = $this->_model->getStore(); if (self::$_log) { $store->setLog(self::$_log); } if ($this->_config->isReadOnly()) { $text = 'ORM :: cannot save readonly object ' . $this->_config->getName(); $this->_errors[] = $text; if (self::$_log) { self::$_log->log($text); } return false; } if ($this->_config->hasEncrypted()) { $ivField = $this->_config->getIvField(); $ivData = $this->get($ivField); if (empty($ivData)) { $this->set($ivField, base64_encode($this->_config->createIv())); } } $emptyFields = $this->_hasRequired(); if ($emptyFields !== true) { $text = 'ORM :: Fields can not be empty. ' . $this->getName() . ' [' . implode(',', $emptyFields) . ']'; $this->_errors[] = $text; if (self::$_log) { self::$_log->log($text); } return false; } $values = $this->validateUniqueValues(); if (!empty($values)) { foreach ($values as $k => $v) { $text = 'The Field value should be unique ' . $k . ':' . $v; $this->_errors[] = $text; } if (self::$_log) { self::$_log->log($this->getName() . ' ' . implode(', ', $this->_errors)); } return false; } try { if (!$this->getId()) { if ($this->_config->isRevControl()) { $this->date_created = date('Y-m-d H:i:s'); $this->date_updated = date('Y-m-d H:i:s'); $this->author_id = User::getInstance()->id; } $id = $store->insert($this, $log, $useTransaction); $this->setId($id); $this->commitChanges(); return (int) $id; } else { if ($this->_config->isRevControl()) { $this->date_updated = date('Y-m-d H:i:s'); $this->editor_id = User::getInstance()->getId(); } $id = (int) $store->update($this, $log, $useTransaction); $this->commitChanges(); return $id; } } catch (Exception $e) { $this->_errors[] = $e->getMessage(); if (self::$_log) { self::$_log->log($e->getMessage()); } return false; } }