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:
class Application_Model_Uzytkownicy
{
protected $_name = 'uzytkownicy';
public function nowyUzytkownik($email, $haslo, $imie, $nazwisko, $rola, $status)
{
$row = $this->createRow();
if ($row) {
$row->email = $email;
$row->haslo = $haslo;
$row->imie = $imie;
$row->nazwisko = $nazwisko;
$row->rola = $rola;
$row->status = $status;
$row->save();
return TRUE;
} else {
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
}
}
public function listaUzytkownikow()
{
$select = $this->select();
$select->order('email ASC');
$adapter = new Zend_Paginator_Adapter_DbTableSelect($select);
return $adapter;
}
}
Kontrolery (ustawiłem sobie w nim echo, aby wiedzieć gdzie się wysypuje):
<?php
class UzytkownicyController extends Zend_Controller_Action
{
public function init()
{
/* Initialize action controller here */
}
public function indexAction()
{
}
public function nowyUzytkownikAction()
{
$form = new Application_Form_Uzytkownik();
$form->setAction('/uzytkownicy/nowy-uzytkownik')
->setMethod('post');
if($this->getRequest()->isPost()) {
if($form->isValid($_POST)) {
$modelUzytkownicy = new Application_Model_Uzytkownicy();
$email = $form->getValue('email');
$haslo = md5($form->getValue('haslo')); $imie = $form->getValue('imie');
$nazwisko = $form->getValue('nazwisko');
$rola = $form->getValue('rola');
$status = $form->getValue('status');
$results = $modelUzytkownicy->nowyUzytkownik($email, $haslo, $imie, $nazwisko, $rola, $status);
if($results) {
return $this->_redirect('/uzytkownicy/lista-uzytkownikow');
//$this->_helper->redirector('/uzytkownicy/lista-uzytkownikow'); // taka wersja też nie działa
}
}
if($form->isErrors()) {
$form->populate($_POST);
}
}
$this->view->form = $form;
}
}
Formularz:
class Application_Form_Uzytkownik extends Zend_Form
{
public function init()
{
$email = $this->createElement('text', 'email');
$email->setLabel('Adres e-mail:')
->setRequired(TRUE)
->setAttrib('size', 30)
new Zend_Filter_StringToLower(),
new Zend_Filter_StringTrim(),
new Zend_Filter_StripNewlines(),
new Zend_Filter_StripTags()
))
new Zend_Validate_NotEmpty(),
new Zend_Validate_EmailAddress(Zend_Validate_Hostname::ALLOW_DNS)
));
$haslo = $this->createElement('password', 'haslo');
$haslo->setLabel('Hasło:')
->setRequired(TRUE)
->setAttrib('size', 30)
new Zend_Filter_StringTrim(),
new Zend_Filter_StripNewlines(),
new Zend_Filter_StripTags()
))
new Zend_Validate_NotEmpty(),
new Zend_Validate_StringLength(3, 12)
));
$imie = $this->createElement('text', 'imie');
$imie->setLabel('Imię:')
->setRequired(FALSE)
->setAttrib('size', 30)
new Zend_Filter_StringTrim(),
new Zend_Filter_StripNewlines(),
new Zend_Filter_StripTags()
))
new Zend_Validate_StringLength(2, 50)
));
$nazwisko = $this->createElement('text', 'nazwisko');
$nazwisko->setLabel('Nazwisko:')
->setRequired(FALSE)
->setAttrib('size', 30)
new Zend_Filter_StringTrim(),
new Zend_Filter_StripNewlines(),
new Zend_Filter_StripTags()
))
new Zend_Validate_StringLength(2, 50)
));
$rola = $this->createElement('select', 'rola');
$rola->setLabel('Rola użytkownika:')
->setRequired(TRUE)
new Zend_Filter_StringTrim(),
new Zend_Filter_StripNewlines(),
new Zend_Filter_StripTags()
))
new Zend_Validate_NotEmpty(),
new Zend_Validate_StringLength(4, 5)
))
'' => 'Wybierz rolę z listy',
'user' => 'Gość',
'autor' => 'Autor',
'admin' => 'Administrator'
));
$status = $this->createElement('select', 'status');
$status->setLabel('Status użytkownika:')
->setRequired(TRUE)
new Zend_Filter_StringTrim(),
new Zend_Filter_StripNewlines(),
new Zend_Filter_StripTags()
))
new Zend_Validate_NotEmpty(),
new Zend_Validate_StringLength(1, 1)
))
'' => 'Wybierz status z listy',
'0' => 'Zawieszony',
'1' => 'Aktywny'
));
$id = $this->createElement('hidden', 'id');
$this->addElements(array( $email,
$haslo,
$imie,
$nazwisko,
$rola,
$status,
'submit', 'submit', array( 'label' => 'zapisz'
)
),
$id
));
}
}
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