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 = [$testValue, $pkValue]; } else { // New record $sql = 'SELECT * FROM ' . $this->getModel()->getTable() . ' WHERE ' . $this->getName() . ' = ? LIMIT 1'; $values = [$testValue]; } $connection = Ajde_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 ['valid' => false, 'error' => trans('This ' . $this->getName() . ' already exists')]; } } return ['valid' => true]; }
public function __construct() { $this->_modelName = str_replace('Collection', '', get_class($this)) . 'Model'; $this->_connection = Ajde_Db::getInstance()->getConnection(); $tableNameCC = str_replace('Collection', '', get_class($this)); $tableName = $this->fromCamelCase($tableNameCC); $this->_table = Ajde_Db::getInstance()->getTable($tableName); $this->_query = new Ajde_Query(); }
public function __construct() { if (empty($this->_tableName)) { $tableNameCC = str_replace('Model', '', get_class($this)); $this->_tableName = $this->fromCamelCase($tableNameCC); } $this->_connection = Ajde_Db::getInstance()->getConnection(); $this->_table = Ajde_Db::getInstance()->getTable($this->_tableName); }
public function getParents() { $parents = Ajde_Db::getInstance()->getAdapter()->getParents((string) $this); $parentColumns = []; 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 getParents() { $parents = Ajde_Db::getInstance()->getAdapter()->getParents((string) $this); $parentTables = array(); foreach ($parents as $parent) { if (isset($parent['REFERENCED_TABLE_NAME'])) { $parentTables[] = $parent['REFERENCED_TABLE_NAME']; } } return $parentTables; }
public function getRowCount(Ajde_Collection $collection = null) { if (isset($collection)) { return $collection->count(true); } if (!isset($this->_rowCount)) { $sql = 'SELECT COUNT(*) AS count FROM ' . $this->_tableName; $connection = Ajde_Db::getInstance()->getConnection(); $statement = $connection->prepare($sql); $statement->execute(); $result = $statement->fetch(PDO::FETCH_ASSOC); $this->_rowCount = $result['count']; } return $this->_rowCount; }
private function _unused() { $used = new MediaCollection(); $unused = new MediaCollection(); $unused->addFilter(new Ajde_Filter_Where('id', Ajde_Filter::FILTER_EQUALS, '-9999')); $db = Ajde_Db::getInstance()->getConnection(); /** @var MediaModel $media */ foreach ($used as $media) { $stmt = $db->query('SELECT id FROM node WHERE media = ' . $media->getPK()); $stmt->execute(); $node = $stmt->rowCount(); $stmt = $db->query('SELECT id FROM node_media WHERE media = ' . $media->getPK()); $stmt->execute(); $nodeMedia = $stmt->rowCount(); $meta = 0; $stmt = $db->query('SELECT * FROM node_meta INNER JOIN meta ON meta.id = node_meta.meta AND node_meta.`value` <> \'\' AND meta.type = \'media\' AND node_meta.`value` = ' . $media->getPK()); $stmt->execute(); $meta += $stmt->rowCount(); $stmt = $db->query('SELECT * FROM setting_meta INNER JOIN meta ON meta.id = setting_meta.meta AND setting_meta.`value` <> \'\' AND meta.type = \'media\' AND setting_meta.`value` = ' . $media->getPK()); $stmt->execute(); $meta += $stmt->rowCount(); if ($node == 0 && $nodeMedia == 0 && $meta == 0) { $unused->add($media); } } return $unused; }