I have some problems to find the solution by myself, that's why I ask the community. I am developping an app with Zend Framework.
I have a form, with some SELECT Elements. Some of them are populated with data from the DB, and some others with data inside the form ( ->addMultiOption(0, 'Option 1')...).
The insert works well, but I have some troubles with the update function. The SELECT elements filled with the options inside the form are populated with success, but the SELECT elements filled with the DB data are not setted correctly. The first option of the table of the DB is always selected.
So I guess there's something wrong in my code. I deleted some elements for better visibility.
My Form:
<?php
class Application_Form_Pda extends Zend_Form
{
public function init()
{
$this->setName('ajouter');
$addFabForm = new Zend_Form;
$addFabForm->setAction('#'); // A compléter
$addFabForm->setMethod('post');
$addFabForm->setAttrib('id', ''); // A compléter
//Id
$id = new Zend_Form_Element_Hidden('PDA_ID');
$id->addFilter('Int');
//Select du Fabriquant
$nomFabriquant = new Zend_Form_Element_Select('FAB_NOM');
$nomFabriquant ->setLabel('Fabriquant')
->setRequired(true)
->addErrorMessage('Vous devez sélectionner un fabriquant');
$TableFab = new Application_Model_DbTable_Fabriquant();
//$nomFabriquant->addMultiOption(0, "");
foreach ($TableFab->fetchAll() as $ind) {
$nomFabriquant->addMultiOption($ind['FAB_ID'], $ind['FAB_NOM']);
}
// Select du Modèle
$nomModele = new Zend_Form_Element_Select('MOD_NOM');
$nomModele ->setLabel('Modèle')
->setRequired(true)
->addErrorMessage('Vous devez sélectionner un modèle');
$TableMod = new Application_Model_DbTable_Modele();
//$nomModele->addMultiOption(0, "");
foreach ($TableMod->fetchAll() as $ind) {
$nomModele ->addMultiOption($ind->MOD_ID, $ind->MOD_NOM);
}
// Champs texte, numéro IMEI
$imei = new Zend_Form_Element_Text('PDA_IMEI');
$imei ->setRequired(true)
->setLabel('IMEI')
->addFilter('StripTags')
/*->addValidator('Default_Validator_Imei')*/
->addValidator('NotEmpty');
// Select du Site
$nomSite = new Zend_Form_Element_Select('PDA_SITE');
$nomSite ->setLabel('Site')
->setRequired(true)
->addMultiOption(0, "Nantes")
->addMultiOption(1, "Paris")
->addErrorMessage('Vous devez sélectionner un site');
// Achat DSIV
$dsiv = new Zend_Form_Element_Checkbox('PDA_ACHATDSIV');
$dsiv ->setChecked(true)
->setLabel('Achat SVSI');
// Checkbox boite
$boite = new Zend_Form_Element_Checkbox('PDA_OPT_BOITE');
$boite ->setChecked(true)
->setLabel('Boite');
// Checkbox USB
$usb = new Zend_Form_Element_Checkbox('PDA_OPT_USB');
$usb ->setChecked(true)
->setLabel('Cable USB');
// Checkbox chargeur
$chargeur = new Zend_Form_Element_Checkbox('PDA_OPT_CHARGEUR');
$chargeur ->setChecked(true)
->setLabel('Chargeur');
// Checkbox casque
$casque = new Zend_Form_Element_Checkbox('PDA_OPT_CASQUE');
$casque ->setChecked(true)
->setLabel('Casque');
// Checkbox batterie
$bat = new Zend_Form_Element_Checkbox('PDA_OPT_BATTERIE');
$bat ->setChecked(true)
->setLabel('Batterie');
// Checkbox filaire
$fil = new Zend_Form_Element_Checkbox('PDA_OPT_FILAIRE');
$fil ->setChecked(true)
->setLabel('Filaire');
// Select de l'état
$etat = new Zend_Form_Element_Select('PDA_ETAT');
$etat ->setLabel('Etat')
->setRequired(true)
->addMultiOption(0, "Stock")
->addMultiOption(1, "En prêt")
->addMultiOption(2, "SAV")
->addMultiOption(3, "Sorti du parc")
->setValue('Stock')
->setLabel('Etat')
->addErrorMessage('Vous devez sélectionner un état');
$validPda = new Zend_Form_Element_Submit('envoyer');
$validPda ->setAttrib('id', 'boutonenvoyer');
$this->addElements(array($id, $nomFabriquant, $nomModele, $imei, $nomSite, $dsiv, $boite, $usb, $chargeur, $casque, $bat, $fil, $etat, $validPda));
}
}
My Controller:
<?php
$form = new Application_Form_Pda(); //Création du formulaire
$form->envoyer->setLabel('Modifier'); // Ajout d'un libellé au bouton d'envoi
$this->view->form = $form; // Envoi du formulaire à la vue
if ($this->getRequest()->isPost()) { // si la méthode isPost de l'objet Request renvoi True, le formulaire à été envoyé
$formData = $this->getRequest()->getPost(); // Récupération des données avec la méthode getPost()
if ($form->isValid($formData)) { // Vérification de ces données avec la méthode isValid()
$id = (int)$form->getValue('PDA_ID'); // Récupération de l'ID du fabriquant
$idModele = $form->getValue('MOD_NOM');
$imei = $form->getValue('PDA_IMEI');
$nomSite = $form->getValue('PDA_SITE');
$dsiv = $form->getValue('PDA_ACHATDSIV');
$boite = $form->getValue('PDA_OPT_BOITE');
$usb = $form->getValue('PDA_OPT_USB');
$chargeur = $form->getValue('PDA_OPT_CHARGEUR');
$casque = $form->getValue('PDA_OPT_CASQUE');
$bat = $form->getValue('PDA_OPT_BATTERIE');
$fil = $form->getValue('PDA_OPT_FILAIRE');
$etat = $form->getValue('PDA_ETAT');
$pda = new Application_Model_DbTable_Pda(); // On créé un nouvel enregistrement
$pda->modifierPda($id, $idModele, $imei, $dsiv, $boite, $usb, $chargeur, $casque, $bat, $fil, $nomSite, $etat); // On affecte à cet enregistrement le nom du fabriquant, et on persiste en BDD
$this->_helper->redirector('index'); // pour finir, on redirige vers index de Fabriquant
} else {
$form->populate($formData); // Si la validation n'est pas passée, on réaffiche les données dans le formulaire
}
}
else {
$id = $this->_getParam('id', 0);
//var_dump($id);
if ($id > 0) {
$pda = new Application_Model_DbTable_Pda();
$formData2 = $pda->getPda($id);
//echo "L'ID est : " . $id; // Vérification
$form->populate($formData2);
} else {
echo "Information : nous sommes dans le else de modifierAction de PdaController.php";
echo "L'ID est : " . $id;
}
}
The Model:
<?php
class Application_Model_DbTable_Pda extends Zend_Db_Table_Abstract
{
protected $_name = 'pda';
protected $_primary = 'PDA_ID'; // La primary key
public function getPda($id){
$id = (int)$id;
$row = $this->fetchRow('PDA_ID = ' . $id);
if (!$row) {
throw new Exception("Impossible de trouver la ligne $id");
}
return $row->toArray();
}
public function ajouterPda($idModele, $imei, $dsiv, $boite, $usb, $chargeur, $casque, $bat, $fil, $nomSite, $etat) {
$idModele = (int)$idModele;
$data = array(
'MOD_ID' => $idModele,
'PDA_IMEI' => $imei,
'PDA_ACHATDSIV' => $dsiv,
'PDA_OPT_BOITE' => $boite,
'PDA_OPT_USB' => $usb,
'PDA_OPT_CHARGEUR' => $chargeur,
'PDA_OPT_CASQUE' => $casque,
'PDA_OPT_BATTERIE' => $bat,
'PDA_OPT_FILAIRE' => $fil,
'PDA_SITE' => $nomSite,
'PDA_ETAT' => $etat
);
$this->insert($data);
}
public function modifierPda($id, $idModele, $imei, $dsiv, $boite, $usb, $chargeur, $casque, $bat, $fil, $nomSite, $etat) {
$id = (int)$id;
$idModele = (int)$idModele;
$data = array(
'MOD_ID' => $idModele,
'PDA_IMEI' => $imei,
'PDA_ACHATDSIV' => $dsiv,
'PDA_OPT_BOITE' => $boite,
'PDA_OPT_USB' => $usb,
'PDA_OPT_CHARGEUR' => $chargeur,
'PDA_OPT_CASQUE' => $casque,
'PDA_OPT_BATTERIE' => $bat,
'PDA_OPT_FILAIRE' => $fil,
'PDA_SITE' => $nomSite,
'PDA_ETAT' => $etat
);
$this->update($data, 'PDA_ID = ' . $id);
}
public function supprimerFabriquant($id) {
$this->delete('PDA_ID = ' . (int)$id);
}
}
The id of the 'Modèle' is good in the DB, but the element doesn't fill with the DB value... Any idea?
Thanks a lot for your help! :)
It looks to me like FAB_NOM
will not populate correctly because you don't seem to be passing the value from your DB table.
Also 'MOD_NOMlikes like it will work if you rename it to
MOD_ID`.
//no data present to reference to FAB_NOM and MOD_ID is present but MOD_NOM is not
$data = array(
'MOD_ID' => $idModele,
'PDA_IMEI' => $imei,
'PDA_ACHATDSIV' => $dsiv,
'PDA_OPT_BOITE' => $boite,
'PDA_OPT_USB' => $usb,
'PDA_OPT_CHARGEUR' => $chargeur,
'PDA_OPT_CASQUE' => $casque,
'PDA_OPT_BATTERIE' => $bat,
'PDA_OPT_FILAIRE' => $fil,
'PDA_SITE' => $nomSite,
'PDA_ETAT' => $etat
);
In order for populate()
to work correctly you need to have some data that the form recognizes as belonging to an element. Usually we accomplish this by naming our form elements the same as the columns we want to populate in our database.
Currently the select elements you are having problems with do not seem to have a column of the same name associated.