Tuto' "Débuter avec Zend Framework (approche MVC)"
Bonsoir !
Alors voilà mon souci. J'ai fini le tutoriel, j'ai tout le code qu'il propose, toutes les pages etc. Mon souci, c'est que les actions ajouter et modifier de l'IndexController ne fonctionnent pas.
Je pense que c'est du à mon Zend_Form vu que l'action supprimer, qui à contrario des deux actions citées ci-dessus ne contient pas mon Zend_Form, fonctionne.
Voici un screenshot de l'erreur que mon navigateur me retourne (la même pour les deux actions) :
http://pix.toile-libre.org/upload/or...1288033504.jpg
Ensuite, l'arborescence de mon projet :
http://pix.toile-libre.org/upload/or...1288033775.jpg
Enfin, voici les différentes pages de code qui vous permettront (j'espère) d'élucider mon problème :
IndexController.php
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
| <?php
class IndexController extends Zend_Controller_Action {
public function init() {
/* Initialize action controller here */
}
public function indexAction() {
$this->view->title = "Mes Albums";
$this->view->headTitle($this->view->title, 'PREPEND');
$albums = new Model_DbTable_Albums();
$this->view->albums = $albums->fetchAll();
}
public function ajouterAction() {
$this->view->title = "Ajouter un nouvel album";
$this->view->headTitle($this->view->title, 'PREPEND');
$form = new Form_Album();
$form->envoyer->setLabel('Ajouter');
$this->view->form = $form;
if ($this->getRequest()->isPost()) {
$formData = $this->getRequest()->getPost();
if ($form->isValid($formData)) {
$artiste = $form->getValue('artiste');
$titre = $form->getValue('titre');
$albums = new Model_DbTable_Albums();
$albums->ajouterAlbum($artiste, $titre);
$this->_redirect('/');
}
else
$form->populate($formData);
}
}
public function modifierAction() {
$this->view->title = 'Modifier l\'album';
$this->view->headTitle($this->view->title, 'PREPEND');
$form = new Form_Album();
$form->envoyer->setLabel('Sauvegarder');
$this->view->form = $form;
if ($this->getRequest()->isPost()) {
$formData = $this->getRequest()->getPost();
if ($form->isValid($formData)) {
$id = (int) $form->getValue('id');
$artiste = $form->getValue('artiste');
$titre = $form->getValue('titre');
$albums = new Model_DbTable_Albums();
$albums ->modifierAlbum($id, $artiste, $titre);
$this->_redirect('/');
}
else
$form->populate($formData);
}
else {
$id = $this->_getParam('id', 0);
if ($id > 0) {
$albums = new Model_DbTable_Albums();
$form->populate($albums->getAlbum($id));
}
}
}
public function supprimerAction() {
$this->view->title = "Supprimer l'album";
$this->view->headTitle($this->view->title, 'PREPEND');
if ($this->getRequest()->isPost()) {
$supprimer = $this->getRequest()->getPost('supprimer');
if ($supprimer == 'Oui') {
$id = $this->getRequest()->getPost('id');
$albums = new Model_DbTable_Albums();
$albums->supprimerAlbum($id);
}
$this->_redirect('/');
}
else {
$id = $this->_getParam('id', 0);
$albums = new Model_DbTable_Albums();
$this->view->album = $albums->getAlbum($id);
}
}
} |
Form_Album.php
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
| <?php
class Form_Album extends Zend_Form {
public function init() {
$this->setMethod('POST');
}
public function __construct($options = null) {
parent::__construct($options);
$this->setName('album');
$id = new Zend_Form_Element_Hidden('id');
$artiste = new Zend_Form_Element_Text('artiste');
$artiste->setLabel('Artiste')
->setRequired(true)
->addFilter('StripTags')
->addFilter('StringTrim')
->addValidator('NotEmpty');
$titre = new Zend_Form_Element_Text('titre');
$titre->setLabel('Titre')
->setRequired(true)
->addFilter('StripTags')
->addFilter('StringTrim')
->addValidator('NotEmpty');
$envoyer = new Zend_Form_Element_Submit('envoyer');
$envoyer->setAttrib('id', 'boutonenvoyer');
$this->addElements(array($id, $artiste, $titre, $envoyer));
}
}
?> |
Albums.php (model)
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
| <?php
class Model_DbTable_Albums extends Zend_Db_Table_Abstract {
protected $_name = "albums";
public function getAlbum($id) {
$id = (int) $id;
$row = $this->fetchRow('id = ' . $id);
if (!$row)
throw new Exception('Count not find row : ' . $id);
return $row->toArray();
}
public function ajouterAlbum($artiste, $titre) {
$data = array(
'artiste' => $artiste,
'titre' => $titre
);
$this->insert($data);
}
public function modifierAlbum($id, $artiste, $titre) {
$data = array(
'artiste' => $artiste,
'titre' => $titre,
);
$this->update($data, 'id = ' . (int) $id);
}
public function supprimerAlbum($id) {
$this->delete('id = ' . (int) $id);
}
}
?> |
ajouter.phtml et modifier.phtml
Code:
<?php echo $this->form; ?>
Merci d'avance !