Beispiel #1
0
 /**
  * _getTableFromString
  *
  * @param string $tableName
  * @return \Zend\DB\Table\AbstractTable
  */
 protected function _getTableFromString($tableName)
 {
     if ($this->_table instanceof Table\AbstractTable) {
         $tableDefinition = $this->_table->getDefinition();
         if ($tableDefinition !== null && $tableDefinition->hasTableConfig($tableName)) {
             return new \Zend\DB\Table\Table($tableName, $tableDefinition);
         }
     }
     //        // assume the tableName is the class name
     //        if (!class_exists($tableName)) {
     //            try {
     //                end\Loader::loadClass($tableName);
     //            } catch (end\Exception $e) {
     //                throw new Exception($e->getMessage(), $e->getCode(), $e);
     //            }
     //        }
     $options = array();
     if ($table = $this->_getTable()) {
         $options['db'] = $table->getAdapter();
     }
     if (isset($tableDefinition) && $tableDefinition !== null) {
         $options[Table\AbstractTable::DEFINITION] = $tableDefinition;
     }
     if (class_exists($tableName, true) == false) {
         throw new Exception('Class ' . $tableName . ' not found');
     }
     return new $tableName($options);
 }
Beispiel #2
0
 /**
  * Lazy load data via table fetchAll() method.
  *
  * @return void
  */
 protected function loadData()
 {
     if ($this->data === null) {
         $this->data = $this->_table->fetchAll($this->_where, $this->_order, $this->_count, $this->_offset);
         if ($this->data instanceof \Zend\Db\Table\AbstractRowset) {
             $this->data = $this->data->toArray();
         }
     }
 }
Beispiel #3
0
 /**
  * _getTableFromString
  *
  * @param string $tableName
  * @return \Zend\Db\Table\AbstractTable
  */
 protected function _getTableFromString($tableName)
 {
     if ($this->_table instanceof AbstractTable) {
         $tableDefinition = $this->_table->getDefinition();
         if ($tableDefinition !== null && $tableDefinition->hasTableConfig($tableName)) {
             return new \Zend\Db\Table\Table($tableName, $tableDefinition);
         }
     }
     // assume the tableName is the class name
     if (!class_exists($tableName)) {
         throw new RowException('Unable to find table class by name "' . $tableName . '"');
     }
     $options = array();
     if ($table = $this->_getTable()) {
         $options['db'] = $table->getAdapter();
     }
     if (isset($tableDefinition) && $tableDefinition !== null) {
         $options[AbstractTable::DEFINITION] = $tableDefinition;
     }
     return new $tableName($options);
 }
Beispiel #4
0
 /**
  * Calls other protected methods for individual setup tasks and requirement checks
  *
  * @return void
  */
 protected function _setup()
 {
     parent::_setup();
     $this->_setupPrimaryAssignment();
     $this->setLifetime($this->_lifetime);
     $this->_checkRequiredColumns();
 }
Beispiel #5
0
Datei: Db.php Projekt: rexmac/zf2
 /**
  * Set the default metadata cache
  * 
  * @param string|Zend_Cache_Core $cache
  * @return Zend_Application_Resource_Db
  */
 public function setDefaultMetadataCache($cache)
 {
     $metadataCache = null;
     if (is_string($cache)) {
         $bootstrap = $this->getBootstrap();
         if ($bootstrap instanceof \Zend\Application\ResourceBootstrapper && $bootstrap->hasPluginResource('CacheManager')) {
             $cacheManager = $bootstrap->bootstrap('CacheManager')->getResource('CacheManager');
             if (null !== $cacheManager && $cacheManager->hasCache($cache)) {
                 $metadataCache = $cacheManager->getCache($cache);
             }
         }
     } else {
         if ($cache instanceof \Zend\Cache\Frontend) {
             $metadataCache = $cache;
         }
     }
     if ($metadataCache instanceof \Zend\Cache\Frontend) {
         \Zend\Db\Table\AbstractTable::setDefaultMetadataCache($metadataCache);
     }
     return $this;
 }
Beispiel #6
0
 /**
 * Adds a FROM table and optional columns to the query.
 *
 * The table name can be expressed
 *
 * @param  array|string|Zend_Db_Expr|\Zend\Db\Table\AbstractTable $name The table name or an
                                                                  associative array relating
                                                                  table name to correlation
                                                                  name.
 * @param  array|string|\Zend\Db\Expr $cols The columns to select from this table.
 * @param  string $schema The schema name to specify, if any.
 * @return \Zend\Db\Table\Select This \Zend\Db\Table\Select object.
 */
 public function from($name, $cols = self::SQL_WILDCARD, $schema = null)
 {
     if ($name instanceof AbstractTable) {
         $info = $name->info();
         $name = $info[AbstractTable::NAME];
         if (isset($info[AbstractTable::SCHEMA])) {
             $schema = $info[AbstractTable::SCHEMA];
         }
     }
     return $this->joinInner($name, null, $cols, $schema);
 }
Beispiel #7
0
 public function testTableSetDefaultMetadataCacheException()
 {
     try {
         Table\AbstractTable::setDefaultMetadataCache(new \stdClass());
         $this->fail('Expected to catch Zend_Db_Table_Exception');
     } catch (\Zend\Exception $e) {
         $this->assertType('Zend\\Db\\Table\\Exception', $e, 'Expecting object of type Zend_Db_Table_Exception, got ' . get_class($e));
         $this->assertEquals("Argument must be of type Zend_Cache_Core, or a Registry key where a Zend_Cache_Core object is stored", $e->getMessage());
     }
     try {
         Table\AbstractTable::setDefaultMetadataCache(327);
         $this->fail('Expected to catch Zend_Db_Table_Exception');
     } catch (\Exception $e) {
         $this->assertType('Zend\\Db\\Table\\Exception', $e, 'Expecting object of type Zend_Db_Table_Exception, got ' . get_class($e));
         $this->assertEquals("Argument must be of type Zend_Cache_Core, or a Registry key where a Zend_Cache_Core object is stored", $e->getMessage());
     }
 }
Beispiel #8
0
 /**
  * _setDbAdapter() - set the database adapter to be used for quering
  *
  * @param Zend_Db_Adapter_Abstract 
  * @throws Zend_Auth_Adapter_Exception
  * @return Zend_Auth_Adapter_DbTable
  */
 protected function _setDbAdapter(AbstractDBAdapter $zendDb = null)
 {
     $this->_zendDb = $zendDb;
     /**
      * If no adapter is specified, fetch default database adapter.
      */
     if (null === $this->_zendDb) {
         $this->_zendDb = AbstractTable::getDefaultAdapter();
         if (null === $this->_zendDb) {
             throw new Exception\RuntimeException('Null was provided for the adapter but there is no default' . ' adatper registered with Zend\\Db\\Table to utilize.');
         }
     }
     return $this;
 }
Beispiel #9
0
 /**
  * @return ZF-8863
  */
 public function testExcludeConstructor()
 {
     AbstractTable::setDefaultAdapter($this->_adapterHasResult);
     $validator = new RecordExistsValidator('users', 'field1', 'id != 1');
     $this->assertTrue($validator->isValid('value3'));
 }
Beispiel #10
0
 /**
  * Test when adapter is provided
  *
  * @return void
  */
 public function testAdapterProvidedNoResult()
 {
     //clear the default adapter to ensure provided one is used
     AbstractTable::setDefaultAdapter(null);
     $validator = new NoRecordExistsValidator('users', 'field1', null, $this->_adapterNoResult);
     $this->assertTrue($validator->isValid('value1'));
 }
Beispiel #11
0
 /**
  * Test when adapter is provided
  *
  * @return void
  */
 public function testAdapterProvidedNoResult()
 {
     //clear the default adapter to ensure provided one is used
     AbstractTable::setDefaultAdapter(null);
     try {
         $validator = new NoRecordExistsValidator('users', 'field1', null, $this->_adapterNoResult);
         $this->assertTrue($validator->isValid('value1'));
     } catch (\Exception $e) {
         $this->markTestSkipped('No database available');
     }
 }
Beispiel #12
0
    /**
     * Run query and returns matches, or null if no matches are found.
     *
     * @param  String $value
     * @return Array when matches are found.
     */
    protected function _query($value)
    {
        /**
         * Check for an adapter being defined. if not, fetch the default adapter.
         */
        if ($this->_adapter === null) {
            $this->_adapter = AbstractTable::getDefaultAdapter();
            if (null === $this->_adapter) {
                throw new Exception\RuntimeException('No database adapter present');
            }
        }

        /**
         * Build select object
         */
        $select = new DBSelect($this->_adapter);
        $select->from($this->_table, array($this->_field), $this->_schema)
               ->where($this->_adapter->quoteIdentifier($this->_field, true).' = ?', $value);
        if ($this->_exclude !== null) {
            if (is_array($this->_exclude)) {
                $select->where($this->_adapter->quoteIdentifier($this->_exclude['field'], true).' != ?', $this->_exclude['value']);
            } else {
                $select->where($this->_exclude);
            }
        }
        $select->limit(1);

        /**
         * Run query
         */
        $result = $this->_adapter->fetchRow($select, array(), Db::FETCH_ASSOC);

        return $result;
    }
Beispiel #13
0
 /**
  * Add a Table dataset representation by specifiying an arbitrary select query.
  *
  * By default a select * will be done on the given tablename.
  *
  * @param \Zend\Db\Table\AbstractTable $table
  * @param string|\Zend\Db\Select $query
  * @param string $where
  * @param string $order
  * @param string $count
  * @param string $offset
  */
 public function addTable(\Zend\Db\Table\AbstractTable $table, $where = null, $order = null, $count = null, $offset = null)
 {
     $tableName = $table->info('name');
     $this->tables[$tableName] = new DbTable($table, $where, $order, $count, $offset);
 }
Beispiel #14
0
 /**
  * @group ZF-10033
  */
 public function testSetDefaultMetadataCacheFromCacheManager()
 {
     $this->markTestSkipped('DbResource has fatal error - skip this test now.');
     return;
     $configCache = array('database' => array('frontend' => array('name' => 'Core', 'options' => array('lifetime' => 120, 'automatic_serialization' => true)), 'backend' => array('name' => 'BlackHole')));
     $resource = new CacheManagerResource($configCache);
     $resource->setBootstrap($this->bootstrap);
     $resource->init();
     //$this->bootstrap->registerPluginResource('cachemanager', $configCache);
     $config = array('bootstrap' => $this->bootstrap, 'adapter' => 'Pdo\\Sqlite', 'params' => array('dbname' => ':memory:'), 'defaultMetadataCache' => 'database');
     $resource = new DbResource($config);
     $resource->init();
     $this->assertInstanceOf('Zend\\Cache\\Frontend', \Zend\Db\Table\AbstractTable::getDefaultMetadataCache());
 }
Beispiel #15
0
 /**
  * _setDbAdapter() - set the database adapter to be used for quering
  *
  * @param Zend_Db_Adapter_Abstract 
  * @throws Zend_Auth_Adapter_Exception
  * @return Zend_Auth_Adapter_DbTable
  */
 protected function _setDbAdapter(AbstractDBAdapter $zendDb = null)
 {
     $this->_zendDb = $zendDb;
     /**
      * If no adapter is specified, fetch default database adapter.
      */
     if (null === $this->_zendDb) {
         $this->_zendDb = AbstractTable::getDefaultAdapter();
         if (null === $this->_zendDb) {
             throw new Exception('No database adapter present');
         }
     }
     return $this;
 }
Beispiel #16
0
 /**
  * Test fallback to default database adapter
  *
  * @group ZF-7510
  */
 public function testAuthenticateWithDefaultDbAdapter()
 {
     // preserve default adapter between cases
     $tmp = \Zend\Db\Table\AbstractTable::getDefaultAdapter();
     // make sure that default db adapter exists
     \Zend\Db\Table\AbstractTable::setDefaultAdapter($this->_db);
     // check w/o passing adapter
     $this->_adapter = new Adapter\DbTable();
     $this->_adapter->setTableName('users')->setIdentityColumn('username')->setCredentialColumn('password')->setTableName('users')->setIdentity('my_username')->setCredential('my_password');
     $result = $this->_adapter->authenticate();
     $this->assertTrue($result->isValid());
     // restore adapter
     \Zend\Db\Table\AbstractTable::setDefaultAdapter($tmp);
 }
Beispiel #17
0
    protected function _createTable()
    {
        $sql = "CREATE TABLE subscription ("
             .      "id varchar(32) PRIMARY KEY NOT NULL DEFAULT '', "
             .      "topic_url varchar(255) DEFAULT NULL, "
             .      "hub_url varchar(255) DEFAULT NULL, "
             .      "created_time datetime DEFAULT NULL, "
             .      "lease_seconds bigint(20) DEFAULT NULL, "
             .      "verify_token varchar(255) DEFAULT NULL, "
             .      "secret varchar(255) DEFAULT NULL, "
             .      "expiration_time datetime DEFAULT NULL, "
             .      "subscription_state varchar(12) DEFAULT NULL"
             . ");";

       \Zend\Db\Table\AbstractTable::getDefaultAdapter()->getConnection()->query($sql);
    }
Beispiel #18
0
 /**
  *
  * @group ZF-10705
  */
 public function testCreatesQueryBasedOnNamedOrPositionalAvailablity()
 {
     AbstractTable::setDefaultAdapter(null);
     $this->_adapterHasResult->setSupportsParametersValues(array('named' => false, 'positional' => true));
     $validator = new RecordExistsValidator('users', 'field1', null, $this->_adapterHasResult);
     $validator->isValid('foo');
     $wherePart = $validator->getSelect()->getPart('where');
     $this->assertEquals('("field1" = ?)', $wherePart[0]);
     $this->_adapterHasResult->setSupportsParametersValues(array('named' => true, 'positional' => true));
     $validator = new RecordExistsValidator('users', 'field1', null, $this->_adapterHasResult);
     $validator->isValid('foo');
     $wherePart = $validator->getSelect()->getPart('where');
     $this->assertEquals('("field1" = :value)', $wherePart[0]);
 }
Beispiel #19
0
    public function testTableRowContructorWithTableNameSpecifiedInSubclass()
    {
        Table\AbstractTable::setDefaultAdapter($this->_db);

        $row = new \ZendTest\Db\Table\TestAsset\Row\TestStandaloneRow();
        $this->assertType('Zend\Db\Table\AbstractTable', $row->getTable());

        Table\AbstractTable::setDefaultAdapter();
    }
Beispiel #20
0
 /**
  * Returns the set adapter
  *
  * @return AbstractDBAdapter
  */
 public function getAdapter()
 {
     /**
      * Check for an adapter being defined. If not, fetch the default adapter.
      */
     if ($this->_adapter === null) {
         $this->_adapter = AbstractTable::getDefaultAdapter();
         if (null === $this->_adapter) {
             throw new Exception\RuntimeException('No database adapter present');
         }
     }
     return $this->_adapter;
 }
Beispiel #21
0
 /**
  * Sets up the database connection and creates the table for session data
  *
  * @param  array $primary
  * @return void
  */
 protected function _setupDb(array $primary)
 {
     if (!extension_loaded('pdo_sqlite')) {
         $this->markTestSkipped('The pdo_sqlite extension must be available and enabled for this test');
     }
     $this->_db = Db::factory('Pdo\\Sqlite', array('dbname' => ':memory:'));
     AbstractTable::setDefaultAdapter($this->_db);
     $query = array();
     $query[] = 'CREATE TABLE `Sessions` ( ';
     $query[] = '`id` varchar(32) NOT NULL, ';
     if (in_array('save_path', $primary)) {
         $query[] = '`save_path` varchar(32) NOT NULL, ';
     }
     if (in_array('name', $primary)) {
         $query[] = '`name` varchar(32) NOT NULL, ';
     }
     $query[] = '`modified` int(11) default NULL, ';
     $query[] = '`lifetime` int(11) default NULL, ';
     $query[] = '`data` text, ';
     $query[] = 'PRIMARY KEY  (' . implode(', ', $primary) . ') ';
     $query[] = ');';
     $this->_db->query(implode("\n", $query));
 }
Beispiel #22
0
 /**
  * Gets content panel for the Debugbar
  *
  * @return string
  */
 public function getPanel()
 {
     if (!$this->_db) {
         return '';
     }
     $html = '<h4>Database queries';
     // @TODO: This is always on?
     if (AbstractTable::getDefaultMetadataCache()) {
         $html .= ' – Metadata cache ENABLED';
     } else {
         $html .= ' – Metadata cache DISABLED';
     }
     $html .= '</h4>';
     return $html . $this->getProfile();
 }