Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9004225
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T00:51:20+00:00 2026-06-16T00:51:20+00:00

First ZF2 application, getting there, but I think still missing a think or two

  • 0

First ZF2 application, getting there, but I think still missing a think or two when it comes to dependency injection and the ServiceManager.

I have a particular problem at the moment with a new database gateway class I’m writing. I won’t to inject a database adapter, so I’ve implemented AdapterAwareInterface. But the setDbAdapter method is never called in my class. I’m wondering if someone would be so kind as to look at my code and suggest what might be going wrong (or what I’m missing!).

So, here is the class in which I implement AdapterAwareInterface.

<?php
namespace Foo\Database;
use Zend\Db\Adapter\Adapter;
use Zend\Db\Adapter\AdapterAwareInterface;
use Zend\Log\LoggerAwareInterface;
use Zend\Log\LoggerInterface;


class Gateway implements AdapterAwareInterface, LoggerAwareInterface
{
protected $logger = NULL;
protected $db = NULL;

public function setDbAdapter(Adapter $adapter)
{
    $this->db = $adapter;
} 

public function setLogger(LoggerInterface $logger)
{
    $this->logger = $logger;
}

This is an extract from my module file showing how I configure my service manager:

    public function getServiceConfig()
{
    return array(
        'factories' => array(
          ....
        ),
        'invokables' => array(
            'FooDatabaseGateway' => 'Foo\Database\Gateway',
        ),
        'abstract_factories' => array(
            'AbstractFeedParserFactory' => 'Bookmakers\Odds\Feeds\AbstractFeedParserFactory',
        ),
    );
}

This is how I’m testing:

gateway = $this->getServiceLocator()->get('FooDatabaseGateway');

And this is part of my global config:

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

Many thanks for any help you can provide.

:wq

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T00:51:22+00:00Added an answer on June 16, 2026 at 12:51 am

    OK a fresh pair of eyes on this problem this morning. I think this is the write answer.. At least that is to say its working for me. If anyone wants to suggest an entirely different of better approach, then please do so :-).

    So the bit is was missing was to use an initializer in my service manager config to call the setDbAdapter function on any class instances that implement AdapterAwareInterface. So in the array I return from getServiceConfig in my Module.php file, I have added the following entry:

    public function getServiceConfig() {
      return array(
        'initializers' => array(
          'db' => function($service, $sm)
          {
            if ($service instanceof AdapterAwareInterface)
            {
              $service->setDbAdapter($sm->get('Zend\Db\Adapter\Adapter'));
            }
          }....
    

    I think what I’m missing while learning ZF2 is that there are a lot of building blocks to work with, but you’ve got to put a lot of them together yourself.

    Things are looking good and I’m enjoying the Framework, but there is a lot to learn, and I’m still not convinced by using Server Manager injection rather than good old constructor injection!

    :wq

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I started to develop new application, it's my first application written in ZF2. I
First of all I have seen that there are many questions about unrecognized selector
I am building my first ZF2 application, and in one of my modules, the
First of all, I'm a complete newbie when it comes to VBA, but unfortunately
first take a look on this picture from localScope app : i have 2
first off I'm a noob to PHP but here is my problem. I am
First, some context: I'm a Python developer who has written a medium-sized application using
First you should know I have looked into many questions and none of them
first sorry if its a stupid question but im lost a bit. So when
First, I have a list of QWidget s that I won't know the length

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.