Comme moi, vous adorez la génération automatique des modules et des formulaires avec Doctrine. C'est tellement simple de générer des formulaires à une telle vitesse, mais qu'est-ce que ça peut être casse-pieds parfois de ne pas obtenir exactement ce que vous voulez.

Nous avons déjà vu dans le post précédent que nous pouvions modifier la mise en forme des formulaires. Sachez qu'il est aussi possible d'intervenir de manière plus large sur ce que Doctrine génère automatiquement.

Vous allez voir comment dompter le générateur de Doctrine pour qu'il fasse ce que vous voulez.

Prenons un exemple : un formulaire Doctrine auto-généré

Comportement par défaut :

  • Vous ajoutez un nouvel élément
  • Vous remplissez le formulaire
  • Vous cliquez sur Save
  • Vous êtes redirigé vers l'action edit pour l'élément fraichement enregistré.

Comportement souhaité :

  • Vous ajoutez un nouvel élément
  • Vous remplissez le formulaire
  • Vous cliquez sur Save
  • Vous êtes redirigés vers l'index du module

En résumé, ce que vous voulez, c'est gagner du temps en tombant sur la liste des enregistrements, juste après en avoir rajouté un.

C'est parti.

Le fichier à modifier, en ligne 14 :

  1. // lib/vendor/symfony/lib/plugins/sfDoctrinePlugin/data/generator/sfDoctrineModule/default/parts/processFormAction.php
  2. protected function processForm(sfWebRequest $request, sfForm $form)
  3. {
  4. $form->bind($request->getParameter($form->getName()), $request->getFiles($form->getName()));
  5. if ($form->isValid())
  6. {
  7. $<?php echo $this->getSingularName() ?> = $form->save();
  8.  
  9. <?php if (isset($this->params['route_prefix']) && $this->params['route_prefix']): ?>
  10. $this->redirect('@<?php echo $this->getUrlForAction('edit') ?>?<?php echo $this->getPrimaryKeyUrlParams() ?>);
  11. <?php else: ?>
  12. // we don't feel like editing again !
  13. //$this->redirect('<?php echo $this->getModuleName() ?>/edit?<?php echo $this->getPrimaryKeyUrlParams() ?>);
  14. $this->redirect('<?php echo $this->getModuleName() ?>/index');
  15. <?php endif; ?>
  16. }
  17. }

Un petit test : on génère ses modules : ça marche.

Petite amélioration

Moi j'suis pas d'accord. Si je retourne sur l'index, je vais pas m'amuser à chercher parmi mes 40 000 enregistrements si tout s'est bien passé ! Comment je peux faire ?

Hé ben mon Coco, la réponse est simple : tu ne cherches pas.

Non, on ne va pas chercher, mais juste notifier l'utilisateur du bon déroulement de l'opération :

  1. $this->getUser()->setFlash('notice', "Your record has been saved !");

Côté action, cela donne en ligne 12 :

  1. // lib/vendor/symfony/lib/plugins/sfDoctrinePlugin/data/generator/sfDoctrineModule/default/parts/processFormAction.php
  2. protected function processForm(sfWebRequest $request, sfForm $form)
  3. {
  4. $form->bind($request->getParameter($form->getName()), $request->getFiles($form->getName()));
  5. if ($form->isValid())
  6. {
  7. $<?php echo $this->getSingularName() ?> = $form->save();
  8.  
  9. <?php if (isset($this->params['route_prefix']) && $this->params['route_prefix']): ?>
  10. $this->redirect('@<?php echo $this->getUrlForAction('edit') ?>?<?php echo $this->getPrimaryKeyUrlParams() ?>);
  11. <?php else: ?>
  12. $this->getUser()->setFlash('notice', "Your record has been saved !");
  13. // we don't feel like editing again !
  14. //$this->redirect('<?php echo $this->getModuleName() ?>/edit?<?php echo $this->getPrimaryKeyUrlParams() ?>);
  15. $this->redirect('<?php echo $this->getModuleName() ?>/index');
  16. <?php endif; ?>
  17. }
  18. }

Côté template, en lignes 5 à 7 :

  1. <?php
  2. // lib/vendor/symfony/lib/plugins/sfDoctrinePlugin/data/generator/sfDoctrineModule/default/template/templates/indexSuccess.php
  3. ?>
  4. <h1><?php echo sfInflector::humanize($this->getPluralName()) ?> List</h1>
  5. [?php if($sf_user->hasFlash('notice')): ?]
  6. [?php echo $sf_user->getFlash('notice') ?]
  7. [?php endif; ?]
  8. <table>
  9. <thead>
  10. <tr>
  11. <?php foreach ($this->getColumns() as $column): ?>
  12. <th><?php echo sfInflector::humanize(sfInflector::underscore($column->getPhpName())) ?></th>
  13. <?php endforeach; ?>
  14. </tr>
  15. </thead>
  16. <tbody>
  17. [?php foreach ($<?php echo $this->getPluralName() ?> as $<?php echo $this->getSingularName() ?>): ?]
  18. <tr>
  19. <?php foreach ($this->getColumns() as $column): ?>
  20. <?php if ($column->isPrimaryKey()): ?>
  21. <?php if (isset($this->params['route_prefix']) && $this->params['route_prefix']): ?>
  22. <td><a href="[?php echo url_for('<?php echo $this->getUrlForAction(isset($this->params['with_show']) && $this->params['with_show'] ? 'show' : 'edit') ?>', $<?php echo $this->getSingularName() ?>) ?]">[?php echo $<?php echo $this->getSingularName() ?>->get<?php echo sfInflector::camelize($column->getPhpName()) ?>() ?]</a></td>
  23. <?php else: ?>
  24. <td><a href="[?php echo url_for('<?php echo $this->getModuleName() ?>/<?php echo isset($this->params['with_show']) && $this->params['with_show'] ? 'show' : 'edit' ?>?<?php echo $this->getPrimaryKeyUrlParams() ?>) ?]">[?php echo $<?php echo $this->getSingularName() ?>->get<?php echo sfInflector::camelize($column->getPhpName()) ?>() ?]</a></td>
  25. <?php endif; ?>
  26. <?php else: ?>
  27. <td>[?php echo $<?php echo $this->getSingularName() ?>->get<?php echo sfInflector::camelize($column->getPhpName()) ?>() ?]</td>
  28. <?php endif; ?>
  29. <?php endforeach; ?>
  30. </tr>
  31. [?php endforeach; ?]
  32. </tbody>
  33. </table>
  34.  
  35. <?php if (isset($this->params['route_prefix']) && $this->params['route_prefix']): ?>
  36. <a href="[?php echo url_for('<?php echo $this->getUrlForAction('new') ?>') ?]">New</a>
  37. <?php else: ?>
  38. <a href="[?php echo url_for('<?php echo $this->getModuleName() ?>/new') ?]">New</a>
  39. <?php endif; ?>

A vous de jouer pour améliorer ces petits mécanismes !