PHP and Zend framework

How can I easily implement queries within Zend framework?

-1


source to share


6 answers


Check out this document:



Zend Framework Database Quickstart (PDF)

+5


source


You can use the doctrine2 Doctrine project . There is a ZF3 DoctrineModule compatible module . You can use QueryBuilder which brings query creation to object manipulation.



+1


source


You can use the Zend Db adapter object like this:

$sql = 'SELECT * FROM bugs WHERE bug_id = ?';

$result = $db->fetchAll($sql, 2);

      

0


source


Use Zend_Db and just create a $ db object using the Zend_Db Factory method, and then create SQL statements using the Zend_Db_Select class and pass the $ select SQL statement to the fetch * methods (fetchRow, fetchAll ...).

0


source


1.Config:

config / autoload / dbAdapter.local.php

<?php
return array(
   'db' => array(
        'driver'   => 'Pdo',
        'dsn'      => 'mysql:dbname=name;host=localhost',
        'username' => 'root',
        'password' => 'root',    
   ),
   'service_manager' => array(
        'abstract_factories' => array(
            'Zend\Db\Adapter\AdapterAbstractServiceFactory',
        ),
    ),
);

      

  1. Implementation:
public function testAction()
{
  $username = 'user';


  $sql       = "SELECT email FROM users WHERE username = ?";
  $statement = $this->getDbAdapter()->createStatement($sql, array($username));
  $result   = $statement->execute()->current();
}



protected function getDbAdapter()
{
    if($this->dbAdapter == null) {
        $this->dbAdapter = $this->getServiceLocator()->get('Zend\Db\Adapter\Adapter');
    }

    return $this->dbAdapter;
}

      

0


source


The Zend framework has abstract_factories, it allows you to handle multiple database queries:

Zend \ Db \ adapter \ AdapterAbstractServiceFactory

  • Service Manager needs to be installed:

    'service_manager' => array ('abstract_factories' => array ('Zend \ Db \ adapter \ AdapterAbstractServiceFactory',),),

  • Configuring adapters in config / autoload / local.php

    db '=> array (' adapters' => array (

        'database1' => array(
           'driver'         => 'Pdo',
           'dsn'             => 'mysql:dbname=userDB;host=localhost',
           'driver_options'  => array(
                PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES \'UTF8\''
            ),
        ),
    
        etc...
    ),
    
          

    ),

  • Configuring adapters in config / autoload / global.php

    return array(
    'db' => array(
        'adapters' => array(
            'database1' => array(
            'username' => 'root',
            'password' => '',
            ),
        ),    
    ),
    
          

    );

  • Call adapters

    $ dbmanager-> Get ('Database1');

  • Use in the model

    use Zend\Db\TableGateway\AbstractTableGateway;
    use Zend\Db\Adapter\Adapter;
    
    class UserTable extends AbstractTableGateway
    {
        protected $table ='user';
    
    public function __invoke(Adapter $adapter)
    {
        $this->adapter = $adapter;
        $this->initialize();
    }
    
    public function fetchAll()
    {
        $resultSet = $this->select();
        return $resultSet->toArray();
    }
    
          

    }

0


source







All Articles