protected function _onRowSave(System_Db_Table_Row_Abstract $row = null, $recordInfo) { if ($row === null) { throw new System_Exception('Row not found'); } $primary = $this->_table->info(Zend_Db_Table_Abstract::PRIMARY); foreach ((array) $primary as $id) { unset($recordInfo->{$id}); } $row->setFromArray((array) $recordInfo); }
/** * Returns id by name($value) from table * * @param string $value * @return string */ public function filter($value) { if ($value === null) { return null; } $select = $this->_table->select()->where($this->_field . ' = ?', $value); $row = $this->_table->fetchRow($select); if ($row !== null) { return $row[reset($this->_table->info(Zend_Db_Table::PRIMARY))]; } else { return null; } }
/** * Constructor. */ public function __construct($config = array()) { $this->_db = $config['db']; $this->_table = $config['table']; $this->_info = $this->_table->info(); if ($config['data'] === false) { // empty row, use blanks $cols = array_keys($this->_info['cols']); $data = array_fill(0, count($cols), null); $this->_data = array_combine($cols, $data); } else { $this->_data = (array) $config['data']; } }
/** * Set the table object, to re-establish a live connection * to the database for a Row that has been de-serialized. * * @param Zend_Db_Table_Abstract $table * @return boolean * @throws Zend_Db_Table_Row_Exception */ public function setTable(Zend_Db_Table_Abstract $table) { if ($table == null) { $this->_table = null; $this->_connected = false; return false; } $tableClass = get_class($table); if (!$table instanceof $this->_tableClass) { require_once 'Zend/Db/Table/Row/Exception.php'; throw new Zend_Db_Table_Row_Exception("The specified Table is of class {$tableClass}, expecting class to be instance of {$this->_tableClass}"); } $this->_table = $table; $this->_tableClass = $tableClass; $info = $this->_table->info(); if ($info['cols'] != array_keys($this->_data)) { require_once 'Zend/Db/Table/Row/Exception.php'; throw new Zend_Db_Table_Row_Exception('The specified Table does not have the same columns as the Row'); } if (!array_intersect((array) $this->_primary, $info['primary']) == (array) $this->_primary) { require_once 'Zend/Db/Table/Row/Exception.php'; throw new Zend_Db_Table_Row_Exception("The specified Table '{$tableClass}' does not have the same primary key as the Row"); } $this->_connected = true; return true; }
/** * Class constructor * * @param Zend_Db_Adapter $db Database adapter instance * @param Zend_Db_Table $table * @param array $columnMap * @return void */ public function __construct($db, Zend_Db_Table $table, $columnMap = null) { $info = $table->info(); parent::__construct($db, $info['schema'] . '.' . $info['name'], $columnMap); $this->_db = $db; $this->_table = $table; $this->_columnMap = $columnMap; }
/** * Save a new entry * @param array $data * @return int|string */ public function save(array $data) { $table = new Zend_Db_Table('users'); $fields = $table->info(Zend_Db_Table_Abstract::COLS); foreach ($data as $field => $value) { if (!in_array($field, $fields)) { unset($data[$field]); } } return $table->insert($data); }
public function updateAd(array $data, $id) { $table = new Zend_Db_Table('ads'); $fields = $table->info(Zend_Db_Table_Abstract::COLS); foreach ($data as $field => $value) { if (!in_array($field, $fields)) { unset($data[$field]); } } return $table->update($data, 'id = ' . (int) $id); }
/** * Show table info (DESCRIBE query) for given table * * @param array $args * @return void */ public function info(array $args = array()) { if (empty($args)) { Garp_Cli::errorOut('Insufficient arguments'); Garp_Cli::lineOut('Usage: garp Db info <tablename>'); return; } $db = new Zend_Db_Table($args[0]); Garp_Cli::lineOut(Zend_Config_Writer_Yaml::encode($db->info())); Garp_Cli::lineOut(''); }
/** * @group ZF-7042 * @group ZF-10778 */ public function testCacheIdGeneratedToMetadata() { /** * @see Zend_Cache */ require_once 'Zend/Cache.php'; /** * @see Zend_Cache_Backend_BlackHole */ require_once 'Zend/Cache/Backend/BlackHole.php'; Zend_Db_Table::setDefaultAdapter($this->_db); $dbConfig = $this->_db->getConfig(); $cacheId = md5((isset($dbConfig['port']) ? ':' . $dbConfig['port'] : null) . (isset($dbConfig['host']) ? ':' . $dbConfig['host'] : null) . '/' . $dbConfig['dbname'] . ':.cache_metadata'); $metadata = array('id' => array('PRIMARY' => true)); $cacheBackend = $this->getMock('Zend_Cache_Backend_BlackHole'); $cacheBackend->expects($this->any())->method('load')->with($this->equalTo($cacheId))->will($this->returnValue($metadata)); $cache = Zend_Cache::factory('Core', $cacheBackend, array('automatic_serialization' => false)); Zend_Db_Table_Abstract::setDefaultMetadataCache($cache); $this->_util->createTable('cache_metadata', array('id' => 'IDENTITY', 'name' => 'VARCHAR(32)')); $configTable = array('name' => 'cache_metadata', 'primary' => 'id'); $table = new Zend_Db_Table($configTable); $table->info(Zend_Db_Table::METADATA); $this->_util->dropTable('cache_metadata'); Zend_Db_Table_Abstract::setDefaultMetadataCache(null); }
$outputDir = isset($argv[2]) ? $argv[2] : 'application/models'; $testDir = 'tests/application/models'; // Start her up echo "\n" . 'Running, trying to source config file' . "\n"; // Find and load a config file, then get the DB params if (!file_exists('application/configs/application.ini')) { die('No config file found! Exiting...'); } $config = new Zend_Config_Ini('application/configs/application.ini', 'development'); $db = Zend_Db::factory($config->resources->db->adapter, $config->resources->db->params->toArray()); // Got out connection to the database, now we need to know which table we are modeling echo 'Database connection established, reading table \'' . $tableToModel . '\'' . "\n"; // Build the table object Zend_Db_Table::setDefaultAdapter($db); $table = new Zend_Db_Table($tableToModel); $data = $table->info('metadata'); $fields = array(); // Inform the user we have something to do echo 'Received info for ' . count($data) . ' fields. Creating the output...' . "\n"; // Create the Fields array foreach ($data as $field) { $newField = new Field($field); $fields[] = $newField; } // Make the Model try { $newModel = new Model($outputDir, $tableToModel, $fields); $newModel->generate(); // Debug //die( $newModel->getOutput() ); // Write it
/** * Returns table information. * * @param $key The specific info part to return OPTIONAL * @return array */ public function info($key = null) { $info = parent::info($key); if ($key === null) { $info = array_merge($info, array("fieldPrefix" => $this->_fieldPrefix)); } return $info; }
/** * Возвращает массив названий столбцов таблицы * * @param string $tablename Имя таблицы * * @return array */ public function getTableCols($tablename) { $table = new Zend_Db_Table(array('name' => $tablename, 'db' => $this->_db)); return $table->info(Zend_Db_Table::COLS); }
/** * Create a WHERE clause for use with Zend_Db_Select * * @param array $query WHERE options * @param string $separator AND/OR * @param bool $useJointView Wether to use the *_joint view or the table. * @return string WHERE clause */ protected function _createWhereClause(array $query, $separator = 'AND', $useJointView = true) { $where = array(); $adapter = $this->_model->getAdapter(); $nativeColumns = $this->_model->info(Zend_Db_Table_Abstract::COLS); if ($useJointView) { $tableName = $this->_getTableName($this->_model); $mockTable = new Zend_Db_Table(array(Zend_Db_Table_Abstract::NAME => $tableName, Zend_Db_Table_Abstract::PRIMARY => $this->_model->info(Zend_Db_Table_Abstract::PRIMARY))); $nativeColumns = $mockTable->info(Zend_Db_Table_Abstract::COLS); } else { $tableName = $this->_model->getName(); } // change native columns to lowercase // because when columnName is configured in camelcase in the model config // it causes problems when checking if refColumn is a native column $nativeColumns = array_map(function ($column) { return strtolower($column); }, $nativeColumns); foreach ($query as $column => $value) { if (strtoupper($column) === 'OR' && is_array($value)) { $where[] = $this->_createWhereClause($value, 'OR'); } elseif (is_array($value)) { $where[] = $adapter->quoteInto($adapter->quoteIdentifier($tableName) . '.' . $column . ' IN(?)', $value); } elseif (is_null($value)) { if (substr($column, -2) == '<>') { $column = preg_replace('/<>$/', '', $column); $where[] = $column . ' IS NOT NULL'; } else { $where[] = $column . ' IS NULL'; } } elseif (is_scalar($value)) { // Use $refColumn to see if this column is native to the current // model. $refColumn = null; if (!preg_match('/(>=?|<=?|like|<>)/i', $column, $matches)) { $refColumn = $column; $column = $adapter->quoteIdentifier($column) . ' ='; } else { // explode column so the actual column name can be quoted $parts = explode(' ', $column); $refColumn = $parts[0]; $column = $adapter->quoteIdentifier($parts[0]) . ' ' . $parts[1]; } if (strpos($refColumn, '.') === false && in_array($refColumn, $nativeColumns)) { $column = $adapter->quoteIdentifier($tableName) . '.' . $column; } $where[] = $adapter->quoteInto($column . ' ?', $value); } } return '(' . implode(" {$separator} ", $where) . ')'; }
protected function _authDb($identity, $credential) { $auth = Zend_Registry::get('Zend_Auth'); // Check if it's possible to authenticate if (!Zend_Registry::isRegistered('Zend_Db') || !($db = Zend_Registry::get('Zend_Db')) instanceof Zend_Db_Adapter_Abstract) { throw new Engine_Exception('Unable to authenticate, no database connection present'); } // Make user table and level table try { $userTable = new Zend_Db_Table(array('db' => $db, 'name' => 'engine4_users')); $userTable->info(); // Forces check on table existence $levelTable = new Zend_Db_Table(array('db' => $db, 'name' => 'engine4_authorization_levels')); $levelTable->info(); // Forces check on table existence $settingsTable = new Zend_Db_Table(array('db' => $db, 'name' => 'engine4_core_settings')); $settingsTable->info(); // Forces check on table existence } catch (Exception $e) { throw new Engine_Exception('Unable to authenticate, missing database tables'); } // Try to authenticate try { // Get static salt $staticSalt = $settingsTable->find('core.secret')->current(); if (is_object($staticSalt)) { $staticSalt = $staticSalt->value; } else { $staticSalt = ''; } // Get superadmin levels $saLevels = $levelTable->select()->where('flag = ?', 'superadmin')->query()->fetchAll(); $saLevelIds = array(); foreach ((array) $saLevels as $dat) { if (is_numeric($dat['level_id'])) { $saLevelIds[] = $dat['level_id']; } } if (empty($saLevelIds)) { return $form->addError('No admin levels'); } $saLevelStr = "'" . join("','", $saLevelIds) . "'"; // Authenticate $authAdapter = new Zend_Auth_Adapter_DbTable($db, 'engine4_users', 'email', 'password', "MD5(CONCAT('" . $staticSalt . "', ?, salt)) && `level_id` IN({$saLevelStr})"); $authAdapter->setIdentity($identity)->setCredential($credential); $authResult = $auth->authenticate($authAdapter); } catch (Exception $e) { throw new Engine_Exception('An error occurred'); } // Check result $authCode = $authResult->getCode(); if ($authCode != Zend_Auth_Result::SUCCESS) { return false; } return true; }
/** * Creates new node * * @return Axis_NSTree_Node */ public function createNode() { $info = $this->_dataTable->info(); $keys = array_values($info['cols']); $vals = array_fill(0, count($keys), null); $node = new Axis_NSTree_Node(array('db' => $this->_db, 'table' => $this->_dataTable, 'struct' => null, 'data' => array_combine($keys, $vals))); return $node; }
public function columnExist($tableName, $columnName) { $resource = Mage::getSingleton('core/resource'); $writeAdapter = $resource->getConnection('core_write'); Zend_Db_Table::setDefaultAdapter($writeAdapter); $table = new Zend_Db_Table($tableName); if (!in_array($columnName, $table->info('cols'))) { return false; } return true; }