Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: [ZF][ZendFramework] jeden model a różne tabele w bazie danych
Forum PHP.pl > Forum > PHP > Frameworki
kociupk
Chciałbym wykonać w jednym controlerze kilka zapytań do różnych tabeli w bazie danych i na chwilę obecną nie wiem jak sobie z tym poradzić może wy macie jakiś pomysł na to rozwiązanie smile.gif z góry dzięki smile.gif

Model.php
  1. <?php
  2.  
  3. namespace Mydata;
  4.  
  5. use Zend\ModuleManager\Feature\ConfigProviderInterface;
  6. use Zend\Db\Adapter\AdapterInterface;
  7. use Zend\Db\ResultSet\ResultSet;
  8. use Zend\Db\TableGateway\TableGateway;
  9.  
  10. class Module implements ConfigProviderInterface
  11. {
  12. public function getConfig()
  13. {
  14. return include __DIR__ . '/../config/module.config.php';
  15. }
  16.  
  17. public function getServiceConfig()
  18. {
  19. return [
  20. 'factories' => [
  21. Model\MydataTable::class => function ($container) {
  22. $tableGateway = $container->get(Model\MydataTableGateway::class);
  23. return new Model\MydataTable($tableGateway);
  24. },
  25. Model\MydataTableGateway::class => function ($container) {
  26. $dbAdapter = $container->get(AdapterInterface::class);
  27. $resultSetPrototype = new ResultSet();
  28. $resultSetPrototype->setArrayObjectPrototype(new Model\Mydata());
  29. return new TableGateway('ps_user', $dbAdapter, null, $resultSetPrototype);
  30. },
  31. Model\UserBankTable::class => function ($container) {
  32. $tableGateway = $container->get(Model\UserBankTableGateway::class);
  33. return new Model\UserBankTable($tableGateway);
  34. },
  35. Model\UserBankGateway::class => function ($container) {
  36. $dbAdapter = $container->get(AdapterInterface::class);
  37. $resultSetPrototype = new ResultSet();
  38. $resultSetPrototype->setArrayObjectPrototype(new Model\UserBank());
  39. return new TableGateway('ps_user_bank_account', $dbAdapter, null, $resultSetPrototype);
  40. },
  41. ],
  42. ];
  43. }
  44.  
  45. public function getControllerConfig()
  46. {
  47. return [
  48. 'factories' => [
  49. Controller\MydataController::class => function ($container) {
  50. return new Controller\MydataController(
  51. $container->get(Model\MydataTable::class)
  52. );
  53. },
  54. ],
  55. ];
  56. }
  57. }


MydataController.php
  1.  
  2. <?php
  3.  
  4. namespace Mydata\Controller;
  5.  
  6. use Mydata\Model\UserBankTable;
  7. use Mydata\Model\MydataTable;
  8. use Mydata\Form\AccountNumberForm;
  9. use Mydata\Form\UserPasswordForm;
  10. use Mydata\Form\UserForm;
  11. use Zend\Mvc\MvcEvent;
  12. use Zend\Mvc\Controller\AbstractActionController;
  13. use Zend\View\Model\ViewModel;
  14.  
  15. class MydataController extends AbstractActionController
  16. {
  17. private $table;
  18.  
  19. public function __construct(MydataTable $table)
  20. {
  21. $this->table = $table;
  22. }
  23.  
  24. public function onDispatch(MvcEvent $e)
  25. {
  26. $response = parent::onDispatch($e);
  27. $this->layout()->setTemplate('layout/admin');
  28. return $response;
  29. }
  30.  
  31. public function indexAction()
  32. {
  33. $id = '1';
  34. $mydata = $this->table->getUser($id);
  35.  
  36.  
  37.  
  38. $UserForm = new UserForm();
  39. $UserForm->bind($mydata);
  40.  
  41. $AccountNumber = new AccountNumberForm();
  42.  
  43. $UserPasswordForm = new UserPasswordForm();
  44.  
  45. return new ViewModel(array(
  46. 'userForm' => $UserForm,
  47. 'accountForm' => $AccountNumber,
  48. 'passwordForm' => $UserPasswordForm
  49. ));
  50. }
Puszy
Nie rozumiem do końca co chcesz osiągnąć ale w Doctrine jest coś takiego jak mapowanie z dziedziczeniem http://docs.doctrine-project.org/projects/...ce-mapping.html
Dzięki temu możesz mieć encje nadrzędną Pojazd i podrzędne Motocykl, Autobus, Osobowy. Teraz odwołując się do encji Pojazd pobierasz dane ze wszystkich trzech tabel do jednej kolekcji.
To jest wersja lo-fi głównej zawartości. Aby zobaczyć pełną wersję z większą zawartością, obrazkami i formatowaniem proszę kliknij tutaj.
Invision Power Board © 2001-2025 Invision Power Services, Inc.