Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

 
Reply to this topicStart new topic
> [inny][ZF2][ZendFramework2]Problem z wyświetleniem id.
cykcykacz
post 3.09.2013, 14:08:36
Post #1





Grupa: Zarejestrowani
Postów: 550
Pomógł: 9
Dołączył: 29.05.2009
Skąd: Ostrów Wielkopolski

Ostrzeżenie: (0%)
-----


Witam,
wyświetla mi wszystkie kolumny z tabelki prócz id. Musi to być jakis drobny problem. Daje kod, kotrollera, modelu, widoku.
Kontroller:
  1. public function indexAction()
  2. {
  3. $this->layout('layout/myaccount');
  4.  
  5. $userTable = $this->getServiceLocator()->get('UserTable');
  6. $viewModel = new ViewModel(array('users' => $userTable->fetchAll()));
  7. return $viewModel;
  8. }

Model:
  1. <?php
  2. namespace Users\Model;
  3.  
  4. use Zend\Db\Adapter\Adapter;
  5. use Zend\Db\ResultSet\ResultSet;
  6. use Zend\Db\TableGateway\TableGateway;
  7.  
  8. class UserTable
  9. {
  10. protected $tableGateway;
  11.  
  12. public function __construct(TableGateway $tableGateway)
  13. {
  14. $this->tableGateway = $tableGateway;
  15. }
  16.  
  17. public function saveUser(User $user)
  18. {
  19. $data = array(
  20. 'email' => $user->email,
  21. 'name' => $user->name,
  22. 'password' => $user->password,
  23. );
  24.  
  25. $id = (int)$user->id;
  26. if ($id == 0) {
  27. $this->tableGateway->insert($data);
  28. } else {
  29. if ($this->getUser($id)) {
  30. if (empty($data['password'])) {
  31. unset($data['password']);
  32. }
  33. $this->tableGateway->update($data, array('id' => $id));
  34. } else {
  35. throw new \Exception('User ID does not exist');
  36. }
  37. }
  38. }
  39.  
  40. public function fetchAll()
  41. {
  42. $resultSet = $this->tableGateway->select();
  43. return $resultSet;
  44. }
  45.  
  46. public function getUser($id)
  47. {
  48. $id = (int) $id;
  49. $rowset = $this->tableGateway->select(array('id' => $id));
  50. $row = $rowset->current();
  51. if (!$row) {
  52. throw new \Exception("Could not find row $id");
  53. }
  54. return $row;
  55. }
  56.  
  57. public function deleteUser($id)
  58. {
  59. $this->tableGateway->delete(array('id' => $id));
  60. }
  61.  
  62. /*
  63.   * Get User account by Email
  64.   */
  65. public function getUserByEmail($user_email)
  66. {
  67. $rowset = $this->tableGateway->select(array('email' => $user_email));
  68. $row = $rowset->current();
  69. if (!$row) {
  70. throw new \Exception("Could not find row $user_email");
  71. }
  72. return $row;
  73. }
  74. }
  75.  

Widok:
Kod
<h3>Users</h3>
<table class="table">
<tr>
    <th>Id</th>
    <th>Name</th>
    <th>User ID/Email</th>
    <th> </th>
</tr>
<?php foreach ($users as $user) : ?>
<tr>
    <td><?php echo $user->id?></td>
    <td><?php echo $this->escapeHtml($user->name);?></td>
    <td><?php echo $this->escapeHtml($user->email);?></td>
    <td>
        <a href="<?php echo $this->url('users/user-manager',
            array('action'=>'edit', 'id' => $user->id));?>">Edit</a> |
        <a href="<?php echo $this->url('users/user-manager',
            array('action'=>'delete', 'id' => $user->id));?>" onclick="return confirm('Are you sure?')">Delete</a>
    </td>
</tr>
<?php endforeach; ?>
</table>


Daj cie znać w czym problem.

Ten post edytował cykcykacz 3.09.2013, 14:10:17
Go to the top of the page
+Quote Post
buliq
post 3.09.2013, 14:19:37
Post #2





Grupa: Zarejestrowani
Postów: 559
Pomógł: 93
Dołączył: 4.03.2008
Skąd: Olsztyn

Ostrzeżenie: (0%)
-----


Nie rozumiem czegoś, używasz entity? bo w kodzie są odwołania, ale nie ma kodu entity, i nie widzę abyś podpinał wynik do entity


--------------------
KTOŚ TU PACZY???

Kompedium wiedzy
Go to the top of the page
+Quote Post
cykcykacz
post 3.09.2013, 14:25:39
Post #3





Grupa: Zarejestrowani
Postów: 550
Pomógł: 9
Dołączył: 29.05.2009
Skąd: Ostrów Wielkopolski

Ostrzeżenie: (0%)
-----


To jest przykład z książki...
Go to the top of the page
+Quote Post
buliq
post 3.09.2013, 14:47:33
Post #4





Grupa: Zarejestrowani
Postów: 559
Pomógł: 93
Dołączył: 4.03.2008
Skąd: Olsztyn

Ostrzeżenie: (0%)
-----


hmm, ok, to pokaż mi klasę User, masz ją w kodzie użytą


--------------------
KTOŚ TU PACZY???

Kompedium wiedzy
Go to the top of the page
+Quote Post
cykcykacz
post 3.09.2013, 20:35:35
Post #5





Grupa: Zarejestrowani
Postów: 550
Pomógł: 9
Dołączył: 29.05.2009
Skąd: Ostrów Wielkopolski

Ostrzeżenie: (0%)
-----


Od razu napiszę że rozwiązałem ten problem ale od razu powstał następny smile.gif A więc czytajcie do końca smile.gif.

Faktycznie w klasie user, w funkcji exchangeArray($data) brakowało id tak wyglądała przed:
  1. function exchangeArray($data)
  2. {
  3. $this->name = (isset($data['name'])) ?
  4. $data['name'] : null;
  5. $this->email = (isset($data['email'])) ?
  6. $data['email'] : null;
  7. if (isset($data["password"]))
  8. {
  9. $this->setPassword($data["password"]);
  10. }
  11. }

Tak wyglądała po
  1. <?php
  2. namespace Users\Model;
  3.  
  4. class User
  5. {
  6. public $id;
  7. public $name;
  8. public $email;
  9. public $password;
  10.  
  11. public function setPassword($clear_password)
  12. {
  13. $this->password = md5($clear_password);
  14. }
  15.  
  16. function exchangeArray($data)
  17. {
  18. $this->id = (isset($data['id'])) ?
  19. $data['id'] : null;
  20. $this->name = (isset($data['name'])) ?
  21. $data['name'] : null;
  22. $this->email = (isset($data['email'])) ?
  23. $data['email'] : null;
  24. if (isset($data["password"]))
  25. {
  26. $this->setPassword($data["password"]);
  27. }
  28. }
  29. }


I fajnie wyświetla mi dane z kolumny id.
Oczywiście teraz chcę edytować i kasować rekordy z tabeli. Kasować mogę ale edytować nie sad.gif Help me
Edit Action
  1. public function editAction()
  2. {
  3. $this->layout('layout/myaccount');
  4.  
  5. $userTable = $this->getServiceLocator()->get('UserTable');
  6.  
  7. $user = $userTable->getUser($this->params()->fromRoute('id'));
  8. $form = $this->getServiceLocator()->get('UserEditForm');
  9. $form->bind($user);
  10. $viewModel = new ViewModel(array('form' => $form, 'user_id' => $this->params()->fromRoute('id')));
  11. return $viewModel;
  12. }

UserEditForm
  1. <?php
  2. // filename : module/Users/src/Users/Form/RegisterForm.php
  3. namespace Users\Form;
  4.  
  5. use Zend\Form\Form;
  6.  
  7. class UserEditForm extends Form
  8. {
  9. public function __construct($name = null)
  10. {
  11. parent::__construct('Edit User');
  12. $this->setAttribute('method', 'post');
  13. $this->setAttribute('enctype','multipart/form-data');
  14.  
  15. $this->add(array(
  16. 'name' => 'id',
  17. 'attributes' => array(
  18. 'type' => 'hidden',
  19. ),
  20. ));
  21.  
  22. $this->add(array(
  23. 'name' => 'name',
  24. 'attributes' => array(
  25. 'type' => 'text',
  26. 'required' => 'required'
  27. ),
  28. 'options' => array(
  29. 'label' => 'Full Name',
  30. ),
  31. ));
  32.  
  33.  
  34. $this->add(array(
  35. 'name' => 'email',
  36. 'attributes' => array(
  37. 'type' => 'email',
  38. 'required' => 'required'
  39. ),
  40. 'options' => array(
  41. 'label' => 'Email',
  42. ),
  43. ));
  44.  
  45. $this->add(array(
  46. 'name' => 'submit',
  47. 'attributes' => array(
  48. 'type' => 'submit',
  49. 'value' => 'Save'
  50. ),
  51. ));
  52. }
  53. }

Edit.phtml
Kod
<h3>Edit User Information</h3>
<?php if ($this->error): ?>
<p class="error">
    There were one or more isues with your submission. Please correct them as
    indicated below.
</p>
<?php endif ?>
<section>
<?php
$form = $this->form;
$form->prepare();
$form->setAttribute('action', $this->url('users/user-manager', array('action' => 'process')));
$form->setAttribute('method', 'post');

echo $this->form()->openTag($form);
?>
<dl class="zend_form">
<dt><?php echo $this->formLabel($form->get('name')); ?></dt>
<?php echo $this->formElement($form->get('id')); ?>

<dd><?php
    echo $this->formElement($form->get('name'));
    echo $this->formElementErrors($form->get('name'));
?></dd>

<dt><?php echo $this->formLabel($form->get('email')); ?></dt>
<dd><?php
    echo $this->formElement($form->get('email'));
    echo $this->formElementErrors($form->get('email'));
?></dd>

<dd><?php
    echo $this->formElement($form->get('submit'));
    echo $this->formElementErrors($form->get('submit'));
?></dd>

</dl>
<?php echo $this->form()->closeTag() ?>
</section>


Po kliknięciu w Edit dostaję taki komunikat:
Kod
An error occurred
An error occurred during execution; please try again later.
Additional information:
Zend\ServiceManager\Exception\ServiceNotFoundException
File:
C:\wamp\www\zend2book\vendor\zendframework\zendframework\library\Zend\ServiceManager\ServiceManager.php:495
Message:
Zend\ServiceManager\ServiceManager::get was unable to fetch or create an instance for UserEditForm
Stack trace:
#0 C:\wamp\www\zend2book\module\Users\src\Users\Controller\UserManagerController.php(34): Zend\ServiceManager\ServiceManager->get('UserEditForm')
#1 C:\wamp\www\zend2book\vendor\zendframework\zendframework\library\Zend\Mvc\Controller\AbstractActionController.php(83): Users\Controller\UserManagerController->editAction()
#2 [internal function]: Zend\Mvc\Controller\AbstractActionController->onDispatch(Object(Zend\Mvc\MvcEvent))
#3 C:\wamp\www\zend2book\vendor\zendframework\zendframework\library\Zend\EventManager\EventManager.php(468): call_user_func(Array, Object(Zend\Mvc\MvcEvent))
#4 C:\wamp\www\zend2book\vendor\zendframework\zendframework\library\Zend\EventManager\EventManager.php(207): Zend\EventManager\EventManager->triggerListeners('dispatch', Object(Zend\Mvc\MvcEvent), Object(Closure))
#5 C:\wamp\www\zend2book\vendor\zendframework\zendframework\library\Zend\Mvc\Controller\AbstractController.php(117): Zend\EventManager\EventManager->trigger('dispatch', Object(Zend\Mvc\MvcEvent), Object(Closure))
#6 C:\wamp\www\zend2book\vendor\zendframework\zendframework\library\Zend\Mvc\DispatchListener.php(114): Zend\Mvc\Controller\AbstractController->dispatch(Object(Zend\Http\PhpEnvironment\Request), Object(Zend\Http\PhpEnvironment\Response))
#7 [internal function]: Zend\Mvc\DispatchListener->onDispatch(Object(Zend\Mvc\MvcEvent))
#8 C:\wamp\www\zend2book\vendor\zendframework\zendframework\library\Zend\EventManager\EventManager.php(468): call_user_func(Array, Object(Zend\Mvc\MvcEvent))
#9 C:\wamp\www\zend2book\vendor\zendframework\zendframework\library\Zend\EventManager\EventManager.php(207): Zend\EventManager\EventManager->triggerListeners('dispatch', Object(Zend\Mvc\MvcEvent), Object(Closure))
#10 C:\wamp\www\zend2book\vendor\zendframework\zendframework\library\Zend\Mvc\Application.php(309): Zend\EventManager\EventManager->trigger('dispatch', Object(Zend\Mvc\MvcEvent), Object(Closure))
#11 C:\wamp\www\zend2book\public\index.php(17): Zend\Mvc\Application->run()
#12 {main}


Nie wiem dlaczego?
Go to the top of the page
+Quote Post
buliq
post 4.09.2013, 07:26:15
Post #6





Grupa: Zarejestrowani
Postów: 559
Pomógł: 93
Dołączył: 4.03.2008
Skąd: Olsztyn

Ostrzeżenie: (0%)
-----


Czy w konfiguracji modułu definiowałeś zasób UserEditForm questionmark.gif To jest np metoda getServiceConfig w pliku Module.php

btw. Wiesz że są prostsze sposoby na ustawianie danych dla Entity? np z użyciem __get i __set

Ten post edytował buliq 4.09.2013, 07:29:41


--------------------
KTOŚ TU PACZY???

Kompedium wiedzy
Go to the top of the page
+Quote Post
cykcykacz
post 4.09.2013, 13:25:11
Post #7





Grupa: Zarejestrowani
Postów: 550
Pomógł: 9
Dołączył: 29.05.2009
Skąd: Ostrów Wielkopolski

Ostrzeżenie: (0%)
-----


Dzięki już sobie poradziłem faktycznie nie miałem w ustawieniach modułu dodane.
Narazie nie znam prostszych metod staram się przerobić książkę i klika tutorialii.
Thx.

Ten post edytował cykcykacz 4.09.2013, 13:26:08
Go to the top of the page
+Quote Post

Reply to this topicStart new topic
1 Użytkowników czyta ten temat (1 Gości i 0 Anonimowych użytkowników)
0 Zarejestrowanych:

 



RSS Wersja Lo-Fi Aktualny czas: 19.04.2024 - 00:16