directory
directory

Reputation: 3169

Get default zend db adapter in table model

How can I get the default db adabter in my table model? I want to use it to create a transaction.

In database.global.php:

    return array(
   'service_manager' => array(
        'factories' => array(
            'Zend\Db\Adapter\Adapter' => 'Zend\Db\Adapter\AdapterServiceFactory',
        ),
        'aliases' => array(
            'db' => 'Zend\Db\Adapter\Adapter',
        ),
    ),
    'db' => array(
        'driver'         => 'Pdo',
        'dsn'            => 'mysql:dbname=cww;host=localhost',
        'driver_options' => array(
            PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES \'UTF8\''
        ),
    ),
);

Now I would like to have $this->adapter in my albumTable.php

I tried to receive it as follow:

use Zend\ServiceManager\ServiceLocatorAwareInterface;
use Zend\ServiceManager\ServiceLocatorInterface;
use Zend\Db\TableGateway\TableGateway;
use Zend\Db\Sql\Expression;
use Zend\Db\Sql\Select;
use Zend\Db\Sql\Update;
use Zend\Db\Sql\Sql;
use Zend\Db\Sql\Where;
use Ajax\Model\Album;

class AlbumTable implements ServiceLocatorAwareInterface
{
    protected $tableGateway;
    protected $adapter;

    public function __construct(TableGateway $tableGateway)
    {
        $this->tableGateway = $tableGateway;
        $this->adapter  = $this->getServiceLocator()->get('db');    
    }

But I get the error:

Fatal error: Class Ajax\Model\AlbumTable contains 2 abstract methods and must therefore be declared abstract or implement the remaining methods (Zend\ServiceManager\ServiceLocatorAwareInterface::setServiceLocator, Zend\ServiceManager\ServiceLocatorAwareInterface::getServiceLocator) in

Upvotes: 0

Views: 3644

Answers (2)

Pawan Kumar
Pawan Kumar

Reputation: 2260

use this to create a database adapter:

$adapter = $this->getServiceLocator()->get('db');

you can use 'db' only as you have created alias. You can also try writing things in locals.php in \autoload\config\local.php. Now, suppose we want to execute a database query in mySQL: Create a sql statetment and put in variable $sql. Now do this:

$statement = $adapter->createStatement($sql);
$result = $statement->execute();

Hope this helps.

Upvotes: 0

Aydin Hassan
Aydin Hassan

Reputation: 1475

Add the following functions:

public function getServiceLocator() {
    return $this->serviceLocator;
}


public function setServiceLocator(Zend\ServiceManager\ServiceLocatorInterface $serviceLocator) {
    $this->serviceLocator= $serviceLocator;
    return $this;
}

Then you can do:

$this->$dbAdapter = $this->getServiceLocator()->get('Zend\Db\Adapter\Adapter');

However, if you read the getting started guide it explains how to construct TableGateways using a service manager factory, passing in the DbAdapter and other parameters like so:

'RoleTableGateway' => function ($sm) {
    $dbAdapter = $sm->get('Zend\Db\Adapter\Adapter');
    $resultSetPrototype = new ResultSet();
    $resultSetPrototype->setArrayObjectPrototype(new Role());
    return new TableGateway('role', $dbAdapter, null, $resultSetPrototype);
},

Upvotes: 1

Related Questions