Exemplo n.º 1
0
 /**
  * action index
  * @return \Zend\View\Model\ViewModel
  */
 public function indexAction()
 {
     /**
      * função anônima para var_dump estilizado
      */
     $myVarDump = function ($nome_linha = "Nome da Linha", $data = null, $caracter = ' - ') {
         echo str_repeat($caracter, 100) . '<br/>' . ucwords($nome_linha) . '<pre><br/>';
         var_dump($data);
         echo '</pre>' . str_repeat($caracter, 100) . '<br/><br/>';
     };
     /**
      * Uso de cache
      */
     if (!$this->cache()->hasItem('nome')) {
         $myVarDump("Registro de Cache Agora", $this->cache()->setItem('nome', 'igor'));
     } else {
         $myVarDump("Cache Existente", $this->cache()->getItem('nome'));
     }
     /**
      * conexão com banco
      */
     /* $adapter = new Adaptador(array( // alias use Zend\Db\Adapter\Adapter as Adaptador
            'driver'    => 'Pdo_Mysql',
            'database'  => 'agenda_contatos',
            'username'  => 'tutorialzf2',
            'password'  => '!nF&rn0d3d4nt&'
        ));*/
     $adapter = $this->getServiceLocator()->get('AdapterDb');
     /**
      * obter nome do schema do nosso banco
      */
     $myVarDump("Nome Schema", $adapter->getCurrentSchema());
     /**
      * contar quantidade de elementos da nossa tabela
      */
     $myVarDump("Quantidade elementos tabela contatos", $adapter->query("SELECT * FROM `contatos`")->execute()->count());
     /**
      * montar objeto sql e executar
      */
     $sql = new Sql($adapter);
     // use Zend\Db\Sql\Sql
     $select = $sql->select()->from('contatos');
     $statement = $sql->prepareStatementForSqlObject($select);
     $resultsSql = $statement->execute();
     $myVarDump("Objet Sql com Select executado", $resultsSql);
     /**
      * motar objeto resultset com objeto sql e mostrar resultado em array
      */
     $resultSet = new \Zend\Db\ResultSet\ResultSet();
     // nao necessita do use devido a sintaxe iniciando em \
     $resultSet->initialize($resultsSql);
     $myVarDump("Resultado do Objeto Sql para Array ", $resultSet->toArray());
     die;
 }
Exemplo n.º 2
0
 /**
  * Load all contents
  * @param array $criteria Filtering criteria
  */
 public function loadAll(array $criteria = null)
 {
     if (is_null($criteria)) {
         $criteria = array();
     }
     $select = $this->customSelect($this->content->getTable()->getSql()->select(), $criteria);
     $tableMetadata = $this->content->getTableMetadata();
     if (array_key_exists('sorting', $criteria) && is_array($criteria['sorting'])) {
         foreach ($criteria['sorting'] as $field => $sort) {
             if (in_array($field, $tableMetadata['columns'])) {
                 $select->order($field . ' ' . strtoupper(addslashes($sort)));
             }
         }
     }
     if (array_key_exists('filter', $criteria) && is_array($criteria['filter'])) {
         foreach ($criteria['filter'] as $field => $word) {
             if (is_string($word) && in_array($field, $tableMetadata['columns'])) {
                 $select->where($field . ' LIKE"' . addslashes($word) . '%"');
             }
         }
     }
     if (array_key_exists('count', $criteria) && $criteria['count'] > 0) {
         $select->limit($criteria['count']);
         if (array_key_exists('page', $criteria) && $criteria['page'] > 0) {
             $select->offset(($criteria['page'] - 1) * $criteria['count']);
         }
     }
     $propertyName = null;
     if (property_exists($select, 'storeToCache')) {
         $propertyName = $select->storeToCache;
     }
     if (!is_null($propertyName) && $this->content->getCache()->getItem($select->storeToCache) != '') {
         $this->items = $this->content->getCache()->getItem($select->storeToCache);
     } else {
         try {
             $statement = $this->content->getTable()->getSql()->prepareStatementForSqlObject($select);
             $results = $statement->execute();
             $resultSet = new \Zend\Db\ResultSet\ResultSet();
             $resultSet->initialize($results);
             $data = $resultSet->toArray();
         } catch (\Exception $e) {
             $mysqli = $this->content->getTable()->getAdapter()->getDriver()->getConnection()->getResource();
             if (array_key_exists('firephp', $GLOBALS) && !headers_sent()) {
                 $GLOBALS['firephp']->error('Error in ' . get_called_class() . ' on query ' . $select->getSqlString($this->content->getTable()->getAdapter()->getPlatform()) . ' ' . $e->getMessage() . ' ' . $mysqli->errno . ' ' . $mysqli->error);
             }
             throw new \Exception('Error in ' . get_called_class() . ' on query ' . $select->getSqlString($this->content->getTable()->getAdapter()->getPlatform()) . ' ' . $e->getMessage() . ' ' . $mysqli->errno . ' ' . $mysqli->error, 1401301242);
         }
         if (!headers_sent()) {
             //echo $select->getSqlString($this->content->getTable()->getAdapter()->getPlatform());
         }
         $this->items = array();
         foreach ($data as $dataitem) {
             $item = clone $this->content;
             //$GLOBALS['firephp']->log($dataitem);
             $item->setData($dataitem);
             array_push($this->items, $item);
         }
     }
     if (property_exists($select, 'storeToCache') && $this->content->getCache()->getItem($select->storeToCache) == '') {
         $this->content->getCache()->setItem($select->storeToCache, $this->items);
     }
 }