Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> [ZF] Przerwanie skryptu po wysłaniu formularza
IceManSpy
post
Post #1





Grupa: Zarejestrowani
Postów: 1 006
Pomógł: 111
Dołączył: 23.07.2010
Skąd: Kraków

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


Witam

Cały czak kontynuuję naukę Zenda i natknąłem się na fajny blog:
http://www.karolnowicki.pl/zend-framework/artykuly/
i wykonuje w nim krok po kroku wszystko z serii "Praca z bazą danych". W części pierwszej pominąłem dopisek do pliku Boostrap, ale dopisuje wszędzie przedrostek Application. Ale do rzeczy.
Model:
  1. class Application_Model_Uzytkownicy
  2. {
  3. protected $_name = 'uzytkownicy';
  4.  
  5. public function nowyUzytkownik($email, $haslo, $imie, $nazwisko, $rola, $status)
  6. {
  7. $row = $this->createRow();
  8. if ($row) {
  9. $row->email = $email;
  10. $row->haslo = $haslo;
  11. $row->imie = $imie;
  12. $row->nazwisko = $nazwisko;
  13. $row->rola = $rola;
  14. $row->status = $status;
  15. $row->save();
  16. return TRUE;
  17. } else {
  18. throw new Zend_Exception('Nie można utworzyć użytkownika. Błąd bazy danych!'); // warto tworzyć własne komunikaty o błędach, kiedy wykonujemy funkcje na tabelach
  19. }
  20. }
  21.  
  22. public function listaUzytkownikow()
  23. {
  24. $select = $this->select();
  25. $select->order('email ASC');
  26. $adapter = new Zend_Paginator_Adapter_DbTableSelect($select);
  27. return $adapter;
  28. }
  29. }

Kontrolery (ustawiłem sobie w nim echo, aby wiedzieć gdzie się wysypuje):
  1. <?php
  2.  
  3. class UzytkownicyController extends Zend_Controller_Action
  4. {
  5.  
  6. public function init()
  7. {
  8. /* Initialize action controller here */
  9. }
  10.  
  11. public function indexAction()
  12. {
  13.  
  14. }
  15. public function nowyUzytkownikAction()
  16. {
  17. $form = new Application_Form_Uzytkownik();
  18. $form->setAction('/uzytkownicy/nowy-uzytkownik')
  19. ->setMethod('post');
  20.  
  21. print_r($_POST);
  22. if($this->getRequest()->isPost()) {
  23. echo "odebrałem forma";
  24. if($form->isValid($_POST)) {
  25. echo "po walidacji";
  26. $modelUzytkownicy = new Application_Model_Uzytkownicy();
  27. $email = $form->getValue('email');
  28. $haslo = md5($form->getValue('haslo'));
  29. $imie = $form->getValue('imie');
  30. $nazwisko = $form->getValue('nazwisko');
  31. $rola = $form->getValue('rola');
  32. $status = $form->getValue('status');
  33. echo "odebrałem values";
  34. echo $email;
  35. $results = $modelUzytkownicy->nowyUzytkownik($email, $haslo, $imie, $nazwisko, $rola, $status);
  36. echo "obiekt stworzony";
  37. print_r($results);
  38. echo "po pront r";
  39. if($results) {
  40. echo "rezultat";
  41. return $this->_redirect('/uzytkownicy/lista-uzytkownikow');
  42. //$this->_helper->redirector('/uzytkownicy/lista-uzytkownikow'); // taka wersja też nie działa
  43. }
  44. }
  45. if($form->isErrors()) {
  46. $form->populate($_POST);
  47. }
  48. }
  49.  
  50. $this->view->form = $form;
  51. }
  52.  
  53. }


Formularz:
  1. class Application_Form_Uzytkownik extends Zend_Form
  2. {
  3.  
  4. public function init()
  5. {
  6. $email = $this->createElement('text', 'email');
  7. $email->setLabel('Adres e-mail:')
  8. ->setRequired(TRUE)
  9. ->setAttrib('size', 30)
  10. ->addFilters(array(
  11. new Zend_Filter_StringToLower(),
  12. new Zend_Filter_StringTrim(),
  13. new Zend_Filter_StripNewlines(),
  14. new Zend_Filter_StripTags()
  15. ))
  16. ->addValidators(array(
  17. new Zend_Validate_NotEmpty(),
  18. new Zend_Validate_EmailAddress(Zend_Validate_Hostname::ALLOW_DNS)
  19. ));
  20. $haslo = $this->createElement('password', 'haslo');
  21. $haslo->setLabel('Hasło:')
  22. ->setRequired(TRUE)
  23. ->setAttrib('size', 30)
  24. ->addFilters(array(
  25. new Zend_Filter_StringTrim(),
  26. new Zend_Filter_StripNewlines(),
  27. new Zend_Filter_StripTags()
  28. ))
  29. ->addValidators(array(
  30. new Zend_Validate_NotEmpty(),
  31. new Zend_Validate_StringLength(3, 12)
  32. ));
  33. $imie = $this->createElement('text', 'imie');
  34. $imie->setLabel('Imię:')
  35. ->setRequired(FALSE)
  36. ->setAttrib('size', 30)
  37. ->addFilters(array(
  38. new Zend_Filter_StringTrim(),
  39. new Zend_Filter_StripNewlines(),
  40. new Zend_Filter_StripTags()
  41. ))
  42. ->addValidators(array(
  43. new Zend_Validate_StringLength(2, 50)
  44. ));
  45. $nazwisko = $this->createElement('text', 'nazwisko');
  46. $nazwisko->setLabel('Nazwisko:')
  47. ->setRequired(FALSE)
  48. ->setAttrib('size', 30)
  49. ->addFilters(array(
  50. new Zend_Filter_StringTrim(),
  51. new Zend_Filter_StripNewlines(),
  52. new Zend_Filter_StripTags()
  53. ))
  54. ->addValidators(array(
  55. new Zend_Validate_StringLength(2, 50)
  56. ));
  57. $rola = $this->createElement('select', 'rola');
  58. $rola->setLabel('Rola użytkownika:')
  59. ->setRequired(TRUE)
  60. ->addFilters(array(
  61. new Zend_Filter_StringTrim(),
  62. new Zend_Filter_StripNewlines(),
  63. new Zend_Filter_StripTags()
  64. ))
  65. ->addValidators(array(
  66. new Zend_Validate_NotEmpty(),
  67. new Zend_Validate_StringLength(4, 5)
  68. ))
  69. ->addMultiOptions(array(
  70. '' => 'Wybierz rolę z listy',
  71. 'user' => 'Gość',
  72. 'autor' => 'Autor',
  73. 'admin' => 'Administrator'
  74. ));
  75. $status = $this->createElement('select', 'status');
  76. $status->setLabel('Status użytkownika:')
  77. ->setRequired(TRUE)
  78. ->addFilters(array(
  79. new Zend_Filter_StringTrim(),
  80. new Zend_Filter_StripNewlines(),
  81. new Zend_Filter_StripTags()
  82. ))
  83. ->addValidators(array(
  84. new Zend_Validate_NotEmpty(),
  85. new Zend_Validate_StringLength(1, 1)
  86. ))
  87. ->addMultiOptions(array(
  88. '' => 'Wybierz status z listy',
  89. '0' => 'Zawieszony',
  90. '1' => 'Aktywny'
  91. ));
  92. $id = $this->createElement('hidden', 'id');
  93. $this->addElements(array(
  94. $email,
  95. $haslo,
  96. $imie,
  97. $nazwisko,
  98. $rola,
  99. $status,
  100. 'submit', 'submit', array(
  101. 'label' => 'zapisz'
  102. )
  103. ),
  104. $id
  105. ));
  106. }
  107.  
  108.  
  109. }


I teraz gdy wypełnię formularz to zatrzymuje mi się na wykonaniu metody nowyUzytkownik. Wyświetla się tylko wszystko do echo "odebrałem values"; echo $email; a potem jest pusta, biała strona. Gdy błędnie wypełnię formularz, to pokazują się komunikaty o błędach i wraca do strony z formularzem. W bazie nie ma żadnych nowych rekordów. To samo się dzieje (białą, pusta strona), gdy chcę wyświetlić listę użytkowników (adres/uzytkownicy/lista-uzytkownikow).

Plik applacation.ini:
Kod
[production]
phpSettings.display_startup_errors = 0
phpSettings.display_errors = 0
includePaths.library = APPLICATION_PATH "/../library"
bootstrap.path = APPLICATION_PATH "/Bootstrap.php"
bootstrap.class = "Bootstrap"
appnamespace = "Application"
resources.frontController.controllerDirectory = APPLICATION_PATH "/controllers"
resources.frontController.params.displayExceptions = 0

resources.layout.layoutPath = APPLICATION_PATH "/layouts/scripts/"
resources.db.adapter = "pdo_mysql"
resources.db.params.host = "localhost"
resources.db.params.username = "user"
resources.db.params.password = "haslo"
resources.db.params.dbname = "naukazend2"
resources.db.params.charset = "utf8"
resources.db.isDefaultTableAdapter = true


[staging : production]

[testing : production]
phpSettings.display_startup_errors = 1
phpSettings.display_errors = 1

[development : production]
phpSettings.display_startup_errors = 1
phpSettings.display_errors = 1
resources.frontController.params.displayExceptions = 1


Gdzie jest błąd? (IMG:style_emoticons/default/sad.gif)

ROZWIĄZAŁEM:
Model ma być dziedziczony z Zend_Db_Table (IMG:style_emoticons/default/smile.gif)

Ten post edytował IceManSpy 5.04.2011, 22:39:48
Go to the top of the page
+Quote Post

Posty w temacie


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

 



RSS Aktualny czas: 22.08.2025 - 11:41