<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Blog Symfony2 - Lexik Montpellier &#187; olivier</title>
	<atom:link href="http://www.lexik.fr/blog/symfony/author/olivier/feed" rel="self" type="application/rss+xml" />
	<link>http://www.lexik.fr/blog/symfony</link>
	<description>Blog sur le développement Web PHP avec Symfony 2</description>
	<lastBuildDate>Thu, 02 Feb 2012 14:00:26 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3</generator>
		<item>
		<title>Qui l&#8217;eu CRUD ? Ou comment créer un thème pour les CRUD Doctrine de Symfony.</title>
		<link>http://www.lexik.fr/blog/symfony/symfony/qui-leu-crud-ou-comment-creer-un-theme-pour-les-crud-doctrine-de-symfony-1181</link>
		<comments>http://www.lexik.fr/blog/symfony/symfony/qui-leu-crud-ou-comment-creer-un-theme-pour-les-crud-doctrine-de-symfony-1181#comments</comments>
		<pubDate>Mon, 09 Aug 2010 08:28:18 +0000</pubDate>
		<dc:creator>olivier</dc:creator>
				<category><![CDATA[1.3.x]]></category>
		<category><![CDATA[1.4.x]]></category>
		<category><![CDATA[doctrine]]></category>
		<category><![CDATA[Symfony]]></category>
		<category><![CDATA[crud]]></category>
		<category><![CDATA[generate]]></category>
		<category><![CDATA[module]]></category>
		<category><![CDATA[theme]]></category>

		<guid isPermaLink="false">http://www.lexik.fr/blog/symfony/?p=1181</guid>
		<description><![CDATA[Symfony propose des outils très puissants pour faciliter le développement d&#8217;applications et surtout la génération des modules grâce à l&#8217;Admin Generator et le CRUD. Chacunes de ces solutions à ses avantages et ses inconvénients. L&#8217;admin generator permet en une commande &#8230;<p class="more aright"><a href="http://www.lexik.fr/blog/symfony/symfony/qui-leu-crud-ou-comment-creer-un-theme-pour-les-crud-doctrine-de-symfony-1181">...</a></p>]]></description>
			<content:encoded><![CDATA[<p>Symfony propose des outils très puissants pour faciliter le développement d&#8217;applications et surtout la génération des modules grâce à l&#8217;Admin Generator et le CRUD. Chacunes de ces solutions  à ses avantages et ses inconvénients.</p>
<p>L&#8217;admin generator permet en une commande d&#8217;avoir un module complet et fonctionnel avec de nombreuses fonctionnalités, le tout relativement configurable. En contre partie, l&#8217;ajout de fonctionnalités spécifiques et la mise en forme peuvent rapidement s&#8217;avérer fastidieuses. Être obligé d&#8217;aller fouiller dans le cache pour aller faire des copier/coller afin de pouvoir surcharger une action, c&#8217;est comme qui dirait, bien mais pas top&#8230;</p>
<p><span id="more-1181"></span></p>
<p>  D&#8217;autre part le CRUD permet lui aussi de générer en une seule commade un module, mais ce module est plus que limité et on se retrouve systématiquement à ré-implémenter les mêmes fonctionnalités de base comme le pager, les filters, etc. Par contre travailler avec un CRUD permet de garder la main sur le code, ce qui n&#8217;est pas négligeable.</p>
<p>C&#8217;est pourquoi je vous propose de voir comment fabriquer un thème pour le CRUD de manière à l&#8217;enrichir de quelques fonctionnalités indispensables pour ne pas avoir à les ré-écrire systematiquement et ainsi ganger du temps. Je vais en profiter pour développer ce thème au sein d&#8217;un plugin de manière à pouvoir s&#8217;en resservir facilement.</p>
<p>Pour se faire je vais partir d&#8217;un projet vierge type sandbox auquel je rajoute le plugin <a href="http://www.symfony-project.org/plugins/sfTaskExtraPlugin">sfTaskExtraPlugin</a> qui facilite grandement la génération de plugins.<br />
Pour me simplifier la vie, je vais reprendre <a href="http://www.symfony-project.org/jobeet/1_4/Doctrine/en/03#chapter_03_the_schema">le schema.yml</a> et les fixtures (<a href="http://svn.jobeet.org/doctrine/trunk/data/fixtures/affiliates.yml">affiliates.yml</a>, <a href="http://svn.jobeet.org/doctrine/trunk/data/fixtures/category.yml">category.yml</a> et <a href="http://svn.jobeet.org/doctrine/trunk/data/fixtures/jobs.yml">jobs.yml</a>) de Jobeet pour avoir un modèle sur lequel m&#8217;appuyer pour les exemples.</p>
<p>(Attention le fichier de fixture category.yml comporte les traductions, alors que le schema n&#8217;a pas le behavior I18n. Il faut au choix modifier le schema.yml pour rajouter le behavior ou modifier les categories pour supprimer les traductions)</p>
<p>Un petit <i>./symfony doctrine:build &#8211;all &#8211;and-load</i> et nous voilà parti.</p>
<h2>Let&#8217;s rock!</h2>
<p>Pour fabriquer un module de type CRUD symfony se sert de template de code pour générer les actions et les vues. Ces fichiers sont bien cachés au fin fond du symfony ! On les trouve dans</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p1181code17'); return false;">View Code</a> PATH</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p118117"><td class="code" id="p1181code17"><pre class="path" style="font-family:monospace;">/lib/vendor/symfony/lib/plugins/sfDoctrinePlugin/data/generator/sfDoctrineModule/default</pre></td></tr></table></div>

</p>
<p>On va donc utiliser le thème par défaut que l&#8217;on va enrichir.</p>
<p>Tout d&#8217;abord, on va générer l&#8217;arborescence du plugin grâce à la commande generate:plugin de sfTaskExtraPlugin.</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p1181code18'); return false;">View Code</a> CONSOLE</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p118118"><td class="code" id="p1181code18"><pre class="console" style="font-family:monospace;">./symfony generate:plugin myCrudThemePlugin</pre></td></tr></table></div>

<p>Puis on prépare l&#8217;arborescence qui va accueillir les fichiers du template à l&#8217;intérieur du plugin.</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p1181code19'); return false;">View Code</a> CONSOLE</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p118119"><td class="code" id="p1181code19"><pre class="console" style="font-family:monospace;">mkdir -p plugins/myCrudThemePlugin/data/generator/sfDoctrineModule/myCrudTheme</pre></td></tr></table></div>

<p>Et on y copie tous les fichiers du thème par défaut.</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p1181code20'); return false;">View Code</a> CONSOLE</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p118120"><td class="code" id="p1181code20"><pre class="console" style="font-family:monospace;">cp -r lib/vendor/symfony/lib/plugins/sfDoctrinePlugin/data/generator/sfDoctrineModule/default/* plugins/myCrudThemePlugin/data/generator/sfDoctrineModule/myCrudTheme</pre></td></tr></table></div>

<p>Si vous avez fait un checkout de symfony, les fichiers que vous avez copiés contiennent les informations de svn. Il faut nettoyer tout ça. Je vous propose une petite ligne de commande qui permet de faire ca.</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p1181code21'); return false;">View Code</a> CONSOLE</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p118121"><td class="code" id="p1181code21"><pre class="console" style="font-family:monospace;">find plugins/myCrudThemePlugin/data/generator/sfDoctrineModule/myCrudTheme -name .svn -exec rm -rf {} \;</pre></td></tr></table></div>

<p>Les fichiers sont prêts. A ce stade, on pourrait déjà générer un thème en utilisant notre thème. Il sufirait d&#8217;activer le plugin dans</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p1181code22'); return false;">View Code</a> PATH</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p118122"><td class="code" id="p1181code22"><pre class="path" style="font-family:monospace;">/config/ProjectConfiguration.class.php</pre></td></tr></table></div>

</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p1181code23'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p118123"><td class="code" id="p1181code23"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> ProjectConfiguration <span style="color: #000000; font-weight: bold;">extends</span> sfProjectConfiguration
<span style="color: #009900;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> setup<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
  <span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">enablePlugins</span><span style="color: #009900;">&#40;</span><a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span>
      <span style="color: #339933;">...</span>
      <span style="color: #0000ff;">'myCrudThemePlugin'</span><span style="color: #339933;">,</span>
    <span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>Et de lancer la commande :</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p1181code24'); return false;">View Code</a> CONSOLE</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p118124"><td class="code" id="p1181code24"><pre class="console" style="font-family:monospace;">./symfony doctrine:generate-module --theme=&quot;myCrudTheme&quot; frontend job JobeetJob</pre></td></tr></table></div>

<p>Mais ne le faites pas ! :p Ca n&#8217;aurait pas un grand intérêt étant donné que l&#8217;on a rien changé par rapport au thème par défaut.</p>
<p>Nous allons commencer par rajouter un pager sur la page de listing. Il faut modifier l&#8217;action dans</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p1181code25'); return false;">View Code</a> PATH</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p118125"><td class="code" id="p1181code25"><pre class="path" style="font-family:monospace;">/plugins/myCrudThemePlugin/data/generator/sfDoctrineModule/myCrudTheme/parts/indexAction.php</pre></td></tr></table></div>

</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p1181code26'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p118126"><td class="code" id="p1181code26"><pre class="php" style="font-family:monospace;">&nbsp;
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> executeIndex<span style="color: #009900;">&#40;</span>sfWebRequest <span style="color: #000088;">$request</span><span style="color: #009900;">&#41;</span>
  <span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$query</span> <span style="color: #339933;">=</span> Doctrine_Core<span style="color: #339933;">::</span><span style="color: #004000;">getTable</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'&lt;?php echo $this-&gt;getModelClass() ?&gt;'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">createQuery</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'&lt;?php echo strtolower(substr($this-&gt;getModelClass(), 0, 1)) ?&gt;'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">pager</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> sfDoctrinePager<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'&lt;?php echo $this-&gt;getModelClass() ?&gt;'</span><span style="color: #339933;">,</span> sfConfig<span style="color: #339933;">::</span><span style="color: #004000;">get</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'app_myCrudThemePlugin_pagination'</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">20</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">pager</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setQuery</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$query</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">pager</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setPage</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getRequestParameter</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'page'</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">pager</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">init</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>Pour l&#8217;affichage du pager, je vous propose de créer un partial que l&#8217;on va inclure dans un module du plugin. Etant donné que le fonctionnement est générique, on a pas besoin qu&#8217;il soit recopié systèmatiquement dans les modules générés.</p>
<p>On va générer un module appelé shared qui va accueillir le partial du pager et éventuellement d&#8217;autres éléments que l&#8217;on voudra mutualiser.<br />
La encore sfTaskExtraPlugin nous aide bien car il intégre une commande qui fais ca.</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p1181code27'); return false;">View Code</a> CONSOLE</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p118127"><td class="code" id="p1181code27"><pre class="console" style="font-family:monospace;">./symfony generate:plugin-module myCrudThemePlugin shared</pre></td></tr></table></div>

<p>Il nous reste à aller créer notre partial dans</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p1181code28'); return false;">View Code</a> PATH</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p118128"><td class="code" id="p1181code28"><pre class="path" style="font-family:monospace;">/plugins/myCrudThemePlugin/modules/shared/templates/_pager.php</pre></td></tr></table></div>

</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p1181code29'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p118129"><td class="code" id="p1181code29"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$pager</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">haveToPaginate</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">:</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>
  <span style="color: #339933;">&lt;</span>div <span style="color: #000000; font-weight: bold;">class</span><span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;pagination&quot;</span><span style="color: #339933;">&gt;</span>
    <span style="color: #339933;">&lt;</span>a title<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;First Page&quot;</span> href<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;&lt;?php echo (isset(<span style="color: #006699; font-weight: bold;">$object</span>)) ? url_for(<span style="color: #006699; font-weight: bold;">$route</span>, <span style="color: #006699; font-weight: bold;">$object</span>) : url_for(<span style="color: #006699; font-weight: bold;">$route</span>) ?&gt;?page=&lt;?php echo <span style="color: #006699; font-weight: bold;">$pager-&gt;getFirstPage</span>() ?&gt;&quot;</span><span style="color: #339933;">&gt;</span>« First<span style="color: #339933;">&lt;/</span>a<span style="color: #339933;">&gt;</span>
    <span style="color: #339933;">&lt;</span>a title<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;Previous Page&quot;</span> href<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;&lt;?php echo (isset(<span style="color: #006699; font-weight: bold;">$object</span>)) ? url_for(<span style="color: #006699; font-weight: bold;">$route</span>, <span style="color: #006699; font-weight: bold;">$object</span>) : url_for(<span style="color: #006699; font-weight: bold;">$route</span>) ?&gt;?page=&lt;?php echo <span style="color: #006699; font-weight: bold;">$pager-&gt;getPreviousPage</span>() ?&gt;&quot;</span><span style="color: #339933;">&gt;</span>« Previous<span style="color: #339933;">&lt;/</span>a<span style="color: #339933;">&gt;</span>
<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">foreach</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$pager</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getLinks</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$page</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">:</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>
    <span style="color: #339933;">&lt;</span>a title<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;&lt;?php echo <span style="color: #006699; font-weight: bold;">$page</span> ?&gt;&quot;</span> <span style="color: #000000; font-weight: bold;">class</span><span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;number&lt;?php echo (<span style="color: #006699; font-weight: bold;">$page</span> == <span style="color: #006699; font-weight: bold;">$pager-&gt;getPage</span>()) ? ' current' : '' ?&gt;&quot;</span> href<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;&lt;?php echo (isset(<span style="color: #006699; font-weight: bold;">$object</span>)) ? url_for(<span style="color: #006699; font-weight: bold;">$route</span>, <span style="color: #006699; font-weight: bold;">$object</span>) : url_for(<span style="color: #006699; font-weight: bold;">$route</span>) ?&gt;?page=&lt;?php echo <span style="color: #006699; font-weight: bold;">$page</span> ?&gt;&quot;</span><span style="color: #339933;">&gt;</span><span style="color: #000000; font-weight: bold;">&lt;?</span>php <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$page</span> <span style="color: #000000; font-weight: bold;">?&gt;</span><span style="color: #339933;">&lt;/</span>a<span style="color: #339933;">&gt;</span>
<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">endforeach</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>
    <span style="color: #339933;">&lt;</span>a title<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;Next Page&quot;</span> href<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;&lt;?php echo (isset(<span style="color: #006699; font-weight: bold;">$object</span>)) ? url_for(<span style="color: #006699; font-weight: bold;">$route</span>, <span style="color: #006699; font-weight: bold;">$object</span>) : url_for(<span style="color: #006699; font-weight: bold;">$route</span>) ?&gt;?page=&lt;?php echo <span style="color: #006699; font-weight: bold;">$pager-&gt;getNextPage</span>() ?&gt;&quot;</span><span style="color: #339933;">&gt;</span>Next »<span style="color: #339933;">&lt;/</span>a<span style="color: #339933;">&gt;</span>
    <span style="color: #339933;">&lt;</span>a title<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;Last Page&quot;</span> href<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;&lt;?php echo (isset(<span style="color: #006699; font-weight: bold;">$object</span>)) ? url_for(<span style="color: #006699; font-weight: bold;">$route</span>, <span style="color: #006699; font-weight: bold;">$object</span>) : url_for(<span style="color: #006699; font-weight: bold;">$route</span>) ?&gt;?page=&lt;?php echo <span style="color: #006699; font-weight: bold;">$pager-&gt;getLastPage</span>() ?&gt;&quot;</span><span style="color: #339933;">&gt;</span>Last »<span style="color: #339933;">&lt;/</span>a<span style="color: #339933;">&gt;</span>
  <span style="color: #339933;">&lt;/</span>div<span style="color: #339933;">&gt;</span>
  <span style="color: #339933;">&lt;</span>div <span style="color: #000000; font-weight: bold;">class</span><span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;clear&quot;</span><span style="color: #339933;">&gt;&lt;/</span>div<span style="color: #339933;">&gt;</span>
<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">endif</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>Un pager générique qui fonctionne pour les <i>sfDoctrineRoute</i> et les <i>sfRequestRoute</i>. Il prends deux paramètres, route (obligatoire) et object (facultatif).</p>
<p>On modifie la vue indexSuccess.php pour la faire fonctionner avec le pager et ajouter le partial du pager.
<p> Dans</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p1181code30'); return false;">View Code</a> PATH</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p118130"><td class="code" id="p1181code30"><pre class="path" style="font-family:monospace;">/plugins/myCrudThemePlugin/data/generator/sfDoctrineModule/myCrudTheme/template/templates/indexSuccess.php</pre></td></tr></table></div>

</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p1181code31'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p118131"><td class="code" id="p1181code31"><pre class="php" style="font-family:monospace;"><span style="color: #339933;">&lt;</span>h1<span style="color: #339933;">&gt;</span><span style="color: #000000; font-weight: bold;">&lt;?</span>php <span style="color: #b1b100;">echo</span> sfInflector<span style="color: #339933;">::</span><span style="color: #004000;">humanize</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getPluralName</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span> List<span style="color: #339933;">&lt;/</span>h1<span style="color: #339933;">&gt;</span>
&nbsp;
<span style="color: #339933;">&lt;</span>table<span style="color: #339933;">&gt;</span>
  <span style="color: #339933;">&lt;</span>thead<span style="color: #339933;">&gt;</span>
    <span style="color: #339933;">&lt;</span>tr<span style="color: #339933;">&gt;</span>
<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">foreach</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getColumns</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$column</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">:</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>
      <span style="color: #339933;">&lt;</span>th<span style="color: #339933;">&gt;</span><span style="color: #000000; font-weight: bold;">&lt;?</span>php <span style="color: #b1b100;">echo</span> sfInflector<span style="color: #339933;">::</span><span style="color: #004000;">humanize</span><span style="color: #009900;">&#40;</span>sfInflector<span style="color: #339933;">::</span><span style="color: #004000;">underscore</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$column</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getPhpName</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span><span style="color: #339933;">&lt;/</span>th<span style="color: #339933;">&gt;</span>
<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">endforeach</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>
    <span style="color: #339933;">&lt;/</span>tr<span style="color: #339933;">&gt;</span>
  <span style="color: #339933;">&lt;/</span>thead<span style="color: #339933;">&gt;</span>
  <span style="color: #339933;">&lt;</span>tbody<span style="color: #339933;">&gt;</span>
    <span style="color: #009900;">&#91;</span>?php <span style="color: #b1b100;">foreach</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$pager</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getResults</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #b1b100;">as</span> $<span style="color: #000000; font-weight: bold;">&lt;?</span>php <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getSingularName</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">:</span> ?<span style="color: #009900;">&#93;</span>
    <span style="color: #339933;">&lt;</span>tr<span style="color: #339933;">&gt;</span>
<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">foreach</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getColumns</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$column</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">:</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>
<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$column</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">isPrimaryKey</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">:</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>
<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><a href="http://www.php.net/isset"><span style="color: #990000;">isset</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">params</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'route_prefix'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">params</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'route_prefix'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">:</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>
&nbsp;
      <span style="color: #339933;">&lt;</span>td<span style="color: #339933;">&gt;&lt;</span>a href<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;[?php echo url_for('&lt;?php echo <span style="color: #006699; font-weight: bold;">$this-&gt;getUrlForAction</span>(isset(<span style="color: #006699; font-weight: bold;">$this-&gt;params</span>['with_show']) &amp;&amp; <span style="color: #006699; font-weight: bold;">$this-&gt;params</span>['with_show'] ? 'show' : 'edit') ?&gt;', $&lt;?php echo <span style="color: #006699; font-weight: bold;">$this-&gt;getSingularName</span>() ?&gt;) ?]&quot;</span><span style="color: #339933;">&gt;</span><span style="color: #009900;">&#91;</span>?php <span style="color: #b1b100;">echo</span> $<span style="color: #000000; font-weight: bold;">&lt;?</span>php <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getSingularName</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">get</span><span style="color: #000000; font-weight: bold;">&lt;?</span>php <span style="color: #b1b100;">echo</span> sfInflector<span style="color: #339933;">::</span><span style="color: #004000;">camelize</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$column</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getPhpName</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> ?<span style="color: #009900;">&#93;</span><span style="color: #339933;">&lt;/</span>a<span style="color: #339933;">&gt;&lt;/</span>td<span style="color: #339933;">&gt;</span>
<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">else</span><span style="color: #339933;">:</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>
      <span style="color: #339933;">&lt;</span>td<span style="color: #339933;">&gt;&lt;</span>a href<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;[?php echo url_for('&lt;?php echo <span style="color: #006699; font-weight: bold;">$this-&gt;getModuleName</span>() ?&gt;/&lt;?php echo isset(<span style="color: #006699; font-weight: bold;">$this-&gt;params</span>['with_show']) &amp;&amp; <span style="color: #006699; font-weight: bold;">$this-&gt;params</span>['with_show'] ? 'show' : 'edit' ?&gt;?&lt;?php echo <span style="color: #006699; font-weight: bold;">$this-&gt;getPrimaryKeyUrlParams</span>() ?&gt;) ?]&quot;</span><span style="color: #339933;">&gt;</span><span style="color: #009900;">&#91;</span>?php <span style="color: #b1b100;">echo</span> $<span style="color: #000000; font-weight: bold;">&lt;?</span>php <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getSingularName</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">get</span><span style="color: #000000; font-weight: bold;">&lt;?</span>php <span style="color: #b1b100;">echo</span> sfInflector<span style="color: #339933;">::</span><span style="color: #004000;">camelize</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$column</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getPhpName</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> ?<span style="color: #009900;">&#93;</span><span style="color: #339933;">&lt;/</span>a<span style="color: #339933;">&gt;&lt;/</span>td<span style="color: #339933;">&gt;</span>
<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">endif</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>
<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">else</span><span style="color: #339933;">:</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>
      <span style="color: #339933;">&lt;</span>td<span style="color: #339933;">&gt;</span><span style="color: #009900;">&#91;</span>?php <span style="color: #b1b100;">echo</span> $<span style="color: #000000; font-weight: bold;">&lt;?</span>php <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getSingularName</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">get</span><span style="color: #000000; font-weight: bold;">&lt;?</span>php <span style="color: #b1b100;">echo</span> sfInflector<span style="color: #339933;">::</span><span style="color: #004000;">camelize</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$column</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getPhpName</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> ?<span style="color: #009900;">&#93;</span><span style="color: #339933;">&lt;/</span>td<span style="color: #339933;">&gt;</span>
<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">endif</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>
<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">endforeach</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>
    <span style="color: #339933;">&lt;/</span>tr<span style="color: #339933;">&gt;</span>
    <span style="color: #009900;">&#91;</span>?php <span style="color: #b1b100;">endforeach</span><span style="color: #339933;">;</span> ?<span style="color: #009900;">&#93;</span>
  <span style="color: #339933;">&lt;/</span>tbody<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;/</span>table<span style="color: #339933;">&gt;</span>
&nbsp;
<span style="color: #009900;">&#91;</span>?php include_partial<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'shared/pager'</span><span style="color: #339933;">,</span> <a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'pager'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$pager</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'route'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'&lt;?php echo (isset($this-&gt;params['</span>route_prefix<span style="color: #0000ff;">']) &amp;&amp; $this-&gt;params['</span>route_prefix<span style="color: #0000ff;">']) ? $this-&gt;getUrlForAction('</span>index<span style="color: #0000ff;">') : $this-&gt;getModuleName().'</span><span style="color: #339933;">/</span>index<span style="color: #0000ff;">' ?&gt;'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> ?<span style="color: #009900;">&#93;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><a href="http://www.php.net/isset"><span style="color: #990000;">isset</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">params</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'route_prefix'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">params</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'route_prefix'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">:</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>
  <span style="color: #339933;">&lt;</span>a href<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;[?php echo url_for('&lt;?php echo <span style="color: #006699; font-weight: bold;">$this-&gt;getUrlForAction</span>('new') ?&gt;') ?]&quot;</span><span style="color: #339933;">&gt;</span>New<span style="color: #339933;">&lt;/</span>a<span style="color: #339933;">&gt;</span>
<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">else</span><span style="color: #339933;">:</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>
  <span style="color: #339933;">&lt;</span>a href<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;[?php echo url_for('&lt;?php echo <span style="color: #006699; font-weight: bold;">$this-&gt;getModuleName</span>() ?&gt;/new') ?]&quot;</span><span style="color: #339933;">&gt;</span>New<span style="color: #339933;">&lt;/</span>a<span style="color: #339933;">&gt;</span>
<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">endif</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>On peut d&#8217;ores et déjà générer le crud pour tester,</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p1181code32'); return false;">View Code</a> CONSOLE</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p118132"><td class="code" id="p1181code32"><pre class="console" style="font-family:monospace;">./symfony doctrine:generate-module --theme=&quot;myCrudTheme&quot; frontend job JobeetJob</pre></td></tr></table></div>

<p>Et vous voilà libéré de la tâche rébarbative des pagers ! :p</p>
<p>Ca faisait un petit moment que je voulais traiter ce sujet sur le blog, et je comptais continuer en intégrant les formFilter. Malheureusement, je manque de temps pour aller plus loin.<br />
Mais vous avez désormais en votre possession les éléments pour pouvoir faire votre propre thème et l&#8217;enrichir à votre guise.</p>
<p>J&#8217;espére que ca vous aura été utile et si j&#8217;en ai le temps je ferais un prochain post pour voir cette intégration des formFilter <img src='http://www.lexik.fr/blog/symfony/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://www.lexik.fr/blog/symfony/symfony/qui-leu-crud-ou-comment-creer-un-theme-pour-les-crud-doctrine-de-symfony-1181/feed</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
	</channel>
</rss>

