0

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

2 Answers 2

1

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);
},
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for your answer. I've set the tableGetaway so I used this one by now to get wat I need '$this->tableGateway->getAdapter()'; that was all I needed :) thanks anyway
I got this error "Fatal error: Call to a member function get() on a non-object in F:\xampp\htdocs\zendtest\module\Application\src\Application\Model\Signup.php on line 41" .please see my question "stackoverflow.com/questions/20319388/calling-adapter-in-model", is your solution suited to my case ?
You need to set the service locator or your database adapter in to your model class manually. The comments on the questions should provide guidance.
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.