Populate qui ne fonctionne pas ou autre chose ?
Bonjour,
À partir du tutoriel "Débuter avec Zend Framework, approche MVC", j'ai réalisé un ensemble similaire déjà évoqué dans cette discussion.
Pour rappel donc, j'ai le formulaire d'ajout ou de modification qui apparaît dans la même vue que le tableau de données, contrairement à l'exemple des albums du tutoriel.
L'ajout se passe correctement mais pour la modification, la discipline choisie n'apparaît pas dans le formulaire de modification.
Le formulaire :
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
| class Application_Form_Discipline extends Zend_Form
{
public function init()
{
$baseurl = Zend_Controller_Front::getInstance()->getBaseUrl();
$this->setName('discipline');
$this->setMethod('post');
// Identifiant de la discipline
$dsc_id = new Zend_Form_Element_Hidden('dsc_id');
$dsc_id->addFilter('Int');
// Nom de la discipline
$dsc_nom = new Zend_Form_Element_Text('dsc_nom');
$dsc_nom->setRequired(true)
->addFilter('StripTags')
->addFilter('StringTrim');
// Bouton Envoyer
$envoyer = new Zend_Form_Element_Submit('envoyer');
$envoyer->setLabel('Enregistrer');
$this->addElements(array($dsc_id, $dsc_nom, $envoyer));
}
} |
Le modèle :
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
| class Application_Model_DbTable_Discipline extends Zend_Db_Table_Abstract
{
protected $_name = 'te_discipline_dsc';
protected $_primary = 'dsc_id';
public function ajouterDiscipline($dsc_nom)
{
$data = array('dsc_nom' => $dsc_nom);
return $this->insert($data);
}
public function modifierDiscipline($id, $nom)
{
$data = array(
'dsc_nom' => $nom
);
$this->update($data, 'dsc_id = '. (int)$id);
}
public function obtenirDiscipline($id)
{
$id = (int)$id;
$row = $this->fetchRow('dsc_id = ' . $id);
if (!$row)
{
throw new Exception("Impossible de trouver la discipline $id !");
}
$result = $row->toArray();
return $result;
}
} |
Le contrôleur :
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
| public function modifierAction()
{
$this->view->action = 'ajouter';
$form = new Application_Form_Discipline();
$form->dsc_nom->setLabel('Modifier le nom de la discipline en');
$this->view->form = $form;
$disciplines = new Application_Model_DbTable_Discipline();
$this->view->disciplines = $disciplines->fetchAll();
$this->renderScript('/gererdisciplines/index.phtml');
if ($this->getRequest()->isPost())
{
$formData = $this->getRequest()->getPost();
if ($form->isValid($formData))
{
$dsc_id = $form->getValue('dsc_id');
$dsc_nom = $form->getValue('dsc_nom');
$discipline = new Application_Model_DbTable_Discipline();
$discipline->modifierDiscipline($dsc_id, $dsc_nom);
$this->_helper->redirector('index', 'gererdisciplines');
}
else
{
$form->populate($formData);
}
}
else
{
$dsc_id = $this->_getParam('dsc_id', 0);
if ($dsc_id > 0)
{
$result = $disciplines->obtenirDiscipline($dsc_id);
var_dump($result);
$form->populate($result);
}
}
} |
La vue :
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
| <?php
$this->headTitle('Gérer disciplines');
?>
<a href="<?php echo $this->url(array('controller' => 'accueilgestionnaire',
'action' => 'index')); ?>">Retour à l'accueil gestionnaire</a><br />
<?php if($this->action == 'index') : ?>
<a href="<?php echo $this->url(array('controller' => 'gererdisciplines',
'action' => 'ajouter')); ?>">Ajouter une discipline</a>
<?php else : ?>
<?php echo $this->form; ?>
<?php endif; ?>
<table>
<tr>
<th>Nom de la discipline</th>
<th> </th>
</tr>
<?php foreach($this->disciplines as $discipline) : ?>
<tr>
<td><?php echo $this->escape($discipline->dsc_nom);?></td>
<td>
<a href="<?php echo $this->url(array('controller'=>'gererdisciplines',
'action'=>'modifier', 'dsc_id'=>$discipline->dsc_id));?>">Modifier</a>
<a href="<?php echo $this->url(array('controller'=>'gererdisciplines',
'action'=>'supprimer', 'dsc_id'=>$discipline->dsc_id));?>">Supprimer</a>
</td>
</tr>
<?php endforeach; ?>
</table> |
J'ai mis un var_dump dans le contrôleur pour vérifier que je récupère bien les données et c'est le cas.
Il semblerait donc que ce soit le $form->populate($result); qui n'opère pas comme il le devrait.
Si dans la vue je fais ceci :
Code:
1 2 3 4
| <?php else : ?>
<?php echo $this->form;
var_dump($this->form->getValue('dsc_id'));?>
<?php endif; ?> |
J'obtiens un magnifique int(0) ! :calim2: