protected function _validate() { if (!empty($this->_value)) { // return array('valid' => false, 'error' => __('This ' . $this->getValue() . ' already exists')); $pkField = $this->getModel()->getTable()->getPK(); $pkValue = $this->getModel()->getPK(); if ($this->getModel()->isFieldEncrypted($this->getName())) { $testValue = $this->getModel()->doEncrypt($this->getValue()); } else { $testValue = $this->getValue(); } if ($pkValue) { // Existing $sql = 'SELECT * FROM ' . $this->getModel()->getTable() . ' WHERE ' . $this->getName() . ' = ? AND ' . $pkField . ' != ? LIMIT 1'; $values = array($testValue, $pkValue); } else { // New record $sql = 'SELECT * FROM ' . $this->getModel()->getTable() . ' WHERE ' . $this->getName() . ' = ? LIMIT 1'; $values = array($testValue); } $connection = Db::getInstance()->getConnection(); $statement = $connection->prepare($sql); $statement->execute($values); $result = $statement->fetch(PDO::FETCH_ASSOC); if ($result === false || empty($result)) { // Not found } else { return array('valid' => false, 'error' => __('This ' . $this->getName() . ' already exists')); } } return array('valid' => true); }
public function getParents() { $parents = Db::getInstance()->getAdapter()->getParents((string) $this); $parentColumns = array(); foreach ($parents as $parent) { if (isset($parent['COLUMN_NAME']) && isset($parent['REFERENCED_TABLE_NAME']) && strtoupper($parent['CONSTRAINT_NAME']) != 'PRIMARY') { $parentColumns[] = $parent['COLUMN_NAME']; } } return array_unique($parentColumns); }
public function getRowCount(Collection $collection = null) { if (isset($collection)) { return $collection->count(true); } if (!isset($this->_rowCount)) { $sql = 'SELECT COUNT(*) AS count FROM ' . $this->_tableName; $connection = Db::getInstance()->getConnection(); $statement = $connection->prepare($sql); $statement->execute(); $result = $statement->fetch(PDO::FETCH_ASSOC); $this->_rowCount = $result['count']; } return $this->_rowCount; }
public function updateDatabase() { $db = Db::getInstance(); return $db->update(); }
public function __construct() { $this->_modelName = str_replace('Collection', '', get_class($this)) . 'Model'; $this->_connection = Db::getInstance()->getConnection(); $tableNameCC = str_replace('Collection', '', get_class($this)); $tableName = $this->fromCamelCase($tableNameCC); $this->_table = Db::getInstance()->getTable($tableName); $this->_query = new Query(); }