0
votes

So, I have an Admin module which houses all the Controllers such as Users, Usergroups, Products etc. In the Module.php there is the getServiceConfig() method which sets all the appropriate database tables for the controllers. This is fine.

My problem is with the Models (User.php and UserTable.php, for ex.). I ideally want these to come from their own independent modules so the Admin module is not completely overwhelmed with files.

When I write Module.php like this:

<?php

namespace Admin;

use User\Model\User;
use User\Model\UserTable;
use Zend\Db\ResultSet\ResultSet;
use Zend\Db\TableGateway\TableGateway;
use Zend\ModuleManager\Feature\AutoloaderProviderInterface;
use Zend\Authentication\Adapter\DbTable as DbTableAuthAdapter;

class Module implements AutoloaderProviderInterface {

//Some other methods

public function getServiceConfig() {
    return array(
        'factories' => array(
            'User\Model\UserTable' => function($sm) {
        $tableGateway = $sm->get('UserTableGateway');
        $table = new UserTable($tableGateway);
        return $table;
    },
            'UserTableGateway' => function($sm) {
        $dbAdapter = $sm->get('Zend\Db\Adapter\Adapter');
        $resultSetPrototype = new ResultSet();
        $resultSetPrototype->setArrayObjectPrototype(new User());
        return new TableGateway('user', $dbAdapter, null, $resultSetPrototype);
    },

I get a blank page with dies not executing after it tries to initialize UserTable and User in getServiceConfig method. So, it is definitely a problem with those. It works fine if these classes are in the Admin module.

Ctrl-clicking the classes work so it is finding them. Is there any way to call these from the Admin's Module.php? Thanks.

1
Show me please text of throwed errorMicrobe
User is an active Module? Are you using classmap? Show also getAutoloaderConfig method from Admin and User Module class.Gabriel
In this case, User Module needs to load before Admin Module. Controllers and such should still belong to the UserModule tho, the AdminModule should rather be used to provide an administrative interface and AccessControl/RoleControl for the UserModule, but that's another topic :)Sam
@Sam this is correct and was the reason it was not loading. I've also re-arranged it now so the controllers are in their respective modules. Thanks.Ciaran

1 Answers

4
votes

try to use it simply like below

class Module
{

    public function getServiceConfig() {
        return array(
            'factories' => array(
                'User\Model\UserTable' => function($sm) {
                    $dbAdapter = $sm->get('Zend\Db\Adapter\Adapter');
                    $table = new \User\Model\UserTable($dbAdapter);
                    return $table;
                },
            ),
        );

    }
}