pia-sophie
pia-sophie

Reputation: 515

tablegateway in Zend3 vs Model in Zend1

In the last years I developed a few projects in ZF1.x. Now I started a new one and used ZF3 the first time. I found some very good improvements, but I'm a bit lost with the database models.

What is the real benefit of the tablegateway design pattern? In Zend1 I found it quite easy to join several tables in my model class, now I habe the gateway, an exchange array and in my Module.php I have something like this:

public function getServiceConfig()
{
    return [
            'factories' => [
                    Model\ImportTable::class => function($container) {
                        $tableGateway = $container->get(Model\ImportTableGateway::class);
                        return new Model\ImportTable($tableGateway);
                    },
                    Model\ImportTableGateway::class => function ($container) {
                        $dbAdapter = $container->get(AdapterInterface::class);
                        $resultSetPrototype = new ResultSet();
                        $resultSetPrototype->setArrayObjectPrototype(new Model\Import());
                        return new TableGateway('tablename', $dbAdapter, null, $resultSetPrototype);
                    },
                    ],
                    ];
}

I also have, believing the tutorial, to fill in all my tablecolumns manually, hm seems quite static. That's the first issue.

The second one: I have trouble to decide where to join several tables. Here is a small and easy example, my tables are much more complex:

author 1 --> n books

The topic is books, but I need also the author table to show the name of course, nobody wants to see just the foreign key. What is state of the art? Where should I place my sql with the join statement, how to implement in the pattern, what about the Module.php?

So I have more or less 3 files which some information to the used table. What would be the best way and place to define joins? Of course I could code the join in my controller, but that is kind of no go in a MVC-application, isn't it?

What about skipping my Interfaceclass? Where to set the filters then? Do I really have to fill in the tablename (ony one) in the factories?

EDIT new ideas:

I fighted with the syntax, but now it works with this:

public function Project_Unit(Unit $unit = null){

    $select = $this->tableGateway->getSql()->select()
        ->join('t_unit', 't_project.ProjectID = t_unit.ProjectID',array('UnitID','CI_Number', 'Unitname','Shortcut','Suppliername'));       //, left
    return $this->tableGateway->selectWith($select);

}

And immediately I have new questions. I have the same fieldnames in both tables, like description etc. How can I add aliasnames in this statement.

And in my view, how can I add something like groups, this is what I just have

 <?php 

foreach ($imports as $import) : 
var_dump(get_object_vars($import));?>
    <tr>
    <td><?= $this->escapeHtml($import->Projectname) ?></td>
     <td><?= $this->escapeHtml($import->Shortcut) ?></td>
     <td><?= $this->escapeHtml($import->CI_Number) ?></td>
     <td><?= $this->escapeHtml($import->Description) ?></td>
     <td><?= $this->escapeHtml($import->Component_Class) ?></td>

It is a snippet, and the vardump was just to check which fields I already have. So how can I group the view? I need something like

each project each unit of the projectgroup

Is there some elegant possibility or do I need just another loop and do the fitting via css?

Upvotes: 0

Views: 135

Answers (0)

Related Questions