This is where I may be going wrong … I started off following the Album tutorial, and this may be where my confusion is coming in …
I have three files for each table:
Model file ( defines the table )
Table file ( defines access to the table … ie. fetchAll )
Factory file, which does:
public function __invoke(ContainerInterface $container, $requestedName, ?array $options = null): VersionsView
{
$dbAdapter = $container->get(AdapterInterface::class);
$resultSetPrototype = new ResultSet();
$resultSetPrototype->setArrayObjectPrototype(new Versions());
$tableGateway = new TableGateway('v_versions_summary', $dbAdapter, null, $resultSetPrototype);
return new VersionsView($tableGateway);
}
So there is my TableGateway …
From your example above, I get the feel that what I should be doing is moving:
$dbAdapter = $container->get(AdapterInterface::class);
$resultSetPrototype = new ResultSet();
$resultSetPrototype->setArrayObjectPrototype(new Versions());
$tableGateway = new TableGateway('v_versions_summary', $dbAdapter, null, $resultSetPrototype);
From the Factory to somewhere else, and change my invoker in the Factory to call that … which makes sense as to why I’m having a problem trying to get more then one …
In fact, short of the separate TableGateway files ( which would be cleaner, not suggesting not doing it ), but what I could do is:
public function __invoke(ContainerInterface $container, $requestedName, ?array $options = null): VersionsView
{
$dbAdapter = $container->get(AdapterInterface::class);
$resultSetPrototype = new ResultSet();
$resultSetPrototype->setArrayObjectPrototype(new Versions());
$tableGateway1 = new TableGateway('v_versions_summary', $dbAdapter, null, $resultSetPrototype);
$tableGateway2 = new TableGateway('v_demo_by_age', $dbAdapter, null, $resultSetPrototype);
return new VersionsView($tableGateway1, $tableGateway2);
}
And extend my Controller to accept two TableGateways in the constructor, instead of the 1 that I’m working with now …
Is this correct?
If I’m reading your code sample above right, your TableGateways are located in:
src/Db/TableGateway.php
Looking something like Table Gateways - laminas-db - Laminas Docs ( Quick Setup ), but you have multiple Classes ( for the example above ) within the same php file, so something like:
namespace Laminas\Db\TableGateway;
use Laminas\Db\Adapter\AdapterInterface;
use Laminas\Db\ResultSet\ResultSet;
use Laminas\Db\ResultSet\ResultSetInterface;
use Laminas\Db\Sql;
use Laminas\Db\Sql\TableIdentifier;
class MyTableGateway1 extends AbstractTableGateway
{
public function __construct( );
}
class MyTableGateway2 extends AbstractTableGateway
{
public function __construct( );
}
and then in my Factory(s), I just call up which TableGateway I want to access …
I’m curious, what does your folder structure look like? Again, if I’m reading your use statements above correctly, you have something like:
src
src/Controller
src/Controller/Factory
src/Db
src/Model
...?
Again, I’ve been basing mine on that Album tutorial, so I have my Factory files mixed into my Model files, which probably works for a simple, single Model module … I know each tends to have their own way of doing things, but rather start off with a structure that probably makes more sense on larger. modules then the tutorial, if you do not mind?
Thanks … unless I’ve truly confused my understanding of what you’ve shown above, this has been a major help …