Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: [ZendFramework] Undefined variable: user in view
Forum PHP.pl > Forum > PHP > Frameworki
michal47
Witam,
mam mam problem z ktorym tocze walke od kilku godzin i czuje sie zagubiony wiec prosze was o pomoc.
Jestem poczatkujacy w php i to moja pierwsza aplikacjia w framworku zend.
Tworze panel do zarzadzania uzytkownikami i otrzymuje komunikat :
<pre>
Użytkownicy systemu
Nazwa użytkownika Email Hasło

Notice: Undefined variable: user in C:\xampp\htdocs\zend\module\Users\view\users\user-manager\index.phtml on line 12

Notice: Trying to get property of non-object in C:\xampp\htdocs\zend\module\Users\view\users\user-manager\index.phtml on line 12

Notice: Undefined variable: user in C:\xampp\htdocs\zend\module\Users\view\users\user-manager\index.phtml on line 13

Notice: Trying to get property of non-object in C:\xampp\htdocs\zend\module\Users\view\users\user-manager\index.phtml on line 13

Notice: Undefined variable: user in C:\xampp\htdocs\zend\module\Users\view\users\user-manager\index.phtml on line 14

Notice: Trying to get property of non-object in C:\xampp\htdocs\zend\module\Users\view\users\user-manager\index.phtml on line 14
</pre>

zmienna ktora przekazuje z kontrollera istnieje i zawiera tablice z uzytkownikami sprawdzalem za pomoca <<var_dump>>

oto moje klasy :

CONTROLLER

<pre>
<?php
namespace Users\Controller;

use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\ViewModel;

use Zend\Authentication\AuthenticationService;
use Zend\Authentication\Adapter\DbTable as DbTableAuthAdapter;

use Users\Form\RegisterForm;
use Users\Form\RegisterFilter;

use Users\Model\User;
use Users\Model\UserTable;
use Users\Model\UserMail;

class UserManagerController extends AbstractActionController
{
public function indexAction()
{
$this->layout('layout/layout');

$userTable = $this->getServiceLocator()->get('UserTable');
$viewModel = new ViewModel(array('users' => $userTable->fetchAll()));
return $viewModel;
}


}

?>
</pre>

VIDOK
<pre>
<h3>Użytkownicy systemu</h3>
<table class="table">
<tr>
<th>Nazwa użytkownika</th>
<th>Email</th>
<th>Hasło</th>
<th>&nbsp;</th>
</tr>

<? foreach($users as $user): ?>
<tr>
<td><?php echo $this->escapeHtml($user->name);?></td>
<td><?php echo $this->escapeHtml($user->email);?></td>
<td><?php echo $this->escapeHtml($user->password);?></td>
<td>
<a href="<?php echo $this->url('users/user-manager', array('action'=>'edit', 'id' => $user->id));?>">Edycja</a> |
<a href="<?php echo $this->url('users/user-manager', array('action'=>'delete', 'id' => $user->id));?>" onclick="return confirm('Jesteś pewien ?')">Usunięcie</a>
</td>
</tr>
<? endforeach; ?>
</table>
</pre>

FUNKCJA DLA SERVICELOCATOR W MODUL.PHP
<pre>
public function getServiceConfig()
{
return array(
'abstract_factories' => array(),
'aliases' => array(),
'factories' => array(
// SERWISY
'AuthService' => function($sm)
{
$dbAdapter = $sm->get('Zend\Db\Adapter\Adapter');
$dbTableAuthAdapter = new DbTableAuthAdapter($dbAdapter, 'user','email','password', 'MD5(?)');

$authService = new AuthenticationService();
$authService->setAdapter($dbTableAuthAdapter);
return $authService;
},
'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);
},
'UserEditForm' => function ($sm)
{
$form = new \Users\Form\UserEditForm();
$form->setInputFilter($sm->get('UserEditFilter'));
return $form;
},
'UserEditFilter' => function ($sm)
{
return new \Users\Form\UserEditFilter();
},
)
);
}
</pre>

zmienna przekazuje do widoku, gdy dodalem ten kod do widoku:
<pre>
<?php if ($this->users): ?>
<p>
zmienna zostala przekazana
</p>
<?php endif ?>
</pre>
i wyswietla mi sie tekst zmienna zostala przekazane, wiec nie czaje co jest nie tak i jeszcze raz zwracam sie o pomoc do was
Turson
Zrób var_dumpa w widoku a nie kontrolerze
michal47
chcialbym sprostowac powyzszego maila
dajac w kontroleze
<pre>
public function indexAction()
{
$this->layout('layout/layout');

$userTable = $this->getServiceLocator()->get('UserTable');
$viewModel = new ViewModel(array('users' => $userTable->fetchAll()));
var_dump( $userTable->fetchAll());die();
return $viewModel;
}
</pre>
otrzymuje pusta tablice

<pre>
object(Zend\Db\ResultSet\ResultSet)#276 (8) { ["allowedReturnTypes":protected]=> array(2) { [0]=> string(11) "arrayobject" [1]=> string(5) "array" } ["arrayObjectPrototype":protected]=> object(Users\Model\User)#257 (4) { ["id"]=> NULL ["name"]=> NULL ["email"]=> NULL ["password"]=> NULL } ["returnType":protected]=> string(11) "arrayobject" ["buffer":protected]=> NULL ["count":protected]=> int(3) ["dataSource":protected]=> object(Zend\Db\Adapter\Driver\Pdo\Result)#267 (8) { ["statementMode":protected]=> string(7) "forward" ["resource":protected]=> object(PDOStatement)#268 (1) { ["queryString"]=> string(27) "SELECT `user`.* FROM `user`" } ["options":protected]=> NULL ["currentComplete":protected]=> bool(false) ["currentData":protected]=> NULL ["position":protected]=> int(-1) ["generatedValue":protected]=> string(1) "0" ["rowCount":protected]=> int(3) } ["fieldCount":protected]=> int(4) ["position":protected]=> int(0) }
</pre>

jezeli ktos bylby zainteresowany to prosilbym o kontakt na dextermailx@gmail.com
przesle cala aplikacjie zapewne to jakas drobnostka
RiE
1. To używaj odpowiednich tagów przy pisaniu postów na forum, aby zachować formatowanie tekstu, bo strasznie źle się czyta taki plain text.
2. W widoku odwołujesz się w ten sposób do zmiennych nadanych w kontrolerze
  1. $this->{klucz_tablicy_z_kontrolera}


Tak więc, zamiast:
  1. <? foreach($users as $user): ?>
  2. <tr>
  3. <td><?php echo $this->escapeHtml($user->name);?></td>
  4. <td><?php echo $this->escapeHtml($user->email);?></td>
  5. <td><?php echo $this->escapeHtml($user->password);?></td>
  6. <td>
  7. <a href="<?php echo $this->url('users/user-manager', array('action'=>'edit', 'id' => $user->id));?>">Edycja</a> |
  8. <a href="<?php echo $this->url('users/user-manager', array('action'=>'delete', 'id' => $user->id));?>" onclick="return confirm('Jesteś pewien ?')">Usunięcie</a>
  9. </td>
  10. </tr>
  11. <? endforeach; ?>


Powinno być:
  1. <? foreach($this->users as $user): ?>


Dlatego też ten kod zadziałał:
Cytat
zmienna przekazuje do widoku, gdy dodalem ten kod do widoku:
  1. <?php if ($this->users): ?>
  2. <p>
  3. zmienna zostala przekazana
  4. </p>
  5. <?php endif ?>

i wyswietla mi sie tekst zmienna zostala przekazane, wiec nie czaje co jest nie tak i jeszcze raz zwracam sie o pomoc do was

michal47
probowalem juz sie odwolywac do obiektu $users na kilka sposobow :
  1. <pre class="php-brief" style="font-family:monospace;">
  2. <? var_dump($users)or die() ?>
  3. <h3>Użytkownicy systemu</h3>
  4. <table class="table">
  5. <tr>
  6. <th>Nazwa użytkownika</th>
  7. <th>Email</th>
  8. <th>Hasło</th>
  9. <th>&nbsp;</th>
  10. </tr>
  11.  
  12. <? foreach($this->users as $user): ?>
  13. <tr>
  14. <td><?php echo $this->escapeHtml($user->name);?></td>
  15. <td><?php echo $this->escapeHtml($this->user->email);?></td>
  16. <td><?php echo $this->escapeHtml($this->user['password']);?></td>
  17. <td>
  18. <a href="<?php echo $this->url('users/user-manager', array('action'=>'edit', 'id' => $user->id));?>">Edycja</a> |
  19. <a href="<?php echo $this->url('users/user-manager', array('action'=>'delete', 'id' => $user->id));?>" onclick="return confirm('Jesteś pewien ?')">Usunięcie</a>
  20. </td>
  21. </tr>
  22. <? endforeach; ?>
  23. </table>
  24. </pre>



lecz nic z tego otrzymuje taki blad
  1. <pre class="php-brief" style="font-family:monospace;">
  2. Notice: Undefined variable: user in C:\xampp\htdocs\zend\module\Users\view\users\user-manager\index.phtml on line 13
  3.  
  4. Notice: Trying to get property of non-object in C:\xampp\htdocs\zend\module\Users\view\users\user-manager\index.phtml on line 13
  5. </pre>


a dajac w kontrollerze var_dum razem z funkcja ktora pobiera wszystkie rekordy z bazy
  1. public function indexAction()
  2. {
  3. $this->layout('layout/layout');
  4.  
  5. $userTable = $this->getServiceLocator()->get('UserTable');
  6. $viewModel = new ViewModel(array('users' => $userTable->fetchAll()));
  7. var_dump( $userTable->fetchAll());die();
  8. return $viewModel;
  9. }


klasa w folderze MODEL w ktorej znajduje sie funkcja fetchALL()
  1. <?php
  2.  
  3. namespace Users\Model;
  4.  
  5. use Zend\Db\Adapter\Adapter;
  6. use Zend\Db\ResultSet\ResultSet;
  7. use Zend\Db\TableGateway\TableGateway;
  8.  
  9. class UserTable
  10. {
  11. protected $tableGateway;
  12.  
  13. public function __construct(TableGateway $tableGateway)
  14. {
  15. $this->tableGateway = $tableGateway;
  16. }
  17.  
  18. public function saveUser(User $user)
  19. {
  20. $data = array(
  21. 'email' => $user->email,
  22. 'name' => $user->name,
  23. 'password' => $user->password,
  24. );
  25.  
  26. $id = (int)$user->id;
  27. if($id == 0)
  28. {
  29. $this->tableGateway->insert($data);
  30. }
  31. else
  32. {
  33. if($this->getUser($id))
  34. {
  35. $this->tableGateway->update($data, array('id' => $id));
  36. }
  37. else
  38. {
  39. throw new \Exception('Użytkownik o tym ID nie istnieje');
  40. }
  41. }
  42. }
  43.  
  44. public function getUser($id)
  45. {
  46. $id = (int)$id;
  47. $rowset = $this->tableGateway->select(array('id' => $id));
  48. $row = $rowset->current();
  49. if(!$row)
  50. {
  51. throw new \Exception('Nie mogę znaleźć rekordu o ID '.$id);
  52. }
  53.  
  54. return $row;
  55. }
  56.  
  57. public function fetchAll()
  58. {
  59. $resultSet = $this->tableGateway->select();
  60. return $resultSet;
  61. }
  62.  
  63. public function deleteUser($id)
  64. {
  65. $this->tableGateway->delete(array('id' => $id));
  66. }
  67.  
  68. public function getUserByEmail($user_email)
  69. {
  70. $rowset = $this->tableGateway->select(array('email' => $user_email));
  71. $row = $rowset->current();
  72. if (!$row)
  73. {
  74. throw new \Exception("Nie mogę znaleźć rekordu $user_email");
  75. }
  76.  
  77. return $row;
  78. }
  79. }
  80.  
  81.  
  82. ?>


w var_dump( $userTable->fetchAll());die(); otrzymuje :

  1. object(Zend\Db\ResultSet\ResultSet)#275 (8) { ["allowedReturnTypes":protected]=> array(2) { [0]=> string(11) "arrayobject" [1]=> string(5) "array" }
  2.  
  3. ["arrayObjectPrototype":protected]=> object(Users\Model\User)#256 (4) { ["id"]=> NULL ["name"]=> NULL ["email"]=> NULL ["password"]=> NULL }
  4.  
  5. ["returnType":protected]=> string(11) "arrayobject" ["buffer":protected]=> NULL ["count":protected]=> int(3) ["dataSource":protected]=>
  6.  
  7. object(Zend\Db\Adapter\Driver\Pdo\Result)#266 (8) { ["statementMode":protected]=> string(7) "forward" ["resource":protected]=> object(PDOStatement)#267 (1) {
  8.  
  9. ["queryString"]=> string(27) "SELECT `user`.* FROM `user`" } ["options":protected]=> NULL ["currentComplete":protected]=> bool(false)
  10.  
  11. ["currentData":protected]=> NULL ["position":protected]=> int(-1) ["generatedValue":protected]=> string(1) "0" ["rowCount":protected]=> int(3) }
  12.  
  13. ["fieldCount":protected]=> int(4) ["position":protected]=> int(0) }

nospor
Nie: <? foreach($this->users as $user): ?>
a: <?php foreach($this->users as $user): ?>

smile.gif

ps: oczywiscie analogicznie reszta odwolan, jak np:
<? endforeach; ?>
ma byc
<?php endforeach; ?>

itd
Turson
var_dumpa w widoku znowu zrobiłeś na $users a nie $this->users
nospor
@Turson czepiasz sie nie istotnych rzeczy.... problem juz wskazalem smile.gif
Turson
Jeżeli nieistotne to po co w ogóle robić var dumpa wink.gif
nospor
No wlasnie mowie: wszystkie te var_dumy sa nie istotne. Problem jest w tym, ze <? nie jest interpretowane jako php... Ot i caly problem
michal47
dzieki wielkie dziala poprawnie

  1.  
  2. Nazwa użytkownika Email Hasło
  3. eduweb michal@sopra.fr 9056c0bbcb4075ff82dd99efe295922f Edycja | Usunięcie
  4. qwerty qwerty@eduweb.pl e130e5e618f15cee7a519d8b7b8306a0 Edycja | Usunięcie
  5. sopra aaa@gmail.com aaa Edycja | Usunięcie

wyswietla mi to co powinno gdy dalem :
  1.  
  2. <h3>Użytkownicy systemu</h3>
  3. <table class="table">
  4. <tr>
  5. <th>Nazwa użytkownika</th>
  6. <th>Email</th>
  7. <th>Hasło</th>
  8. <th>&nbsp;</th>
  9. </tr>
  10.  
  11. <?php foreach($this->users as $user): ?>
  12. <tr>
  13. <td><?php echo $this->escapeHtml($user->name);?></td>
  14. <td><?php echo $this->escapeHtml($user->email);?></td>
  15. <td><?php echo $this->escapeHtml($user->password);?></td>
  16. <td>
  17. <a href="<?php echo $this->url('users/user-manager', array('action'=>'edit', 'id' => $user->id));?>">Edycja</a> |
  18. <a href="<?php echo $this->url('users/user-manager', array('action'=>'delete', 'id' => $user->id));?>" onclick="return confirm('Jesteś pewien ?')">Usunięcie</a>
  19. </td>
  20. </tr>
  21. <?php endforeach; ?>
  22. </table>


stwierdzam ze za szybko sie porwalem na zenda i raczej powinienem powrocic do podstaw php
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.