3

I'm trying to use zend expressive and looking at how to do database stuff now. I was looking at this, but it's not clear. I used composer to install zend-db and it mentioned to add a line in dependencies.global.php and then use container in the factory class to get the adapter but then didn't talk about how to access it in the actual action class so I don't know what's going on as the adapter object is out of scope from other class.

Anyone has good and clear example from start to finish to actually able to connect and query sql?

iehrlich
  • 3,572
  • 4
  • 34
  • 43
sparkmix
  • 2,157
  • 3
  • 25
  • 33
  • did you get any example to integrate Zend/Db with expressive at the end? It might be helpful because I'm struggling with that same topic too. – Leo May 04 '17 at 23:39

1 Answers1

1

try to inject your db class via factory, follow example on skeleton app, you can do something like this:

HomePageFactory.php

    public function __invoke(ContainerInterface $container)
{
    $router   = $container->get(RouterInterface::class);
    $template = $container->has(TemplateRendererInterface::class) ? $container->get(TemplateRendererInterface::class) : null;

    $adapter    = $container->get( Adapter::class );
    $usersTable = $container->get( Table\UsersTable::class );


    return new HomePageAction($router, $template,$adapter,$usersTable);
}

HomePageAction.php

class HomePageAction implements ServerMiddlewareInterface
{
/**
 * @var Router\RouterInterface
 */
private $router;

/**
 * @var null|Template\TemplateRendererInterface
 */
private $template;

/**
 * @var Adapter
 */
private $dbAdapter;

/**
 * @var UsersTable
 */
private $usersTable;

/**
 * HomePageAction constructor.
 * @param Router\RouterInterface $router
 * @param Template\TemplateRendererInterface|null $template
 * @param Adapter $adapter
 * @param UsersTable $usersTable
 */
public function __construct( Router\RouterInterface $router, Template\TemplateRendererInterface $template = null, Adapter $adapter, Table\UsersTable $usersTable )
{
    $this->router     = $router;
    $this->template   = $template;
    $this->dbAdapter  = $adapter;
    $this->usersTable = $usersTable;

}

on your config provider where you have your tables u have to config dependencies as factory EX:

'factories'  => [

            Table\UsersTable::class => function($container) {
                $dbAdapter          = $container->get( AdapterInterface::class );
                $resultSetPrototype = new ResultSet();
                $resultSetPrototype->setArrayObjectPrototype( new Model\Users() );
                $tableGateway       = new TableGateway('users', $dbAdapter, null, $resultSetPrototype);
                return new Table\UsersTable($tableGateway);
            },

        ],
rafaelphp
  • 279
  • 2
  • 11
  • With this implementation, UserTable.php must be create on App/src/models/ in order to be provided, right? – Leo May 04 '17 at 23:36
  • 1
    where do you want to :), but yes. – rafaelphp May 05 '17 at 04:12
  • Could you please, please, please update your answer and give an example of that UsersTable class and User Model? and how to handle a simple query? I've lost how many attemps I've tried to make framework works – Leo May 05 '17 at 04:21
  • Yes, I can give you an expressive app with an example – rafaelphp May 05 '17 at 04:55
  • 1
    Here on github: https://github.com/rafaelphp/zf-expressive-db-tutorial-stackoverflow – rafaelphp May 05 '17 at 05:06