One model two tables database

For programming and general questions on Zend Framework

One model two tables database

Postby pawe5563 on Sun Apr 16, 2017 5:03 pm

I have a problem with adding two queries from a database in one controller. I want to retrieve data with data in the database.

Module.php:

Code: Select all
<?php

namespace Mydata;

use Zend\ModuleManager\Feature\ConfigProviderInterface;
use Zend\Db\Adapter\AdapterInterface;
use Zend\Db\ResultSet\ResultSet;
use Zend\Db\TableGateway\TableGateway;

class Module implements ConfigProviderInterface
{
    public function getConfig()
    {
        return include __DIR__ . '/../config/module.config.php';
    }

    public function getServiceConfig()
    {
        return [
            'factories' => [
                Model\MydataTable::class => function ($container) {
                    $tableGateway = $container->get(Model\MydataTableGateway::class);
                    return new Model\MydataTable($tableGateway);
                },
                Model\MydataTableGateway::class => function ($container) {
                    $dbAdapter = $container->get(AdapterInterface::class);
                    $resultSetPrototype = new ResultSet();
                    $resultSetPrototype->setArrayObjectPrototype(new Model\Mydata());
                    return new TableGateway('ps_user', $dbAdapter, null, $resultSetPrototype);
                },     
                Model\UserBankTable::class => function ($container) {
                    $tableGateway = $container->get(Model\UserBankTableGateway::class);
                    return new Model\UserBankTable($tableGateway);
                },
                Model\UserBankGateway::class => function ($container) {
                    $dbAdapter = $container->get(AdapterInterface::class);
                    $resultSetPrototype = new ResultSet();
                    $resultSetPrototype->setArrayObjectPrototype(new Model\UserBank());
                    return new TableGateway('ps_user_bank_account', $dbAdapter, null, $resultSetPrototype);
                }, 
            ],
        ];
    }

    public function getControllerConfig()
    {
        return [
            'factories' => [
                Controller\MydataController::class => function ($container) {
                    return new Controller\MydataController(
                        $container->get(Model\MydataTable::class)
                    );
                },
            ],
        ];
    }   
}


MydataController.php:
Code: Select all
<?php

namespace Mydata\Controller;

use Mydata\Model\UserBankTable;
use Mydata\Model\MydataTable;
use Mydata\Form\AccountNumberForm;
use Mydata\Form\UserPasswordForm;
use Mydata\Form\UserForm;
use Zend\Mvc\MvcEvent;
use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\ViewModel;

class MydataController extends AbstractActionController
{
    private $table;

    public function __construct(MydataTable $table)
    {
        $this->table = $table;
    }

    public function onDispatch(MvcEvent $e)
    {
        $response = parent::onDispatch($e);       
        $this->layout()->setTemplate('layout/admin');               
        return $response;
    }

    public function indexAction()
    {
        $id = '1';
        $mydata = $this->table->getUser($id);



        $UserForm = new UserForm();
        $UserForm->bind($mydata);

        $AccountNumber = new AccountNumberForm();

        $UserPasswordForm = new UserPasswordForm();

        return new ViewModel(array(
            'userForm' => $UserForm,
            'accountForm' => $AccountNumber,
            'passwordForm' => $UserPasswordForm     
        ));
    }


How to add a query to the controller with UserBankTable?
pawe5563
 
Posts: 1
Joined: Sun Apr 16, 2017 5:01 pm

Return to Zend Framework

Who is online

Users browsing this forum: No registered users and 4 guests