<?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 Symfony - Lexik Montpellier</title>
	<atom:link href="http://www.lexik.fr/blog/symfony/feed" rel="self" type="application/rss+xml" />
	<link>http://www.lexik.fr/blog/symfony</link>
	<description>Blog sur le développement Web PHP</description>
	<lastBuildDate>Mon, 09 Aug 2010 11:33:56 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</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[Symfony]]></category>
		<category><![CDATA[doctrine]]></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 d&#8217;avoir un module complet et fonctionnel avec de nombreuses fonctionnalités, le tout relativement configurable. En [...]]]></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 width="100%" ><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 width="100%" ><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 width="100%" ><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 width="100%" ><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 width="100%" ><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 width="100%" ><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 width="100%" ><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><span style="color: #990000;">array</span><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 width="100%" ><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 width="100%" ><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 width="100%" ><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 width="100%" ><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 width="100%" ><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 width="100%" ><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: #990000;">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 width="100%" ><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 width="100%" ><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: #990000;">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: #990000;">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: #990000;">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><span style="color: #990000;">isset</span><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: #990000;">echo</span> $<span style="color: #000000; font-weight: bold;">&lt;?</span>php <span style="color: #990000;">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: #990000;">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: #990000;">echo</span> $<span style="color: #000000; font-weight: bold;">&lt;?</span>php <span style="color: #990000;">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: #990000;">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: #990000;">echo</span> $<span style="color: #000000; font-weight: bold;">&lt;?</span>php <span style="color: #990000;">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: #990000;">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> <span style="color: #990000;">array</span><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><span style="color: #990000;">isset</span><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 width="100%" ><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>
<a href="http://www.addtoany.com/add_to/twitter?linkurl=http%3A%2F%2Fwww.lexik.fr%2Fblog%2Fsymfony%2Fsymfony%2Fqui-leu-crud-ou-comment-creer-un-theme-pour-les-crud-doctrine-de-symfony-1181&amp;linkname=Qui%20l%26%238217%3Beu%20CRUD%20%3F%20Ou%20comment%20cr%C3%A9er%20un%20th%C3%A8me%20pour%20les%20CRUD%20Doctrine%20de%20Symfony." title="Twitter" rel="nofollow" target="_blank"><img src="http://www.lexik.fr/blog/symfony/wp-content/plugins/add-to-any/icons/twitter.png" alt="Twitter"/></a> <a href="http://www.addtoany.com/add_to/facebook?linkurl=http%3A%2F%2Fwww.lexik.fr%2Fblog%2Fsymfony%2Fsymfony%2Fqui-leu-crud-ou-comment-creer-un-theme-pour-les-crud-doctrine-de-symfony-1181&amp;linkname=Qui%20l%26%238217%3Beu%20CRUD%20%3F%20Ou%20comment%20cr%C3%A9er%20un%20th%C3%A8me%20pour%20les%20CRUD%20Doctrine%20de%20Symfony." title="Facebook" rel="nofollow" target="_blank"><img src="http://www.lexik.fr/blog/symfony/wp-content/plugins/add-to-any/icons/facebook.png" alt="Facebook"/></a> <a href="http://www.addtoany.com/add_to/delicious?linkurl=http%3A%2F%2Fwww.lexik.fr%2Fblog%2Fsymfony%2Fsymfony%2Fqui-leu-crud-ou-comment-creer-un-theme-pour-les-crud-doctrine-de-symfony-1181&amp;linkname=Qui%20l%26%238217%3Beu%20CRUD%20%3F%20Ou%20comment%20cr%C3%A9er%20un%20th%C3%A8me%20pour%20les%20CRUD%20Doctrine%20de%20Symfony." title="Delicious" rel="nofollow" target="_blank"><img src="http://www.lexik.fr/blog/symfony/wp-content/plugins/add-to-any/icons/delicious.png" alt="Delicious"/></a> <a href="http://www.addtoany.com/add_to/viadeo?linkurl=http%3A%2F%2Fwww.lexik.fr%2Fblog%2Fsymfony%2Fsymfony%2Fqui-leu-crud-ou-comment-creer-un-theme-pour-les-crud-doctrine-de-symfony-1181&amp;linkname=Qui%20l%26%238217%3Beu%20CRUD%20%3F%20Ou%20comment%20cr%C3%A9er%20un%20th%C3%A8me%20pour%20les%20CRUD%20Doctrine%20de%20Symfony." title="Viadeo" rel="nofollow" target="_blank"><img src="http://www.lexik.fr/blog/symfony/wp-content/plugins/add-to-any/icons/viadeo.png" alt="Viadeo"/></a> <a href="http://www.addtoany.com/add_to/technorati_favorites?linkurl=http%3A%2F%2Fwww.lexik.fr%2Fblog%2Fsymfony%2Fsymfony%2Fqui-leu-crud-ou-comment-creer-un-theme-pour-les-crud-doctrine-de-symfony-1181&amp;linkname=Qui%20l%26%238217%3Beu%20CRUD%20%3F%20Ou%20comment%20cr%C3%A9er%20un%20th%C3%A8me%20pour%20les%20CRUD%20Doctrine%20de%20Symfony." title="Technorati Favorites" rel="nofollow" target="_blank"><img src="http://www.lexik.fr/blog/symfony/wp-content/plugins/add-to-any/icons/technorati.png" alt="Technorati Favorites"/></a> <a href="http://www.addtoany.com/add_to/netvibes_share?linkurl=http%3A%2F%2Fwww.lexik.fr%2Fblog%2Fsymfony%2Fsymfony%2Fqui-leu-crud-ou-comment-creer-un-theme-pour-les-crud-doctrine-de-symfony-1181&amp;linkname=Qui%20l%26%238217%3Beu%20CRUD%20%3F%20Ou%20comment%20cr%C3%A9er%20un%20th%C3%A8me%20pour%20les%20CRUD%20Doctrine%20de%20Symfony." title="Netvibes Share" rel="nofollow" target="_blank"><img src="http://www.lexik.fr/blog/symfony/wp-content/plugins/add-to-any/icons/netvibes.png" alt="Netvibes Share"/></a> <a href="http://www.addtoany.com/add_to/gmail?linkurl=http%3A%2F%2Fwww.lexik.fr%2Fblog%2Fsymfony%2Fsymfony%2Fqui-leu-crud-ou-comment-creer-un-theme-pour-les-crud-doctrine-de-symfony-1181&amp;linkname=Qui%20l%26%238217%3Beu%20CRUD%20%3F%20Ou%20comment%20cr%C3%A9er%20un%20th%C3%A8me%20pour%20les%20CRUD%20Doctrine%20de%20Symfony." title="Gmail" rel="nofollow" target="_blank"><img src="http://www.lexik.fr/blog/symfony/wp-content/plugins/add-to-any/icons/gmail.png" alt="Gmail"/></a> <a href="http://www.addtoany.com/add_to/yoolink?linkurl=http%3A%2F%2Fwww.lexik.fr%2Fblog%2Fsymfony%2Fsymfony%2Fqui-leu-crud-ou-comment-creer-un-theme-pour-les-crud-doctrine-de-symfony-1181&amp;linkname=Qui%20l%26%238217%3Beu%20CRUD%20%3F%20Ou%20comment%20cr%C3%A9er%20un%20th%C3%A8me%20pour%20les%20CRUD%20Doctrine%20de%20Symfony." title="Yoolink" rel="nofollow" target="_blank"><img src="http://www.lexik.fr/blog/symfony/wp-content/plugins/add-to-any/icons/yoolink.png" alt="Yoolink"/></a> <a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save?linkurl=http%3A%2F%2Fwww.lexik.fr%2Fblog%2Fsymfony%2Fsymfony%2Fqui-leu-crud-ou-comment-creer-un-theme-pour-les-crud-doctrine-de-symfony-1181&amp;linkname=Qui%20l%26%238217%3Beu%20CRUD%20%3F%20Ou%20comment%20cr%C3%A9er%20un%20th%C3%A8me%20pour%20les%20CRUD%20Doctrine%20de%20Symfony.">Partager cet article</a>]]></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>1</slash:comments>
		</item>
		<item>
		<title>Force-download avec Symfony</title>
		<link>http://www.lexik.fr/blog/symfony/symfony/force-download-avec-symfony-1237</link>
		<comments>http://www.lexik.fr/blog/symfony/symfony/force-download-avec-symfony-1237#comments</comments>
		<pubDate>Tue, 20 Jul 2010 08:35:36 +0000</pubDate>
		<dc:creator>tsyr</dc:creator>
				<category><![CDATA[1.4.x]]></category>
		<category><![CDATA[Symfony]]></category>
		<category><![CDATA[doctrine]]></category>
		<category><![CDATA[download]]></category>
		<category><![CDATA[force-download]]></category>
		<category><![CDATA[session]]></category>
		<category><![CDATA[téléchargement]]></category>

		<guid isPermaLink="false">http://www.lexik.fr/blog/symfony/?p=1237</guid>
		<description><![CDATA[Aujourd&#8217;hui, nous allons aborder quelque chose de simple et répandu sur la plupart des sites Internet de nos jours : le téléchargement de fichiers. Bien sûr, il ne s&#8217;agit pas de permettre aux utilisateurs de télécharger votre dernier rush de photos nocturnes sous forme d&#8217;archive zip, ou encore les rapports de la dernière assemblée générale [...]]]></description>
			<content:encoded><![CDATA[<p>Aujourd&#8217;hui, nous allons aborder quelque chose de simple et répandu sur la plupart des sites Internet de nos jours : le téléchargement de fichiers.</p>
<p>Bien sûr, il ne s&#8217;agit pas de permettre aux utilisateurs de télécharger votre dernier rush de photos nocturnes sous forme d&#8217;archive zip, ou encore les rapports de la dernière assemblée générale de votre association en PDF; car ceci ne nécessite en rien l&#8217;intervention de symfony.</p>
<p>Par contre, dès qu&#8217;une action doit être entreprise pour vérifier l&#8217;authenticité de l&#8217;utilisateur, ou ne serait-ce qu&#8217;une table de log pour savoir qui a téléchargé quel fichier, on va avoir besoin de symfony (à moins d&#8217;avoir envie de réinventer la roue).<br />
<span id="more-1237"></span><br />
Pour commencer, une action simple, qui affiche selon votre convenance une liste de fichiers ou le détail d&#8217;un fichier en particulier, mais surtout, un lien de téléchargement. Pour mon exemple, j&#8217;ai déjà fais l&#8217;installation du projet, et du plugin sfDoctrineGuardPlugin (avec les fixtures par défaut du plugin).</p>
<p>Mon schéma de base de données ressemble à ça:</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('p1237code44'); return false;">View Code</a> YML</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table width="100%" ><tr id="p123744"><td class="code" id="p1237code44"><pre class="yml" style="font-family:monospace;">Software:
  columns:
    name: string
    path: string
    size: integer
&nbsp;
Download:
  options:
    symfony:          { form: false, filter: false }
  actAs:
    Timestampable:
      updated:        { disabled: true }
  columns:
    user_id:          { type: integer(4), notnull: true }
    software_id:      { type: integer(4), notnull: true }
  relations:
    Software:
      local:          software_id
      foreign:        id
      foreignAlias:   Downloads
    User:
      class:          sfGuardUser
      local:          user_id
      foreign:        id
      foreignAlias:   Downloads</pre></td></tr></table></div>

<p>Mes fixtures, à ça:</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('p1237code45'); return false;">View Code</a> YML</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table width="100%" ><tr id="p123745"><td class="code" id="p1237code45"><pre class="yml" style="font-family:monospace;">Software:
  test:
    name:     symfony latest
    path:     &lt;?php echo sfConfig::get('sf_data_dir') ?&gt;/symfony-1.4.6.tgz
    size:     3233168</pre></td></tr></table></div>

<p>Une fois ceci chargé en base de données, on va pouvoir se lancer dans le développement, et plus particulièrement le téléchargement. Pour me simplifier la tâche lors de cet exemple, un unique fichier pourra être télécharger, ainsi, la route pour lancer le &laquo;&nbsp;download&nbsp;&raquo; du fichier est aussi simple que:</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('p1237code46'); return false;">View Code</a> YML</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table width="100%" ><tr id="p123746"><td class="code" id="p1237code46"><pre class="yml" style="font-family:monospace;">download:
  class:      sfDoctrineRoute
  url:        /download/:id
  options:
    model:    Software
    type:     object
  param:
    module:   file
    action:   download</pre></td></tr></table></div>

<p>De ce fait, pour télécharger notre fichier d&#8217;exemple, il suffit sur n&#8217;importe quelle page, d&#8217;inclure un lien vers celui-ci, de cette façon:</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('p1237code47'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table width="100%" ><tr id="p123747"><td class="code" id="p1237code47"><pre class="php" style="font-family:monospace;"><span style="color: #339933;">&lt;</span>p<span style="color: #339933;">&gt;</span><span style="color: #000000; font-weight: bold;">&lt;?</span>php <span style="color: #990000;">echo</span> link_to<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Télécharger'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'@download'</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span><span style="color: #339933;">&lt;/</span>p<span style="color: #339933;">&gt;</span></pre></td></tr></table></div>

<p>Tout le code sera centralisé dans l&#8217;action, mais je le répète, ce n&#8217;est qu&#8217;à titre d&#8217;exemple, il serait bien plus approprié de faire un lien de ce type:</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('p1237code48'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table width="100%" ><tr id="p123748"><td class="code" id="p1237code48"><pre class="php" style="font-family:monospace;"><span style="color: #339933;">&lt;</span>p<span style="color: #339933;">&gt;</span><span style="color: #000000; font-weight: bold;">&lt;?</span>php <span style="color: #990000;">echo</span> link_to<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Télécharger '</span><span style="color: #339933;">.</span><span style="color: #000088;">$file</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'download'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$file</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span><span style="color: #339933;">&lt;/</span>p<span style="color: #339933;">&gt;</span></pre></td></tr></table></div>

<p>Regardons maintenant plus en détail le contenu de notre action «download» du module «file» (cf. la route ci-dessus)</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('p1237code49'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table width="100%" ><tr id="p123749"><td class="code" id="p1237code49"><pre class="php" style="font-family:monospace;">  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> executeDownload<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;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">forward404Unless</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getUser</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">isAuthenticated</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000088;">$file</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getRoute</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getObject</span><span style="color: #009900;">&#40;</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;">forward404Unless</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">file_exists</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$file</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getPath</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: #0000ff;">'Fichier introuvable'</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;">getResponse</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">clearHttpHeaders</span><span style="color: #009900;">&#40;</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;">getResponse</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setContentType</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'application/force-download'</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;">getResponse</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setHttpHeader</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Content-Disposition'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'attachment; filename=&quot;'</span> <span style="color: #339933;">.</span> <span style="color: #990000;">basename</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$file</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getPath</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: #0000ff;">'&quot;'</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;">getResponse</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setHttpHeader</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Content-Transfer-Encoding'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'binary'</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;">getResponse</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setHttpHeader</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Content-Length'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$file</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getSize</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: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getResponse</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setHttpHeader</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Connection'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'close'</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;">getResponse</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setContent</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">file_get_contents</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$file</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getPath</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: #339933;">;</span>
    <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getResponse</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">send</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>A ce niveau, deux problèmes majeurs se posent:</p>
<ul>
<li>file_get_contents est dépendant du paramètre memory_limit de php</li>
<li>la web_debug_toolbar va venir s&#8217;immiscer dans chaque download</li>
</ul>
<p>Le problème de la web_debug_toolbar peut vite être résolu en la désactivant dans l&#8217;action avec sfConfig::set(&#8216;sf_web_debug_false&#8217;, false) mais il reste le problème du téléchargement de fichiers dont la taille dépasse notre memory_limit. Ce paramètre étant généralement bas en production (128 Mo par défaut), ça peut vite être génant. La solution est d&#8217;utiliser la fonction php <a href="http://fr.php.net/manual/fr/function.readfile.php">readfile()</a> qui elle, envoit directement des blocs de 8 Ko sur la sortie, et donc n&#8217;est pas soumis à la limite d&#8217;utilisation de mémoire PHP.</p>
<p>On remplace donc:</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('p1237code50'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table width="100%" ><tr id="p123750"><td class="code" id="p1237code50"><pre class="php" style="font-family:monospace;">    <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getResponse</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setContent</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">file_get_contents</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$file</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getPath</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: #339933;">;</span>
    <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getResponse</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">send</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>par</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('p1237code51'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table width="100%" ><tr id="p123751"><td class="code" id="p1237code51"><pre class="php" style="font-family:monospace;">    <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getResponse</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">sendHttpHeaders</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #339933;">@</span><span style="color: #990000;">readfile</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$file</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getPath</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    throw <span style="color: #000000; font-weight: bold;">new</span> sfStopException<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>L&#8217;exception sfStopException() est une manière un peu «crade» de mettre un terme à l&#8217;exécution du script, mais c&#8217;est moins pire qu&#8217;un &laquo;&nbsp;die&nbsp;&raquo; et du coup, ça permet dans un deuxième temps de ne pas être embêté par la web_debug_toolbar.<br />
Il arrive souvent aussi que les headers ne soient pas envoyés, c&#8217;est dû au fait que la sortie est bufferisée, ce problème se résoud avec l&#8217;utilisation des fonctions <a href="http://fr.php.net/manual/fr/function.flush.php">flush()</a> et/ou <a href="http://fr.php.net/manual/fr/function.ob-end-clean.php">ob_end_clean()</a> juste avant le readfile.</p>
<p>Maintenant, on avait pour but de passer par symfony pour enregistrer des logs sur les téléchargements. Rien de plus simple, un exemple pourrait être le code suivant à rajouter après la vérification de l&#8217;existence du fichier (file_exists()) :</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('p1237code52'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table width="100%" ><tr id="p123752"><td class="code" id="p1237code52"><pre class="php" style="font-family:monospace;">    <span style="color: #000088;">$dl</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Download<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$dl</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setSoftware</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$file</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$dl</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setUser</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getUser</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getGuardUser</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: #666666; font-style: italic;">// other stuff</span>
    <span style="color: #000088;">$dl</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">save</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>A ce stade, tout pourrait sembler correct. C&#8217;était sans compter une fourberie de PHP, qui va bloquer toute autre action sur votre site pendant un téléchargement. En effet, les sessions fonctionnent avec un &laquo;&nbsp;LOCK&nbsp;&raquo; et vu que les sessions sont en auto_start avec symfony, le téléchargement se fait pendant une session, et aucune autre action de la part de l&#8217;utilisateur ne sera accepté pendant un téléchargement, car sa session étant identifiée comme &laquo;&nbsp;en cours d&#8217;écriture&nbsp;&raquo; (le principe de lock).<br />
Une personne ayant déjà rencontré ce problème pourrait se dire qu&#8217;en fermant la session à l&#8217;aide de</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('p1237code53'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table width="100%" ><tr id="p123753"><td class="code" id="p1237code53"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getUser</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">shutdown</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>dans l&#8217;action résoudrait le problème, et ce n&#8217;est pas le cas, même si en effet, la fonction shutdown() va libérer la session grâce à la fonction <a href="http://fr.php.net/manual/fr/function.session-write-close.php">session_write_close()</a>, elle ne sera exécuté qu&#8217;à la fin du script, donc dans notre cas, ça n&#8217;a aucun intêret, il faut faire le session_write_close() directement dans notre action.</p>
<p>Pour finir, voici le code complet de notre action :</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('p1237code54'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table width="100%" ><tr id="p123754"><td class="code" id="p1237code54"><pre class="php" style="font-family:monospace;">  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> executeDownload<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;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">forward404Unless</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getUser</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">isAuthenticated</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000088;">$file</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getRoute</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getObject</span><span style="color: #009900;">&#40;</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;">forward404Unless</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">file_exists</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$file</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getPath</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: #0000ff;">'Fichier introuvable'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000088;">$dl</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Download<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$dl</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setSoftware</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$file</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$dl</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setUser</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getUser</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getGuardUser</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: #666666; font-style: italic;">// other stuff</span>
    <span style="color: #000088;">$dl</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">save</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #990000;">session_write_close</span><span style="color: #009900;">&#40;</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;">getResponse</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">clearHttpHeaders</span><span style="color: #009900;">&#40;</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;">getResponse</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setContentType</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'application/force-download'</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;">getResponse</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setHttpHeader</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Content-Disposition'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'attachment; filename=&quot;'</span> <span style="color: #339933;">.</span> <span style="color: #990000;">basename</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$file</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getPath</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: #0000ff;">'&quot;'</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;">getResponse</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setHttpHeader</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Content-Transfer-Encoding'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'binary'</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;">getResponse</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setHttpHeader</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Content-Length'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$file</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getSize</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: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getResponse</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setHttpHeader</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Connection'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'close'</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;">getResponse</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">sendHttpHeaders</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">//    @ob_end_clean();</span>
<span style="color: #666666; font-style: italic;">//    flush();</span>
&nbsp;
    <span style="color: #339933;">@</span><span style="color: #990000;">readfile</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$file</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getPath</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    throw <span style="color: #000000; font-weight: bold;">new</span> sfStopException<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>J&#8217;espère que ceci vous aidera dans vos développements actuels et futurs,<br />
@ bientôt.</p>
<a href="http://www.addtoany.com/add_to/twitter?linkurl=http%3A%2F%2Fwww.lexik.fr%2Fblog%2Fsymfony%2Fsymfony%2Fforce-download-avec-symfony-1237&amp;linkname=Force-download%20avec%20Symfony" title="Twitter" rel="nofollow" target="_blank"><img src="http://www.lexik.fr/blog/symfony/wp-content/plugins/add-to-any/icons/twitter.png" alt="Twitter"/></a> <a href="http://www.addtoany.com/add_to/facebook?linkurl=http%3A%2F%2Fwww.lexik.fr%2Fblog%2Fsymfony%2Fsymfony%2Fforce-download-avec-symfony-1237&amp;linkname=Force-download%20avec%20Symfony" title="Facebook" rel="nofollow" target="_blank"><img src="http://www.lexik.fr/blog/symfony/wp-content/plugins/add-to-any/icons/facebook.png" alt="Facebook"/></a> <a href="http://www.addtoany.com/add_to/delicious?linkurl=http%3A%2F%2Fwww.lexik.fr%2Fblog%2Fsymfony%2Fsymfony%2Fforce-download-avec-symfony-1237&amp;linkname=Force-download%20avec%20Symfony" title="Delicious" rel="nofollow" target="_blank"><img src="http://www.lexik.fr/blog/symfony/wp-content/plugins/add-to-any/icons/delicious.png" alt="Delicious"/></a> <a href="http://www.addtoany.com/add_to/viadeo?linkurl=http%3A%2F%2Fwww.lexik.fr%2Fblog%2Fsymfony%2Fsymfony%2Fforce-download-avec-symfony-1237&amp;linkname=Force-download%20avec%20Symfony" title="Viadeo" rel="nofollow" target="_blank"><img src="http://www.lexik.fr/blog/symfony/wp-content/plugins/add-to-any/icons/viadeo.png" alt="Viadeo"/></a> <a href="http://www.addtoany.com/add_to/technorati_favorites?linkurl=http%3A%2F%2Fwww.lexik.fr%2Fblog%2Fsymfony%2Fsymfony%2Fforce-download-avec-symfony-1237&amp;linkname=Force-download%20avec%20Symfony" title="Technorati Favorites" rel="nofollow" target="_blank"><img src="http://www.lexik.fr/blog/symfony/wp-content/plugins/add-to-any/icons/technorati.png" alt="Technorati Favorites"/></a> <a href="http://www.addtoany.com/add_to/netvibes_share?linkurl=http%3A%2F%2Fwww.lexik.fr%2Fblog%2Fsymfony%2Fsymfony%2Fforce-download-avec-symfony-1237&amp;linkname=Force-download%20avec%20Symfony" title="Netvibes Share" rel="nofollow" target="_blank"><img src="http://www.lexik.fr/blog/symfony/wp-content/plugins/add-to-any/icons/netvibes.png" alt="Netvibes Share"/></a> <a href="http://www.addtoany.com/add_to/gmail?linkurl=http%3A%2F%2Fwww.lexik.fr%2Fblog%2Fsymfony%2Fsymfony%2Fforce-download-avec-symfony-1237&amp;linkname=Force-download%20avec%20Symfony" title="Gmail" rel="nofollow" target="_blank"><img src="http://www.lexik.fr/blog/symfony/wp-content/plugins/add-to-any/icons/gmail.png" alt="Gmail"/></a> <a href="http://www.addtoany.com/add_to/yoolink?linkurl=http%3A%2F%2Fwww.lexik.fr%2Fblog%2Fsymfony%2Fsymfony%2Fforce-download-avec-symfony-1237&amp;linkname=Force-download%20avec%20Symfony" title="Yoolink" rel="nofollow" target="_blank"><img src="http://www.lexik.fr/blog/symfony/wp-content/plugins/add-to-any/icons/yoolink.png" alt="Yoolink"/></a> <a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save?linkurl=http%3A%2F%2Fwww.lexik.fr%2Fblog%2Fsymfony%2Fsymfony%2Fforce-download-avec-symfony-1237&amp;linkname=Force-download%20avec%20Symfony">Partager cet article</a>]]></content:encoded>
			<wfw:commentRss>http://www.lexik.fr/blog/symfony/symfony/force-download-avec-symfony-1237/feed</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>Exemple d&#8217;application utilisant la Graph API de Facebook</title>
		<link>http://www.lexik.fr/blog/symfony/symfony/exemple-dapplication-utilisant-la-graph-api-de-facebook-1187</link>
		<comments>http://www.lexik.fr/blog/symfony/symfony/exemple-dapplication-utilisant-la-graph-api-de-facebook-1187#comments</comments>
		<pubDate>Wed, 07 Jul 2010 13:42:30 +0000</pubDate>
		<dc:creator>laurent</dc:creator>
				<category><![CDATA[1.4.x]]></category>
		<category><![CDATA[Symfony]]></category>
		<category><![CDATA[tips]]></category>

		<guid isPermaLink="false">http://www.lexik.fr/blog/symfony/?p=1187</guid>
		<description><![CDATA[Facebook a lancé il y a maintenant quelque smois sa nouvelle Graph API, mais la documentation qui l'entoure se fait encore rare et peu détaillée. Je vais donc profiter de cet article pour présenter quelques aspects de cette nouvelle API à l'aide d'une petite iframe application Facebook, et avec Symfony (pour rester dans la thématique du blog !).]]></description>
			<content:encoded><![CDATA[<p>Facebook a lancé il y a maintenant quelques mois sa nouvelle Graph API, qui permet d&#8217;accèder et d&#8217;interagir avec les informations Facebook de l&#8217;utilisateur encore plus simplement. Seuelement la documentation qui l&#8217;entoure se fait encore rare et peu détaillée. Je vais donc profiter de cet article pour présenter quelques aspects de cette nouvelle API à l&#8217;aide d&#8217;une petite iframe application Facebook, et avec Symfony (pour rester dans la thématique du blog !).</p>
<p><span id="more-1187"></span></p>
<p>Pour commencer nous avons bien sûr besoin de créer notre application Facebook. Cela n&#8217;étant pas le but de cet article, je vous laisse trouver la marche à suivre sur le net; les sites couvrant le sujet sont légion (par exemple <a href="http://www.creer-une-application-facebook.com" target="_blank">créer une application Facebook</a>).</p>
<p>On paramètre correctement l&#8217;application et celle-ci s&#8217;occupera ensuite de charger nos pages dynamiques depuis l&#8217;url spécifiée:</p>
<p><a href="http://www.lexik.fr/blog/symfony/wp-content/uploads/2010/07/canvas.png"><img class="alignnone size-medium wp-image-1188" title="canvas" src="http://www.lexik.fr/blog/symfony/wp-content/uploads/2010/07/canvas-300x232.png" alt="" width="300" height="232" /></a></p>
<p>L&#8217;application Symfony n&#8217;a ici qu&#8217;un seul module &laquo;&nbsp;demo&nbsp;&raquo; qui s&#8217;occupe des quelques actions nécessaires. Editons &laquo;&nbsp;app.yml&nbsp;&raquo; pour y ajouter l&#8217;ID et le SECRET obtenus lors de la création de notre application:</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('p1187code55'); return false;">View Code</a> YML</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table width="100%" ><tr id="p118755"><td class="code" id="p1187code55"><pre class="yml" style="font-family:monospace;">all:
  facebook:
    app_id: 116399618389033
    api_key: *******************************************
    secret: *****************************************
    base_url: http://facebook.prestataire-symfony.com
    app_url: http://apps.facebook.com/demolexik</pre></td></tr></table></div>

<p>Il nous faut ensuite récupérer <a href="http://github.com/facebook/php-sdk/downloads" target="_blank">l&#8217;api Facebook PHP sur Github</a>, que nous allons coller dans /lib/facebook/facebook.php.<br />
Afin de ne pas avoir à initialiser les appels à l&#8217;api depuis chaque action, un filter va grandement nous simplifier la tâche:</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('p1187code56'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table width="100%" ><tr id="p118756"><td class="code" id="p1187code56"><pre class="php" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">// lib/filter/FacebookFilter.class.php</span>
<span style="color: #000000; font-weight: bold;">class</span> FacebookFilter <span style="color: #000000; font-weight: bold;">extends</span> sfFilter
<span style="color: #009900;">&#123;</span>
&nbsp;
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> execute<span style="color: #009900;">&#40;</span>sfFilterChain <span style="color: #000088;">$filterChain</span><span style="color: #009900;">&#41;</span>
  <span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$context</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getContext</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$request</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$context</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getRequest</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #666666; font-style: italic;">//initialisation de l'appel à l'api...</span>
    <span style="color: #000088;">$facebook</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Facebook<span style="color: #009900;">&#40;</span><span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
          <span style="color: #0000ff;">'appId'</span>   <span style="color: #339933;">=&gt;</span> sfConfig<span style="color: #339933;">::</span><span style="color: #004000;">get</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'app_facebook_app_id'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
          <span style="color: #0000ff;">'secret'</span>  <span style="color: #339933;">=&gt;</span> sfConfig<span style="color: #339933;">::</span><span style="color: #004000;">get</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'app_facebook_secret'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
          <span style="color: #0000ff;">'cookie'</span>  <span style="color: #339933;">=&gt;</span> <span style="color: #000000; font-weight: bold;">true</span><span style="color: #339933;">,</span>
      <span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000088;">$context</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getController</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
            <span style="color: #339933;">-&gt;</span><span style="color: #004000;">getActionStack</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
            <span style="color: #339933;">-&gt;</span><span style="color: #004000;">getLastEntry</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
            <span style="color: #339933;">-&gt;</span><span style="color: #004000;">getActionInstance</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
            <span style="color: #339933;">-&gt;</span><span style="color: #004000;">facebook</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$facebook</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000088;">$filterChain</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">execute</span><span style="color: #009900;">&#40;</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>Pensez à éditer votre &laquo;&nbsp;filters.yml&nbsp;&raquo; en conséquence.</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('p1187code57'); return false;">View Code</a> YML</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table width="100%" ><tr id="p118757"><td class="code" id="p1187code57"><pre class="yml" style="font-family:monospace;">#apps/frontend/config/filters.yml
rendering: ~
security:  ~
# insert your own filters here
Facebook:
  class: FacebookFilter
cache:     ~
execution: ~</pre></td></tr></table></div>

<p>Pour l&#8217;api Javascript, nous allons éditer le layout de l&#8217;application:</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('p1187code58'); return false;">View Code</a> HTML</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table width="100%" ><tr id="p118758"><td class="code" id="p1187code58"><pre class="html" style="font-family:monospace;">&lt;html xmlns=&quot;http://www.w3.org/1999/xhtml&quot; xmlns:fb=&quot;http://www.facebook.com/2008/fbml&quot;&gt;
  &lt;head&gt;
    &lt;?php include_http_metas() ?&gt;
    &lt;?php include_metas() ?&gt;
    &lt;title&gt;Facebook App&lt;/title&gt;
    &lt;script src=&quot;http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.min.js&quot; type=&quot;text/javascript&quot;&gt;&lt;/script&gt;
    &lt;style&gt;
      div.section {
        margin-bottom: 10px;
        background: #EEEEEE;
        padding: 5px 10px;
      }
      body {
        font-family: Arial;
        font-size: 12px;
      }
      pre {
        font-size: 10px;
        color: #333333;
        margin-left: 50px;
      }
    &lt;/style&gt;
  &lt;/head&gt;
  &lt;body&gt;
    &lt;div id=&quot;fb-root&quot;&gt;&lt;/div&gt;
    &lt;script src=&quot;http://connect.facebook.net/en_US/all.js&quot; type=&quot;text/javascript&quot;&gt;&lt;/script&gt;
    &lt;script type=&quot;text/javascript&quot;&gt;
    FB.init({
       appId  : '&lt;?php echo sfConfig::get('app_facebook_app_id') ?&gt;',
       status : true, // vérifie le status de la connection
       cookie : true, // autorise les cookies pour permettre au serveur d'accéder à la session'
       xfbml  : true  // active le parsing de XFBML
      });
    &lt;/script&gt;
    ...</pre></td></tr></table></div>

<p>Nous en profitons pour charger JQuery afin de faciliter l&#8217;ajax et le remaniement du Dom.</p>
<p>Voilà, nous sommes prêts à attaquer les actions.</p>
<p>La première action va être l&#8217;authentification de l&#8217;utilisateur Facebook, et la gestion des permissions que celui-ci devra accorder à notre application.<br />
Le nouveau modèle de Facebook veut que nous demandions à l&#8217;utilisateur, dès le premier accès à l&#8217;application, qu&#8217;il accepte simultanément toutes les permissions nécessaires. Pour plus d&#8217;informations à ce sujet, consultez la liste des permissions sur <a href="http://developers.facebook.com/docs/authentication/permissions" target="_blank">le wiki Facebook Developer</a>.<br />
Voici l&#8217;action Login, qui sera appelée lorsque l&#8217;utilisateur n&#8217;a pas accordé ces permissions, ou lors d&#8217;une première visite de l&#8217;application:</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('p1187code59'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table width="100%" ><tr id="p118759"><td class="code" id="p1187code59"><pre class="php" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">//apps/frontend/modules/demo/actions/actions.class.php</span>
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> executeLogin<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: #666666; font-style: italic;">//on génère l'url qui permettra le login à l'application</span>
    <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">loginUrl</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">facebook</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getLoginUrl</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
            <span style="color: #0000ff;">'canvas'</span>    <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">,</span>
            <span style="color: #0000ff;">'fbconnect'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span>
            <span style="color: #666666; font-style: italic;">//ici on demande les permissions email, publication sur le mur, et changement du status</span>
            <span style="color: #0000ff;">'req_perms'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'email,publish_stream,status_update'</span><span style="color: #339933;">,</span>
            <span style="color: #0000ff;">'next'</span>      <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">''</span>
        <span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>Comme nous sommes dans une application iframe, il faut charger l&#8217;url de login depuis la frame parente, et notre template va s&#8217;en charger:</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('p1187code60'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table width="100%" ><tr id="p118760"><td class="code" id="p1187code60"><pre class="php" style="font-family:monospace;"><span style="color: #339933;">&lt;!--</span>loginSuccess<span style="color: #339933;">.</span>php<span style="color: #339933;">--&gt;</span>
<span style="color: #339933;">&lt;</span>script type<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;text/javascript&quot;</span><span style="color: #339933;">&gt;</span>
top<span style="color: #339933;">.</span>location<span style="color: #339933;">.</span>href <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;&lt;?php echo <span style="color: #006699; font-weight: bold;">$sf_data-&gt;getRaw</span>('loginUrl') ?&gt;&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">&lt;/script&gt;</span></pre></td></tr></table></div>

<p>Il faut maintenant que notre index redirige vers ce login dans les cas précédemment cités:</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('p1187code61'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table width="100%" ><tr id="p118761"><td class="code" id="p1187code61"><pre class="php" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">//apps/frontend/modules/demo/actions/actions.class.php</span>
<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: #666666; font-style: italic;">//on vérifie si l'utilisateur a une session facebook active</span>
    <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">session</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">facebook</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getSession</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">session</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
      <span style="color: #666666; font-style: italic;">//sinon il doit s'identifier</span>
      <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">redirect</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'demo/login'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">//s'il a une session active, on vérifie si elle est valide pour notre application</span>
    try
    <span style="color: #009900;">&#123;</span>
      <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">uid</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">facebook</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getUser</span><span style="color: #009900;">&#40;</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;">fbme</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">facebook</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">api</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'/me'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
    catch <span style="color: #009900;">&#40;</span>FacebookApiException <span style="color: #000088;">$e</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
      <span style="color: #666666; font-style: italic;">//sinon il doit à nouveau s'identifier</span>
      <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">redirect</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'demo/login'</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>On peut maintenant voir une demande de permissions s&#8217;afficher lorsque l&#8217;on se connecte à l&#8217;application:<br />
<a href="http://www.lexik.fr/blog/symfony/wp-content/uploads/2010/07/perms.png"><img class="alignnone size-medium wp-image-1189" title="perms" src="http://www.lexik.fr/blog/symfony/wp-content/uploads/2010/07/perms-300x172.png" alt="" width="300" height="172" /></a></p>
<p>En vérifiant la validité de la session, nous avons récupéré toutes les informations sur l&#8217;utilisateur auxquelles nos permissions nous donnent accès. Voyons l&#8217;étendue de ces informations en les affichant dans notre vue:</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('p1187code62'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table width="100%" ><tr id="p118762"><td class="code" id="p1187code62"><pre class="php" style="font-family:monospace;"><span style="color: #339933;">&lt;!--</span> indexSuccess<span style="color: #339933;">.</span>php <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;section&quot;</span><span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;</span>h3<span style="color: #339933;">&gt;</span>Informations utilisateur<span style="color: #339933;">&lt;/</span>h3<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;</span>pre<span style="color: #339933;">&gt;</span><span style="color: #000000; font-weight: bold;">&lt;?</span>php <span style="color: #990000;">print_r</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$fbme</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span> <span style="color: #339933;">/</span>pre<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;/</span>div<span style="color: #339933;">&gt;</span></pre></td></tr></table></div>

<p><a href="http://www.lexik.fr/blog/symfony/wp-content/uploads/2010/07/infos.png"><img class="alignnone size-medium wp-image-1195" title="infos" src="http://www.lexik.fr/blog/symfony/wp-content/uploads/2010/07/infos-300x174.png" alt="" width="300" height="174" /></a><br />
Nous accédons bien à l&#8217;adresse email comme spécifié, ainsi qu&#8217;aux informations génériques et publiques sur l&#8217;utilisateur.</p>
<p>Il est maintenant temps d&#8217;interagir avec l&#8217;utilisateur. Il nous a également accordé l&#8217;accès à son status, nous allons donc lui proposer de l&#8217;éditer via notre application:</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('p1187code63'); return false;">View Code</a> HTML</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table width="100%" ><tr id="p118763"><td class="code" id="p1187code63"><pre class="html" style="font-family:monospace;">&lt;!-- indexSuccess.php --&gt;
...
&lt;div class=&quot;section&quot;&gt;
  &lt;a onclick=&quot;updateStatus();return false;&quot; href=&quot;#&quot;&gt;Changer mon status via Ajax et l'API PHP&lt;/a&gt;
  &lt;textarea id=&quot;status_text&quot; cols=&quot;40&quot; rows=&quot;2&quot;&gt;  &lt;/textarea&gt;
&lt;/div&gt;
...
&lt;script type=&quot;text/javascript&quot;&gt;
function updateStatus()
  {
    var status = document.getElementById('status_text').value;
&nbsp;
    $.ajax({
      type: 'post',
      url: '&lt;?php echo url_for('demo/statusUpdate') ?&gt;',
      data: 'status=' + status,
      success: function(response) {
        alert(response);
      },
      error: function(response) {
        alert(response);
      }
    });
  }
&lt;/script&gt;</pre></td></tr></table></div>

<p>La méthode js &laquo;&nbsp;updateStatus()&nbsp;&raquo; va faire un appel ajax à une nouvelle action updateStatus de notre application:</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('p1187code64'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table width="100%" ><tr id="p118764"><td class="code" id="p1187code64"><pre class="php" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">//apps/frontend/modules/demo/actions/actions.class.php</span>
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> executeUpdateStatus<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;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">forward404Unless</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$request</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">isXmlHttpRequest</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: #000088;">$status</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$request</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getParameter</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'status'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    try
    <span style="color: #009900;">&#123;</span>
      <span style="color: #666666; font-style: italic;">//un simple appel a l'api suffit à poster un nouveau status sur le profile de l'utilisateur</span>
      <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">facebook</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">api</span><span style="color: #009900;">&#40;</span>
          <span style="color: #0000ff;">'/me/feed'</span><span style="color: #339933;">,</span>
          <span style="color: #0000ff;">'post'</span><span style="color: #339933;">,</span>
          <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">'message'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$status</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'cb'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">''</span> <span style="color: #009900;">&#41;</span>
        <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
    catch <span style="color: #009900;">&#40;</span>FacebookApiException <span style="color: #000088;">$e</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
      <span style="color: #b1b100;">return</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">renderText</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Erreur: '</span><span style="color: #339933;">.</span><span style="color: #000088;">$e</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #b1b100;">return</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">renderText</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Status mis-à-jour'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>L&#8217;api Javascript est toute aussi simple à utiliser. Proposons cette fois à l&#8217;utilisateur de parler de notre application sur son mur à l&#8217;aide de javascript:</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('p1187code65'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table width="100%" ><tr id="p118765"><td class="code" id="p1187code65"><pre class="php" style="font-family:monospace;"><span style="color: #339933;">&lt;!--</span> indexSuccess<span style="color: #339933;">.</span>php <span style="color: #339933;">--&gt;</span>
<span style="color: #339933;">...</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;section&quot;</span><span style="color: #339933;">&gt;</span>
  <span style="color: #339933;">&lt;</span>a onclick<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;publishWall();return false;&quot;</span> href<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;#&quot;</span><span style="color: #339933;">&gt;</span>Poster un article sur mon mur via l<span style="color: #0000ff;">'API Javascript&lt;/a&gt;
&lt;/div&gt;
...
&lt;script type=&quot;text/javascript&quot;&gt;
function publishWall()
  {
    FB.ui(
    {
      method: '</span>stream<span style="color: #339933;">.</span>publish<span style="color: #0000ff;">',
      message: '</span><span style="color: #0000ff;">',
      attachment:
      {
        name: &quot;Démo Symfony+Facebook&quot;,
        caption: '</span><span style="color: #0000ff;">',
        description: &quot;Une démo simple d'</span>application Facebook avec Symfony et les dernières API Javascript et PHP de Facebook<span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;,
        href: &quot;</span>http<span style="color: #339933;">:</span><span style="color: #666666; font-style: italic;">//www.lexik.fr/blog/symfony/non-classe/exemple-dapplication-utilisant-la-graph-api-de-facebook-1187&quot;</span>
      <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
      user_prompt_message<span style="color: #339933;">:</span> <span style="color: #0000ff;">&quot;Application de démo Symfony+Facebook&quot;</span>
    <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
    <span style="color: #000000; font-weight: bold;">function</span> <span style="color: #009900;">&#40;</span>response<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
<span style="color: #000000; font-weight: bold;">&lt;/script&gt;</span></pre></td></tr></table></div>

<p>Le résultat obtenu en cliquant sur le lien est le pseudo-popup bien connu de Facebook:<br />
<a href="http://www.lexik.fr/blog/symfony/wp-content/uploads/2010/07/publish.png"><img class="alignnone size-medium wp-image-1190" title="publish" src="http://www.lexik.fr/blog/symfony/wp-content/uploads/2010/07/publish-300x129.png" alt="" width="300" height="129" /></a></p>
<p>Nous venons de voir quelques méthodes simples d&#8217;accès ou de publication de données. Une liste plus complète des méthodes éxistantes est sur <a href="http://developers.facebook.com/docs/reference/rest/" target="_blank">le wiki Facebook Developer</a>.<br />
Dans le cadre d&#8217;applications plus complexes, il sera souvent utile de pouvoir formuler précisément les informations que l&#8217;on souhaite récupérer, et celles-ci ne correspondront pas nécessairement à une méthode éxistante. Pour cela Facebook met à notre disposition FQL, un langage de requète similaire à SQL dans sa syntaxe, pour générer nos requètes spécifiques.</p>
<p>Par exemple, récupérons la liste des amis de l&#8217;utilisateur avec leur nom, leur uid, et leur photo. Voici la requète FQL pour cela, et son éxécution via l&#8217;api PHP:</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('p1187code66'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table width="100%" ><tr id="p118766"><td class="code" id="p1187code66"><pre class="php" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">//apps/frontend/modules/demo/actions/actions.class.php</span>
<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: #339933;">...</span>
    <span style="color: #666666; font-style: italic;">//récupération des amis via FQL</span>
    <span style="color: #000088;">$query</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;SELECT name, uid, pic_square FROM user WHERE uid in (SELECT uid2 FROM friend WHERE uid1 = &quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">uid</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;)&quot;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">amis</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">facebook</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">api</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
        <span style="color: #0000ff;">'method'</span>    <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'fql.query'</span><span style="color: #339933;">,</span>
        <span style="color: #0000ff;">'query'</span>     <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$query</span><span style="color: #339933;">,</span>
        <span style="color: #0000ff;">'callback'</span>  <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">''</span>
    <span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>Et l&#8217;affichage de la liste dans la vue:</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('p1187code67'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table width="100%" ><tr id="p118767"><td class="code" id="p1187code67"><pre class="php" style="font-family:monospace;"><span style="color: #339933;">&lt;!--</span> indexSuccess<span style="color: #339933;">.</span>php <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;section&quot;</span><span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;</span>h3<span style="color: #339933;">&gt;</span>Liste d<span style="color: #0000ff;">'amis&lt;/h3&gt;
&lt;?php foreach ($amis as $ami): ?&gt;
&lt;ul&gt;
	&lt;li&gt;&lt;img src=&quot;&lt;?php echo $ami['</span>pic_square<span style="color: #0000ff;">'] ?&gt;&quot; alt=&quot;&quot; /&gt;&lt;/li&gt;
	&lt;li&gt;&lt;strong&gt;&lt;?php echo $ami ['</span>name<span style="color: #0000ff;">'] ?&gt;&lt;/strong&gt;&lt;/li&gt;
	&lt;li&gt;uid: &lt;?php echo $ami['</span>uid<span style="color: #0000ff;">'] ?&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;?php endforeach; ?&gt;
&lt;/div&gt;</span></pre></td></tr></table></div>

<p>Cet article n&#8217;était pas dédié à Symfony, mais l&#8217;utilisait plutôt pour proposer un environnement familier à notre application. Ce survol rapide de la nouvelle Graph Api permet surtout de donner un point de départ sur l&#8217;utilisation de la nouvelle librairie à un moment ou la documentation à son sujet n&#8217;est pas vraiment fournie, en particulier en français !<br />
Vous pouvez voir l&#8217;application tourner sur <a href="http://apps.facebook.com/demoLexik" target="_blank">http://apps.facebook.com/demoLexik</a>.</p>
<a href="http://www.addtoany.com/add_to/twitter?linkurl=http%3A%2F%2Fwww.lexik.fr%2Fblog%2Fsymfony%2Fsymfony%2Fexemple-dapplication-utilisant-la-graph-api-de-facebook-1187&amp;linkname=Exemple%20d%26%238217%3Bapplication%20utilisant%20la%20Graph%20API%20de%20Facebook" title="Twitter" rel="nofollow" target="_blank"><img src="http://www.lexik.fr/blog/symfony/wp-content/plugins/add-to-any/icons/twitter.png" alt="Twitter"/></a> <a href="http://www.addtoany.com/add_to/facebook?linkurl=http%3A%2F%2Fwww.lexik.fr%2Fblog%2Fsymfony%2Fsymfony%2Fexemple-dapplication-utilisant-la-graph-api-de-facebook-1187&amp;linkname=Exemple%20d%26%238217%3Bapplication%20utilisant%20la%20Graph%20API%20de%20Facebook" title="Facebook" rel="nofollow" target="_blank"><img src="http://www.lexik.fr/blog/symfony/wp-content/plugins/add-to-any/icons/facebook.png" alt="Facebook"/></a> <a href="http://www.addtoany.com/add_to/delicious?linkurl=http%3A%2F%2Fwww.lexik.fr%2Fblog%2Fsymfony%2Fsymfony%2Fexemple-dapplication-utilisant-la-graph-api-de-facebook-1187&amp;linkname=Exemple%20d%26%238217%3Bapplication%20utilisant%20la%20Graph%20API%20de%20Facebook" title="Delicious" rel="nofollow" target="_blank"><img src="http://www.lexik.fr/blog/symfony/wp-content/plugins/add-to-any/icons/delicious.png" alt="Delicious"/></a> <a href="http://www.addtoany.com/add_to/viadeo?linkurl=http%3A%2F%2Fwww.lexik.fr%2Fblog%2Fsymfony%2Fsymfony%2Fexemple-dapplication-utilisant-la-graph-api-de-facebook-1187&amp;linkname=Exemple%20d%26%238217%3Bapplication%20utilisant%20la%20Graph%20API%20de%20Facebook" title="Viadeo" rel="nofollow" target="_blank"><img src="http://www.lexik.fr/blog/symfony/wp-content/plugins/add-to-any/icons/viadeo.png" alt="Viadeo"/></a> <a href="http://www.addtoany.com/add_to/technorati_favorites?linkurl=http%3A%2F%2Fwww.lexik.fr%2Fblog%2Fsymfony%2Fsymfony%2Fexemple-dapplication-utilisant-la-graph-api-de-facebook-1187&amp;linkname=Exemple%20d%26%238217%3Bapplication%20utilisant%20la%20Graph%20API%20de%20Facebook" title="Technorati Favorites" rel="nofollow" target="_blank"><img src="http://www.lexik.fr/blog/symfony/wp-content/plugins/add-to-any/icons/technorati.png" alt="Technorati Favorites"/></a> <a href="http://www.addtoany.com/add_to/netvibes_share?linkurl=http%3A%2F%2Fwww.lexik.fr%2Fblog%2Fsymfony%2Fsymfony%2Fexemple-dapplication-utilisant-la-graph-api-de-facebook-1187&amp;linkname=Exemple%20d%26%238217%3Bapplication%20utilisant%20la%20Graph%20API%20de%20Facebook" title="Netvibes Share" rel="nofollow" target="_blank"><img src="http://www.lexik.fr/blog/symfony/wp-content/plugins/add-to-any/icons/netvibes.png" alt="Netvibes Share"/></a> <a href="http://www.addtoany.com/add_to/gmail?linkurl=http%3A%2F%2Fwww.lexik.fr%2Fblog%2Fsymfony%2Fsymfony%2Fexemple-dapplication-utilisant-la-graph-api-de-facebook-1187&amp;linkname=Exemple%20d%26%238217%3Bapplication%20utilisant%20la%20Graph%20API%20de%20Facebook" title="Gmail" rel="nofollow" target="_blank"><img src="http://www.lexik.fr/blog/symfony/wp-content/plugins/add-to-any/icons/gmail.png" alt="Gmail"/></a> <a href="http://www.addtoany.com/add_to/yoolink?linkurl=http%3A%2F%2Fwww.lexik.fr%2Fblog%2Fsymfony%2Fsymfony%2Fexemple-dapplication-utilisant-la-graph-api-de-facebook-1187&amp;linkname=Exemple%20d%26%238217%3Bapplication%20utilisant%20la%20Graph%20API%20de%20Facebook" title="Yoolink" rel="nofollow" target="_blank"><img src="http://www.lexik.fr/blog/symfony/wp-content/plugins/add-to-any/icons/yoolink.png" alt="Yoolink"/></a> <a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save?linkurl=http%3A%2F%2Fwww.lexik.fr%2Fblog%2Fsymfony%2Fsymfony%2Fexemple-dapplication-utilisant-la-graph-api-de-facebook-1187&amp;linkname=Exemple%20d%26%238217%3Bapplication%20utilisant%20la%20Graph%20API%20de%20Facebook">Partager cet article</a>]]></content:encoded>
			<wfw:commentRss>http://www.lexik.fr/blog/symfony/symfony/exemple-dapplication-utilisant-la-graph-api-de-facebook-1187/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Validation d&#8217;un numéro siret : sfValidatorSiret</title>
		<link>http://www.lexik.fr/blog/symfony/symfony/1-4-x/validation-dun-numero-siret-sfvalidatorsiret-1159</link>
		<comments>http://www.lexik.fr/blog/symfony/symfony/1-4-x/validation-dun-numero-siret-sfvalidatorsiret-1159#comments</comments>
		<pubDate>Tue, 25 May 2010 06:30:00 +0000</pubDate>
		<dc:creator>thomas</dc:creator>
				<category><![CDATA[1.4.x]]></category>
		<category><![CDATA[tips]]></category>
		<category><![CDATA[siret]]></category>
		<category><![CDATA[Symfony]]></category>
		<category><![CDATA[validator]]></category>

		<guid isPermaLink="false">http://www.lexik.fr/blog/symfony/?p=1159</guid>
		<description><![CDATA[Bonjour, comme vous avez pu le voir avec les récents posts, nous avons eu pas mal de gestion de facturation: conversion de devises, validation d&#8217;un numéro de tva intracommunautaire. Pour continuer sur la lancée, nous allons faire un petit validator sur les numéros siret. Un numéro siret correspond à un petit algorithme assez simple expliqué [...]]]></description>
			<content:encoded><![CDATA[<p>Bonjour,</p>
<p>comme vous avez pu le voir avec les récents posts, nous avons eu pas mal de gestion de facturation: <a title="conversion de devises" href="http://www.lexik.fr/blog/symfony/symfony/tips-conversions-de-devises-1137" target="_blank">conversion de devises</a>, <a title="tva intracommunautaire" href="http://www.lexik.fr/blog/symfony/symfony/un-validator-tva-bien-pratique-1123" target="_blank">validation d&#8217;un numéro de tva intracommunautaire</a>.</p>
<p>Pour continuer sur la lancée, nous allons faire un petit validator sur les numéros siret.</p>
<p><span id="more-1159"></span>Un numéro siret correspond à un petit algorithme assez simple <a title="algorithme numéro siret" href="http://fr.wikipedia.org/wiki/Syst%C3%A8me_d%E2%80%99identification_du_r%C3%A9pertoire_des_%C3%A9tablissements#Calcul_et_validit.C3.A9_d.27un_num.C3.A9ro_SIRET" target="_blank">expliqué ici</a> (<a title="Luhn" href="http://fr.wikipedia.org/wiki/Luhn" target="_blank">algorithme de Luhn</a>)<br />
Le principe est le suivant : on multiplie les chiffres de rang impair à partir de la droite par 1, ceux de rang pair par 2 ; la somme des chiffres obtenus doit être congrue au modulo 10, c&#8217;est-à-dire qu&#8217;elle doit être multiple de 10.<br />
Un numéro siret comporte 14 chiffre.</p>
<p>Voici le code du validator: sfValidatorSiret.class.php</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('p1159code69'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table width="100%" ><tr id="p115969"><td class="code" id="p1159code69"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">class</span> sfValidatorSiret <span style="color: #000000; font-weight: bold;">extends</span> sfValidatorBase 
<span style="color: #009900;">&#123;</span>
  protected <span style="color: #000000; font-weight: bold;">function</span> doClean<span style="color: #009900;">&#40;</span><span style="color: #000088;">$values</span><span style="color: #009900;">&#41;</span>
  <span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$siret</span> <span style="color: #339933;">=</span> <span style="color: #990000;">trim</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$values</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">empty</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$siret</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">||</span> <span style="color: #990000;">strlen</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$siret</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">!=</span> <span style="color: #cc66cc;">14</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
      throw <span style="color: #000000; font-weight: bold;">new</span> sfValidatorError<span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'Le numéro siret est invalide'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #000088;">$sum</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">for</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$i</span><span style="color: #339933;">=</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span> <span style="color: #000088;">$i</span><span style="color: #339933;">&lt;</span><span style="color: #cc66cc;">14</span><span style="color: #339933;">;</span> <span style="color: #000088;">$i</span><span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
      <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$i</span><span style="color: #339933;">%</span><span style="color:#800080;">2</span> <span style="color: #339933;">==</span> <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span>
      <span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$tmp</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$siret</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$i</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">*</span><span style="color: #cc66cc;">2</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$tmp</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$tmp</span> <span style="color: #339933;">&gt;</span> <span style="color: #cc66cc;">9</span> ? <span style="color: #000088;">$tmp</span> <span style="color: #339933;">-</span> <span style="color: #cc66cc;">9</span> <span style="color: #339933;">:</span> <span style="color: #000088;">$tmp</span><span style="color: #339933;">;</span>
      <span style="color: #009900;">&#125;</span>
      <span style="color: #b1b100;">else</span>
      <span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$tmp</span><span style="color: #339933;">=</span> <span style="color: #000088;">$siret</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$i</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
      <span style="color: #009900;">&#125;</span>
      <span style="color: #000088;">$sum</span> <span style="color: #339933;">+=</span> <span style="color: #000088;">$tmp</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$sum</span><span style="color: #339933;">%</span><span style="color:#800080;">10</span> <span style="color: #339933;">!==</span> <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
      throw <span style="color: #000000; font-weight: bold;">new</span> sfValidatorError<span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'Le numéro siret est invalide'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #b1b100;">return</span> <span style="color: #000088;">$siret</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>Pour expliquer rapidement:<br />
1. si la valeur est vide ou de longueur différente de 14 on retourne une exception.<br />
2. on parcours les 14 chiffres, si le chiffre est paire alors on le multiplie par 2 et si ce résultat est supérieur à  9 alors on retranche 9.<br />
ex: 7 =&gt; 7&#215;2 = 14 =&gt; 14 &#8211; 9 = 5.<br />
faire 14 -9  reviens à additionner les 2 chiffres: 1 + 4 = 5.<br />
3. On additionne tous les chiffres paires x2 et les chiffres impaires.<br />
4. si le résultat est divisible par 10 alors c&#8217;est un numéro siret.</p>
<p>Attention ceci indique que le numéro fourni peut être un numéro siret, MAIS PAS que ce numéro est réellement un numéro siret inscrit à L&#8217;INSEE, actif et correspond à une société.</p>
<p><a href="http://www.lexik.fr/blog/symfony/wp-content/uploads/2010/05/sfValidatorSiret.tar.gz">Vous pouvez télécharger le validator ici.</a></p>
<a href="http://www.addtoany.com/add_to/twitter?linkurl=http%3A%2F%2Fwww.lexik.fr%2Fblog%2Fsymfony%2Fsymfony%2F1-4-x%2Fvalidation-dun-numero-siret-sfvalidatorsiret-1159&amp;linkname=Validation%20d%26%238217%3Bun%20num%C3%A9ro%20siret%20%3A%20sfValidatorSiret" title="Twitter" rel="nofollow" target="_blank"><img src="http://www.lexik.fr/blog/symfony/wp-content/plugins/add-to-any/icons/twitter.png" alt="Twitter"/></a> <a href="http://www.addtoany.com/add_to/facebook?linkurl=http%3A%2F%2Fwww.lexik.fr%2Fblog%2Fsymfony%2Fsymfony%2F1-4-x%2Fvalidation-dun-numero-siret-sfvalidatorsiret-1159&amp;linkname=Validation%20d%26%238217%3Bun%20num%C3%A9ro%20siret%20%3A%20sfValidatorSiret" title="Facebook" rel="nofollow" target="_blank"><img src="http://www.lexik.fr/blog/symfony/wp-content/plugins/add-to-any/icons/facebook.png" alt="Facebook"/></a> <a href="http://www.addtoany.com/add_to/delicious?linkurl=http%3A%2F%2Fwww.lexik.fr%2Fblog%2Fsymfony%2Fsymfony%2F1-4-x%2Fvalidation-dun-numero-siret-sfvalidatorsiret-1159&amp;linkname=Validation%20d%26%238217%3Bun%20num%C3%A9ro%20siret%20%3A%20sfValidatorSiret" title="Delicious" rel="nofollow" target="_blank"><img src="http://www.lexik.fr/blog/symfony/wp-content/plugins/add-to-any/icons/delicious.png" alt="Delicious"/></a> <a href="http://www.addtoany.com/add_to/viadeo?linkurl=http%3A%2F%2Fwww.lexik.fr%2Fblog%2Fsymfony%2Fsymfony%2F1-4-x%2Fvalidation-dun-numero-siret-sfvalidatorsiret-1159&amp;linkname=Validation%20d%26%238217%3Bun%20num%C3%A9ro%20siret%20%3A%20sfValidatorSiret" title="Viadeo" rel="nofollow" target="_blank"><img src="http://www.lexik.fr/blog/symfony/wp-content/plugins/add-to-any/icons/viadeo.png" alt="Viadeo"/></a> <a href="http://www.addtoany.com/add_to/technorati_favorites?linkurl=http%3A%2F%2Fwww.lexik.fr%2Fblog%2Fsymfony%2Fsymfony%2F1-4-x%2Fvalidation-dun-numero-siret-sfvalidatorsiret-1159&amp;linkname=Validation%20d%26%238217%3Bun%20num%C3%A9ro%20siret%20%3A%20sfValidatorSiret" title="Technorati Favorites" rel="nofollow" target="_blank"><img src="http://www.lexik.fr/blog/symfony/wp-content/plugins/add-to-any/icons/technorati.png" alt="Technorati Favorites"/></a> <a href="http://www.addtoany.com/add_to/netvibes_share?linkurl=http%3A%2F%2Fwww.lexik.fr%2Fblog%2Fsymfony%2Fsymfony%2F1-4-x%2Fvalidation-dun-numero-siret-sfvalidatorsiret-1159&amp;linkname=Validation%20d%26%238217%3Bun%20num%C3%A9ro%20siret%20%3A%20sfValidatorSiret" title="Netvibes Share" rel="nofollow" target="_blank"><img src="http://www.lexik.fr/blog/symfony/wp-content/plugins/add-to-any/icons/netvibes.png" alt="Netvibes Share"/></a> <a href="http://www.addtoany.com/add_to/gmail?linkurl=http%3A%2F%2Fwww.lexik.fr%2Fblog%2Fsymfony%2Fsymfony%2F1-4-x%2Fvalidation-dun-numero-siret-sfvalidatorsiret-1159&amp;linkname=Validation%20d%26%238217%3Bun%20num%C3%A9ro%20siret%20%3A%20sfValidatorSiret" title="Gmail" rel="nofollow" target="_blank"><img src="http://www.lexik.fr/blog/symfony/wp-content/plugins/add-to-any/icons/gmail.png" alt="Gmail"/></a> <a href="http://www.addtoany.com/add_to/yoolink?linkurl=http%3A%2F%2Fwww.lexik.fr%2Fblog%2Fsymfony%2Fsymfony%2F1-4-x%2Fvalidation-dun-numero-siret-sfvalidatorsiret-1159&amp;linkname=Validation%20d%26%238217%3Bun%20num%C3%A9ro%20siret%20%3A%20sfValidatorSiret" title="Yoolink" rel="nofollow" target="_blank"><img src="http://www.lexik.fr/blog/symfony/wp-content/plugins/add-to-any/icons/yoolink.png" alt="Yoolink"/></a> <a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save?linkurl=http%3A%2F%2Fwww.lexik.fr%2Fblog%2Fsymfony%2Fsymfony%2F1-4-x%2Fvalidation-dun-numero-siret-sfvalidatorsiret-1159&amp;linkname=Validation%20d%26%238217%3Bun%20num%C3%A9ro%20siret%20%3A%20sfValidatorSiret">Partager cet article</a>]]></content:encoded>
			<wfw:commentRss>http://www.lexik.fr/blog/symfony/symfony/1-4-x/validation-dun-numero-siret-sfvalidatorsiret-1159/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Tips : Conversions de devises</title>
		<link>http://www.lexik.fr/blog/symfony/symfony/tips-conversions-de-devises-1137</link>
		<comments>http://www.lexik.fr/blog/symfony/symfony/tips-conversions-de-devises-1137#comments</comments>
		<pubDate>Mon, 17 May 2010 12:29:10 +0000</pubDate>
		<dc:creator>Nikaw</dc:creator>
				<category><![CDATA[1.0.x]]></category>
		<category><![CDATA[1.2.x]]></category>
		<category><![CDATA[1.3.x]]></category>
		<category><![CDATA[1.4.x]]></category>
		<category><![CDATA[Symfony]]></category>
		<category><![CDATA[astuce]]></category>
		<category><![CDATA[helper]]></category>
		<category><![CDATA[taux de change]]></category>

		<guid isPermaLink="false">http://www.lexik.fr/blog/symfony/?p=1137</guid>
		<description><![CDATA[Aujourd&#8217;hui je vous montre juste une petite astuce pour avoir les taux de conversion de l&#8217;Euro vers d&#8217;autres devises. Le site de la banque centrale Européenne publie tous les jours un fichier XML contenant les taux de conversion à jour. Il fournit également un morceau de code donnant un exemple de manipulation http://www.ecb.europa.eu/stats/eurofxref/eurofxref-daily.xml Voici le [...]]]></description>
			<content:encoded><![CDATA[<p>Aujourd&#8217;hui je vous montre juste une petite astuce pour avoir les taux de conversion de l&#8217;Euro vers d&#8217;autres devises.</p>
<p>Le site de la banque centrale Européenne publie tous les jours un fichier XML contenant les taux de conversion à jour.</p>
<p>Il fournit également un morceau de code donnant un exemple de manipulation<br />
<span id="more-1137"></span><br />
<a href="http://www.ecb.europa.eu/stats/eurofxref/eurofxref-daily.xml">http://www.ecb.europa.eu/stats/eurofxref/eurofxref-daily.xml</a></p>
<p>Voici le code fourni par le site lui-même pour les développeurs :</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('p1137code73'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table width="100%" ><tr id="p113773"><td class="code" id="p1137code73"><pre class="php" style="font-family:monospace;">  <span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #666666; font-style: italic;">//This is a PHP (4/5) script example on how eurofxref-daily.xml can be parsed </span>
&nbsp;
<span style="color: #666666; font-style: italic;">//Read eurofxref-daily.xml file in memory </span>
<span style="color: #000088;">$XMLContent</span><span style="color: #339933;">=</span> <span style="color: #990000;">file</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;http://www.ecb.europa.eu/stats/eurofxref/eurofxref-daily.xml&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #666666; font-style: italic;">//the file is updated daily between 2.15 p.m. and 3.00 p.m. CET</span>
&nbsp;
<span style="color: #b1b100;">foreach</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$XMLContent</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$line</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">ereg</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;currency='([[:alpha:]]+)'&quot;</span><span style="color: #339933;">,</span><span style="color: #000088;">$line</span><span style="color: #339933;">,</span><span style="color: #000088;">$currencyCode</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">ereg</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;rate='([[:graph:]]+)'&quot;</span><span style="color: #339933;">,</span><span style="color: #000088;">$line</span><span style="color: #339933;">,</span><span style="color: #000088;">$rate</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                    <span style="color: #666666; font-style: italic;">//Output the value of 1 EUR for a currency code </span>
                    <span style="color: #990000;">echo</span> <span style="color: #0000ff;">'1 &amp;euro; = '</span><span style="color: #339933;">.</span><span style="color: #000088;">$rate</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">' '</span><span style="color: #339933;">.</span><span style="color: #000088;">$currencyCode</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">'&lt;br /&gt;'</span><span style="color: #339933;">;</span>
                    <span style="color: #666666; font-style: italic;">//--------------------------------------------------</span>
                    <span style="color: #666666; font-style: italic;">// Here you can add your code for inserting</span>
                    <span style="color: #666666; font-style: italic;">// $rate[1] and $currencyCode[1] into your database</span>
                    <span style="color: #666666; font-style: italic;">//--------------------------------------------------</span>
            <span style="color: #009900;">&#125;</span>
        <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>Ce code permet d&#8217;afficher les taux de conversion Euro vers devise pour toutes les devises.<br />
(source : <a href="http://www.ecb.europa.eu/stats/exchange/eurofxref/html/index.en.html">http://www.ecb.europa.eu/stats/exchange/eurofxref/html/index.en.html</a>)</p>
<p>Et voici un helper tout simple permettant de convertir directement de EUR vers une devise, en utilisant la librairie Dom.</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('p1137code74'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table width="100%" ><tr id="p113774"><td class="code" id="p1137code74"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> convertEuroToCurrency<span style="color: #009900;">&#40;</span><span style="color: #000088;">$value</span><span style="color: #339933;">,</span> <span style="color: #000088;">$currency</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
<span style="color: #666666; font-style: italic;">//ouverture du doc</span>
  <span style="color: #000088;">$dom_rates</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> DOMDocument<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #000088;">$dom_rates</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">load</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;http://www.ecb.europa.eu/stats/eurofxref/eurofxref-daily.xml&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #666666; font-style: italic;">//récupération de l'ensemble des éléments Cube</span>
  <span style="color: #000088;">$dom_cubes</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$dom_rates</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getElementsByTagName</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Cube'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #000088;">$i</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
  <span style="color: #000088;">$j</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
  <span style="color: #000088;">$rate_found</span><span style="color: #339933;">=</span><span style="color: #000000; font-weight: bold;">false</span><span style="color: #339933;">;</span>
  <span style="color: #666666; font-style: italic;">//tant qu'on a pas trouvé le bon pays, on continue</span>
  <span style="color: #b1b100;">while</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #000088;">$rate_found</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #000088;">$cube</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$dom_cubes</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">item</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$i</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
  <span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$j</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
    <span style="color: #666666; font-style: italic;">//tant qu'on a pas trouvé le bon pays, on continue</span>
    <span style="color: #b1b100;">while</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #000088;">$rate_found</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #000088;">$attribute</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$cube</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">attributes</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">item</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$j</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
    <span style="color: #666666; font-style: italic;">//si on est en train de lire le noeud &quot;temps&quot; on stocke la date</span>
      <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$attribute</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">name</span> <span style="color: #339933;">==</span> <span style="color: #0000ff;">'time'</span><span style="color: #009900;">&#41;</span> <span style="color: #000088;">$date</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$attribute</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">value</span><span style="color: #339933;">;</span>
&nbsp;
      <span style="color: #666666; font-style: italic;">//si l'attribut et &quot;currency&quot; on est en train de lire le code pays, on vérifie aussi que c'est le bon</span>
      <span style="color: #000088;">$rate_found</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$attribute</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">name</span> <span style="color: #339933;">==</span> <span style="color: #0000ff;">'currency'</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #000088;">$attribute</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">value</span> <span style="color: #339933;">==</span> <span style="color: #000088;">$currency</span><span style="color: #339933;">;</span>
      <span style="color: #000088;">$j</span><span style="color: #339933;">++;</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #000088;">$i</span><span style="color: #339933;">++;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #666666; font-style: italic;">//si le pays a été trouvé, on affiche le résultat de la conversion</span>
  <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$rate_found</span><span style="color: #009900;">&#41;</span>
  <span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">return</span> <span style="color: #0000ff;">&quot;le &quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$date</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot; &quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$value</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot; EUR = &quot;</span><span style="color: #339933;">.</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$cube</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">attributes</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">item</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$j</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">value</span><span style="color: #339933;">*</span><span style="color: #000088;">$value</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">' '</span><span style="color: #339933;">.</span><span style="color: #000088;">$cube</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">attributes</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">item</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$j</span><span style="color: #339933;">-</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">value</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span><span style="color: #b1b100;">else</span><span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">return</span> <span style="color: #0000ff;">&quot;cette devise n'existe pas&quot;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>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('p1137code75'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table width="100%" ><tr id="p113775"><td class="code" id="p1137code75"><pre class="php" style="font-family:monospace;">use_helper<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Converter'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">echo</span> convertEurToCurrency<span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">125</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'USD'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>Et voilà !</p>
<a href="http://www.addtoany.com/add_to/twitter?linkurl=http%3A%2F%2Fwww.lexik.fr%2Fblog%2Fsymfony%2Fsymfony%2Ftips-conversions-de-devises-1137&amp;linkname=Tips%20%3A%20Conversions%20de%20devises" title="Twitter" rel="nofollow" target="_blank"><img src="http://www.lexik.fr/blog/symfony/wp-content/plugins/add-to-any/icons/twitter.png" alt="Twitter"/></a> <a href="http://www.addtoany.com/add_to/facebook?linkurl=http%3A%2F%2Fwww.lexik.fr%2Fblog%2Fsymfony%2Fsymfony%2Ftips-conversions-de-devises-1137&amp;linkname=Tips%20%3A%20Conversions%20de%20devises" title="Facebook" rel="nofollow" target="_blank"><img src="http://www.lexik.fr/blog/symfony/wp-content/plugins/add-to-any/icons/facebook.png" alt="Facebook"/></a> <a href="http://www.addtoany.com/add_to/delicious?linkurl=http%3A%2F%2Fwww.lexik.fr%2Fblog%2Fsymfony%2Fsymfony%2Ftips-conversions-de-devises-1137&amp;linkname=Tips%20%3A%20Conversions%20de%20devises" title="Delicious" rel="nofollow" target="_blank"><img src="http://www.lexik.fr/blog/symfony/wp-content/plugins/add-to-any/icons/delicious.png" alt="Delicious"/></a> <a href="http://www.addtoany.com/add_to/viadeo?linkurl=http%3A%2F%2Fwww.lexik.fr%2Fblog%2Fsymfony%2Fsymfony%2Ftips-conversions-de-devises-1137&amp;linkname=Tips%20%3A%20Conversions%20de%20devises" title="Viadeo" rel="nofollow" target="_blank"><img src="http://www.lexik.fr/blog/symfony/wp-content/plugins/add-to-any/icons/viadeo.png" alt="Viadeo"/></a> <a href="http://www.addtoany.com/add_to/technorati_favorites?linkurl=http%3A%2F%2Fwww.lexik.fr%2Fblog%2Fsymfony%2Fsymfony%2Ftips-conversions-de-devises-1137&amp;linkname=Tips%20%3A%20Conversions%20de%20devises" title="Technorati Favorites" rel="nofollow" target="_blank"><img src="http://www.lexik.fr/blog/symfony/wp-content/plugins/add-to-any/icons/technorati.png" alt="Technorati Favorites"/></a> <a href="http://www.addtoany.com/add_to/netvibes_share?linkurl=http%3A%2F%2Fwww.lexik.fr%2Fblog%2Fsymfony%2Fsymfony%2Ftips-conversions-de-devises-1137&amp;linkname=Tips%20%3A%20Conversions%20de%20devises" title="Netvibes Share" rel="nofollow" target="_blank"><img src="http://www.lexik.fr/blog/symfony/wp-content/plugins/add-to-any/icons/netvibes.png" alt="Netvibes Share"/></a> <a href="http://www.addtoany.com/add_to/gmail?linkurl=http%3A%2F%2Fwww.lexik.fr%2Fblog%2Fsymfony%2Fsymfony%2Ftips-conversions-de-devises-1137&amp;linkname=Tips%20%3A%20Conversions%20de%20devises" title="Gmail" rel="nofollow" target="_blank"><img src="http://www.lexik.fr/blog/symfony/wp-content/plugins/add-to-any/icons/gmail.png" alt="Gmail"/></a> <a href="http://www.addtoany.com/add_to/yoolink?linkurl=http%3A%2F%2Fwww.lexik.fr%2Fblog%2Fsymfony%2Fsymfony%2Ftips-conversions-de-devises-1137&amp;linkname=Tips%20%3A%20Conversions%20de%20devises" title="Yoolink" rel="nofollow" target="_blank"><img src="http://www.lexik.fr/blog/symfony/wp-content/plugins/add-to-any/icons/yoolink.png" alt="Yoolink"/></a> <a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save?linkurl=http%3A%2F%2Fwww.lexik.fr%2Fblog%2Fsymfony%2Fsymfony%2Ftips-conversions-de-devises-1137&amp;linkname=Tips%20%3A%20Conversions%20de%20devises">Partager cet article</a>]]></content:encoded>
			<wfw:commentRss>http://www.lexik.fr/blog/symfony/symfony/tips-conversions-de-devises-1137/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Valider un numéro de TVA intracommunautaire</title>
		<link>http://www.lexik.fr/blog/symfony/symfony/un-validator-tva-bien-pratique-1123</link>
		<comments>http://www.lexik.fr/blog/symfony/symfony/un-validator-tva-bien-pratique-1123#comments</comments>
		<pubDate>Mon, 03 May 2010 08:23:01 +0000</pubDate>
		<dc:creator>Nikaw</dc:creator>
				<category><![CDATA[1.2.x]]></category>
		<category><![CDATA[1.3.x]]></category>
		<category><![CDATA[1.4.x]]></category>
		<category><![CDATA[Symfony]]></category>
		<category><![CDATA[formulaire]]></category>
		<category><![CDATA[TVA]]></category>
		<category><![CDATA[validator]]></category>

		<guid isPermaLink="false">http://www.lexik.fr/blog/symfony/?p=1123</guid>
		<description><![CDATA[Aujourd&#8217;hui il est courant d&#8217;avoir à développer des sites avec abonnement, mise à disposition de service payant en ligne et autre, à l&#8217;échelle internationale et à des professionnels. Or il se trouve que dans une situation pareille intervient la notion de TVA, où la responsabilité du développeur (ou de son employeur) peut être mise en [...]]]></description>
			<content:encoded><![CDATA[<p>Aujourd&#8217;hui il est courant d&#8217;avoir à développer des sites avec abonnement, mise à disposition de service payant en ligne et autre, à l&#8217;échelle internationale et à des professionnels.</p>
<p>Or il se trouve que dans une situation pareille intervient la notion de TVA, où la responsabilité du développeur (ou de son employeur) peut être mise en jeu (ainsi que celle du client, mais ça&#8230;).</p>
<p>Lors du paiement, un client professionnel (une société, entreprise, personne morale quoi) devra saisir son numéro de TVA ainsi que son pays pour que le montant de la TVA correspondant soit calculé puisqu&#8217;il change d&#8217;un pays à l&#8217;autre.<br />
Le site http://ec.europa.eu/ propose un webService permettant de vérifier cela.</p>
<p>Moi, je vous propose un validator personnalisé pour valider le numéro de TVA entré dès la validation du formulaire, basé sur ce webService.<br />
<span id="more-1123"></span><br />
Voici l&#8217;adresse du wsdl : <a href="http://ec.europa.eu/taxation_customs/vies/services/checkVatService.wsdl">http://ec.europa.eu/taxation_customs/vies/services/checkVatService.wsdl</a>.</p>
<h2>Le validator</h2>
<p>Je pars du principe que l&#8217;utilisateur saisit son numéro de TVA et son code pays dans 2 champs différents. Le validator devra alors faire un contrôle en prenant en compte les 2 champs, à l&#8217;instar du validator Doctrine qui vérifie la concordance entre le username et le password au login. Donc il héritera de la classe sfValidatorSchema.</p>
<p>Commençons par voir la méthode __construct :</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('p1123code81'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table width="100%" ><tr id="p112381"><td class="code" id="p1123code81"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> __construct<span style="color: #009900;">&#40;</span><span style="color: #000088;">$vat_number</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'vat_number'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$country</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'country'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$options</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$messages</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</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;">addOption</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'vat_number'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$vat_number</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;">addOption</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'country'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$country</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;">addOption</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'throw_global_error'</span><span style="color: #339933;">,</span> <span style="color: #000000; font-weight: bold;">false</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;">messages</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array_merge</span><span style="color: #009900;">&#40;</span>
    <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">messages</span><span style="color: #339933;">,</span>
    <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
      <span style="color: #0000ff;">'invalid_syntax'</span><span style="color: #339933;">=&gt;</span><span style="color: #0000ff;">'Your VAT Number syntax is not correct. You should have something like this: BE805670816B01'</span><span style="color: #339933;">,</span>
      <span style="color: #0000ff;">'invalid_country'</span><span style="color: #339933;">=&gt;</span><span style="color: #0000ff;">'Your VAT Number is not valid for the selected country.'</span><span style="color: #339933;">,</span>
      <span style="color: #0000ff;">'invalid'</span><span style="color: #339933;">=&gt;</span>sprintf<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Invalid VAT Number. Check the validity on the customer VAT Number via &lt;a href=&quot;%s&quot;&gt;Europa VAT Number validation webservice&lt;/a&gt;'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'http://ec.europa.eu/taxation_customs/vies/lang.do?fromWhichPage=vieshome'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
    <span style="color: #009900;">&#41;</span>
  <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  parent<span style="color: #339933;">::</span>__construct<span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">null</span><span style="color: #339933;">,</span> <span style="color: #000088;">$options</span><span style="color: #339933;">,</span> <span style="color: #000088;">$messages</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>Ici on définit l&#8217;intitulé par défaut des champs contenant le numéro de TVA (vat_number) et le code du pays (country).<br />
Puis on définit les messages des différentes erreurs pouvant être générées :<br />
- mauvaise syntaxe<br />
- pays qui ne correspond pas<br />
- numéro invalide.</p>
<p>Voyons maintenant le doClean et la méthode appelant le webService :</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('p1123code82'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table width="100%" ><tr id="p112382"><td class="code" id="p1123code82"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> doClean<span style="color: #009900;">&#40;</span><span style="color: #000088;">$values</span><span style="color: #009900;">&#41;</span>
  <span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">null</span> <span style="color: #339933;">===</span> <span style="color: #000088;">$values</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
      <span style="color: #000088;">$values</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #990000;">is_array</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$values</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
      throw <span style="color: #000000; font-weight: bold;">new</span> InvalidArgumentException<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'You must pass an array parameter to the clean() method'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000088;">$vatnumber</span>  <span style="color: #339933;">=</span> <span style="color: #990000;">isset</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$values</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getOption</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'vat_number'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> ? <span style="color: #000088;">$values</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getOption</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'vat_number'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">:</span> <span style="color: #000000; font-weight: bold;">null</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$country</span> <span style="color: #339933;">=</span> <span style="color: #990000;">isset</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$values</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getOption</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'country'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> ? <span style="color: #000088;">$values</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getOption</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'country'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">:</span> <span style="color: #000000; font-weight: bold;">null</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">//on récupère la validité du numéro de TVA via le webService</span>
    <span style="color: #000088;">$valid</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">haleValidateVAT</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'vatnumber'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$vatnumber</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'country'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$country</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">//si le résultat n'est pas valide, on throw l'erreur correspondante</span>
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #000088;">$valid</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'result'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
      throw <span style="color: #000000; font-weight: bold;">new</span> sfValidatorError<span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">,</span> <span style="color: #000088;">$valid</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'error'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'value'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$vatnumber</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
      <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getOption</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'throw_global_error'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
      <span style="color: #009900;">&#123;</span>
        throw <span style="color: #000088;">$error</span><span style="color: #339933;">;</span>
      <span style="color: #009900;">&#125;</span>
&nbsp;
      <span style="color: #666666; font-style: italic;">//l'erreur s'applique sur le champ vat_number</span>
      throw <span style="color: #000000; font-weight: bold;">new</span> sfValidatorErrorSchema<span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">,</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getOption</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'vat_number'</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$error</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">//si valide, on retourne les valeurs</span>
    <span style="color: #b1b100;">return</span> <span style="color: #000088;">$values</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #009933; font-style: italic;">/**
   * vérifie la validité du numéro de TVA en prenant en compte le pays donné
   *
   * @param array $args
   * @return array
   */</span>
  protected <span style="color: #000000; font-weight: bold;">function</span> haleValidateVAT<span style="color: #009900;">&#40;</span><span style="color: #000088;">$args</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">''</span> <span style="color: #339933;">!=</span> <span style="color: #000088;">$args</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'vatnumber'</span><span style="color: #009900;">&#93;</span> <span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
      <span style="color: #666666; font-style: italic;">// on sérialize le numéro TVA</span>
      <span style="color: #000088;">$vat_number</span> 	<span style="color: #339933;">=</span> <span style="color: #990000;">str_replace</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">' '</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'.'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'-'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">','</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">', '</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">''</span><span style="color: #339933;">,</span> <span style="color: #000088;">$args</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'vatnumber'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #666666; font-style: italic;">// on récupère le code pays</span>
      <span style="color: #000088;">$countryCode</span> 	<span style="color: #339933;">=</span> <span style="color: #990000;">substr</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$vat_number</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">2</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #666666; font-style: italic;">//on récupère le numéro TVA</span>
      <span style="color: #000088;">$vatNumber</span> 		<span style="color: #339933;">=</span> <span style="color: #990000;">substr</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$vat_number</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">2</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
      <span style="color: #666666; font-style: italic;">//on vérifie la syntaxe du numéro</span>
      <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">strlen</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$countryCode</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">!=</span> <span style="color: #cc66cc;">2</span> <span style="color: #339933;">||</span> <span style="color: #990000;">is_numeric</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">substr</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$countryCode</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">0</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: #990000;">is_numeric</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">substr</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$countryCode</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">2</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
      <span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$error</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'result'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000000; font-weight: bold;">false</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'error'</span><span style="color: #339933;">=&gt;</span><span style="color: #0000ff;">'invalid_syntax'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #b1b100;">return</span> <span style="color: #000088;">$error</span><span style="color: #339933;">;</span>
      <span style="color: #009900;">&#125;</span>
&nbsp;
      <span style="color: #666666; font-style: italic;">//on vérifie que le pays correspond bien au pays indiqué dans le numéro de TVA</span>
      <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span> <span style="color: #000088;">$args</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'country'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">!=</span> <span style="color: #000088;">$countryCode</span> <span style="color: #009900;">&#41;</span>
      <span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$error</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'result'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000000; font-weight: bold;">false</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'error'</span><span style="color: #339933;">=&gt;</span><span style="color: #0000ff;">'invalid_country'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #b1b100;">return</span> <span style="color: #000088;">$error</span><span style="color: #339933;">;</span>
      <span style="color: #009900;">&#125;</span>
&nbsp;
      <span style="color: #666666; font-style: italic;">//appelle le webservice</span>
      <span style="color: #000088;">$client</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> SoapClient<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;http://ec.europa.eu/taxation_customs/vies/services/checkVatService.wsdl&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #000088;">$params</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'countryCode'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$countryCode</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'vatNumber'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$vatNumber</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #000088;">$result</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$client</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">checkVat</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$params</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
      <span style="color: #666666; font-style: italic;">//vérifie la validité et renvoie l'erreyr correspondante</span>
      <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span> <span style="color: #339933;">!</span><span style="color: #000088;">$result</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">valid</span> <span style="color: #009900;">&#41;</span>
      <span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$error</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'result'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000000; font-weight: bold;">false</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'error'</span><span style="color: #339933;">=&gt;</span><span style="color: #0000ff;">'invalid'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #b1b100;">return</span> <span style="color: #000088;">$error</span><span style="color: #339933;">;</span>
      <span style="color: #009900;">&#125;</span><span style="color: #b1b100;">else</span><span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">return</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'result'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000000; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #b1b100;">return</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'result'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000000; font-weight: bold;">false</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'error'</span><span style="color: #339933;">=&gt;</span><span style="color: #0000ff;">'required'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<h2>Testons</h2>
<p>Pour tester tout simplement, voici un petit formulaire contenant uniquement les champs pays et numéro de TVA :</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('p1123code83'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table width="100%" ><tr id="p112383"><td class="code" id="p1123code83"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">class</span> TestVATForm <span style="color: #000000; font-weight: bold;">extends</span> sfForm
<span style="color: #009900;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> configure<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;">widgetSchema</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'vat_number'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> sfWidgetFormInput<span style="color: #009900;">&#40;</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;">widgetSchema</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'country'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> sfWidgetFormInput<span style="color: #009900;">&#40;</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;">validatorSchema</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'vat_number'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> sfValidatorString<span style="color: #009900;">&#40;</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;">validatorSchema</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'country'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> sfValidatorString<span style="color: #009900;">&#40;</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;">validatorSchema</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setPostValidator</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> sfValidatorVAT<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</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;">widgetSchema</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setNameFormat</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'tva_form[%s]'</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>Voici l&#8217;action :</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('p1123code84'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table width="100%" ><tr id="p112384"><td class="code" id="p1123code84"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">class</span> defaultActions <span style="color: #000000; font-weight: bold;">extends</span> sfActions
<span style="color: #009900;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> executeTestTVA<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;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">form</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> TestVATForm<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$request</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">isMethod</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'post'</span><span style="color: #009900;">&#41;</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;">form</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">bind</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$request</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getParameter</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">form</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getName</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: #339933;">;</span>
&nbsp;
      <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">form</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">isValid</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</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;">getUser</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setFlash</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'notice'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'TVA ok'</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;">redirect</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'default/testTVA'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #009900;">&#125;</span><span style="color: #b1b100;">else</span><span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getUser</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setFlash</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'error'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'TVA ko'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span>
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>Et pour finir, voici la vue (toute bête) :</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('p1123code85'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table width="100%" ><tr id="p112385"><td class="code" id="p1123code85"><pre class="php" style="font-family:monospace;"><span style="color: #339933;">&lt;</span>form action<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;&lt;?php echo url_for('default/testTVA') ?&gt;&quot;</span> method<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;post&quot;</span><span style="color: #339933;">&gt;</span>
  <span style="color: #339933;">&lt;</span>table<span style="color: #339933;">&gt;</span>
    <span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #990000;">echo</span> <span style="color: #000088;">$form</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>td<span style="color: #339933;">&gt;&lt;/</span>td<span style="color: #339933;">&gt;</span>
      <span style="color: #339933;">&lt;</span>td<span style="color: #339933;">&gt;</span>
        <span style="color: #339933;">&lt;</span>input type<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;submit&quot;</span> value<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;Tester&quot;</span> <span style="color: #339933;">/&gt;</span>
      <span style="color: #339933;">&lt;/</span>td<span style="color: #339933;">&gt;</span>
    <span style="color: #339933;">&lt;/</span>tr<span style="color: #339933;">&gt;</span>
  <span style="color: #339933;">&lt;/</span>table<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;/</span>form<span style="color: #339933;">&gt;</span></pre></td></tr></table></div>

<p>Et voilà !</p>
<a href="http://www.addtoany.com/add_to/twitter?linkurl=http%3A%2F%2Fwww.lexik.fr%2Fblog%2Fsymfony%2Fsymfony%2Fun-validator-tva-bien-pratique-1123&amp;linkname=Valider%20un%20num%C3%A9ro%20de%20TVA%20intracommunautaire" title="Twitter" rel="nofollow" target="_blank"><img src="http://www.lexik.fr/blog/symfony/wp-content/plugins/add-to-any/icons/twitter.png" alt="Twitter"/></a> <a href="http://www.addtoany.com/add_to/facebook?linkurl=http%3A%2F%2Fwww.lexik.fr%2Fblog%2Fsymfony%2Fsymfony%2Fun-validator-tva-bien-pratique-1123&amp;linkname=Valider%20un%20num%C3%A9ro%20de%20TVA%20intracommunautaire" title="Facebook" rel="nofollow" target="_blank"><img src="http://www.lexik.fr/blog/symfony/wp-content/plugins/add-to-any/icons/facebook.png" alt="Facebook"/></a> <a href="http://www.addtoany.com/add_to/delicious?linkurl=http%3A%2F%2Fwww.lexik.fr%2Fblog%2Fsymfony%2Fsymfony%2Fun-validator-tva-bien-pratique-1123&amp;linkname=Valider%20un%20num%C3%A9ro%20de%20TVA%20intracommunautaire" title="Delicious" rel="nofollow" target="_blank"><img src="http://www.lexik.fr/blog/symfony/wp-content/plugins/add-to-any/icons/delicious.png" alt="Delicious"/></a> <a href="http://www.addtoany.com/add_to/viadeo?linkurl=http%3A%2F%2Fwww.lexik.fr%2Fblog%2Fsymfony%2Fsymfony%2Fun-validator-tva-bien-pratique-1123&amp;linkname=Valider%20un%20num%C3%A9ro%20de%20TVA%20intracommunautaire" title="Viadeo" rel="nofollow" target="_blank"><img src="http://www.lexik.fr/blog/symfony/wp-content/plugins/add-to-any/icons/viadeo.png" alt="Viadeo"/></a> <a href="http://www.addtoany.com/add_to/technorati_favorites?linkurl=http%3A%2F%2Fwww.lexik.fr%2Fblog%2Fsymfony%2Fsymfony%2Fun-validator-tva-bien-pratique-1123&amp;linkname=Valider%20un%20num%C3%A9ro%20de%20TVA%20intracommunautaire" title="Technorati Favorites" rel="nofollow" target="_blank"><img src="http://www.lexik.fr/blog/symfony/wp-content/plugins/add-to-any/icons/technorati.png" alt="Technorati Favorites"/></a> <a href="http://www.addtoany.com/add_to/netvibes_share?linkurl=http%3A%2F%2Fwww.lexik.fr%2Fblog%2Fsymfony%2Fsymfony%2Fun-validator-tva-bien-pratique-1123&amp;linkname=Valider%20un%20num%C3%A9ro%20de%20TVA%20intracommunautaire" title="Netvibes Share" rel="nofollow" target="_blank"><img src="http://www.lexik.fr/blog/symfony/wp-content/plugins/add-to-any/icons/netvibes.png" alt="Netvibes Share"/></a> <a href="http://www.addtoany.com/add_to/gmail?linkurl=http%3A%2F%2Fwww.lexik.fr%2Fblog%2Fsymfony%2Fsymfony%2Fun-validator-tva-bien-pratique-1123&amp;linkname=Valider%20un%20num%C3%A9ro%20de%20TVA%20intracommunautaire" title="Gmail" rel="nofollow" target="_blank"><img src="http://www.lexik.fr/blog/symfony/wp-content/plugins/add-to-any/icons/gmail.png" alt="Gmail"/></a> <a href="http://www.addtoany.com/add_to/yoolink?linkurl=http%3A%2F%2Fwww.lexik.fr%2Fblog%2Fsymfony%2Fsymfony%2Fun-validator-tva-bien-pratique-1123&amp;linkname=Valider%20un%20num%C3%A9ro%20de%20TVA%20intracommunautaire" title="Yoolink" rel="nofollow" target="_blank"><img src="http://www.lexik.fr/blog/symfony/wp-content/plugins/add-to-any/icons/yoolink.png" alt="Yoolink"/></a> <a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save?linkurl=http%3A%2F%2Fwww.lexik.fr%2Fblog%2Fsymfony%2Fsymfony%2Fun-validator-tva-bien-pratique-1123&amp;linkname=Valider%20un%20num%C3%A9ro%20de%20TVA%20intracommunautaire">Partager cet article</a>]]></content:encoded>
			<wfw:commentRss>http://www.lexik.fr/blog/symfony/symfony/un-validator-tva-bien-pratique-1123/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Doctrine_Collection et hydration hiérarchisée.</title>
		<link>http://www.lexik.fr/blog/symfony/symfony/doctrine_collection-et-hydration-hierarchisee-1109</link>
		<comments>http://www.lexik.fr/blog/symfony/symfony/doctrine_collection-et-hydration-hierarchisee-1109#comments</comments>
		<pubDate>Wed, 07 Apr 2010 07:51:10 +0000</pubDate>
		<dc:creator>yoye</dc:creator>
				<category><![CDATA[1.4.x]]></category>
		<category><![CDATA[Symfony]]></category>
		<category><![CDATA[doctrine]]></category>
		<category><![CDATA[Doctrine_Collection]]></category>
		<category><![CDATA[Doctrine_Hydrator]]></category>
		<category><![CDATA[helper]]></category>
		<category><![CDATA[nestedset]]></category>
		<category><![CDATA[toHierarchy]]></category>
		<category><![CDATA[treeTable]]></category>

		<guid isPermaLink="false">http://www.lexik.fr/blog/symfony/?p=1109</guid>
		<description><![CDATA[En ce moment nous sommes en train de travailler sur un site de collectionneurs et nous avons le plaisir de tomber sur quelques cas intéressants au niveau développement ce qui n&#8217;est pas pour nous déplaire. Ce site contient une grosse partie sur la gestion des zones géographiques, on a décidé d&#8217;utiliser le behavior Nestedset ce [...]]]></description>
			<content:encoded><![CDATA[<p>En ce moment nous sommes en train de travailler sur un site de collectionneurs et nous avons le plaisir de tomber sur quelques cas intéressants au niveau développement ce qui n&#8217;est pas pour nous déplaire. Ce site contient une grosse partie sur la gestion des zones géographiques, on a décidé d&#8217;utiliser le behavior Nestedset ce qui nous permet de gérer facilement les différentes arborescences : &laquo;&nbsp;Continent > Pays > état&nbsp;&raquo; ou &laquo;&nbsp;Continent > Pays > Région&nbsp;&raquo; ou encore &laquo;&nbsp;Continent > Pays / Ancien Pays&nbsp;&raquo;.<br />
<span id="more-1109"></span><br />
Je ne vais pas faire un point sur le nested, la <a href="http://www.lexik.fr/blog/symfony/symfony/nested-set-doctrine-135">gestion par arborescence</a> a déjà été abordée sur ce blog. En revanche je vais vous faire part d&#8217;une découverte récente. La possibilité d&#8217;hydrater directement une collection sous sa forme d&#8217;arbre.</p>
<p>Pour le listing de ces zones j&#8217;ai donc choisi d&#8217;utiliser un plugin jQuery : <a href="http://blog.cubicphuse.nl/2008/11/12/jquery-treetable-2-0">treeTable</a>. Comme son nom l&#8217;indique ce plugin va nous permettre une organisation d&#8217;un arbre dans une table HTML. Il faut indiquer en &laquo;&nbsp;id&nbsp;&raquo; de la balise<br />
<tr> de notre tableau un identifiant de notre objet (ex: id=&nbsp;&raquo;node-1&#8243;) et à l&#8217;attribut &laquo;&nbsp;class&nbsp;&raquo; on signale de qui le noeud est l&#8217;enfant (ex: child-of-node-1 sera le fils du noeud 1).</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('p1109code90'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table width="100%" ><tr id="p110990"><td class="code" id="p1109code90"><pre class="php" style="font-family:monospace;">  <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;content-box-content&quot;</span><span style="color: #339933;">&gt;</span>
    <span style="color: #339933;">&lt;</span>table id<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;tree&quot;</span><span style="color: #339933;">&gt;</span>
      <span style="color: #339933;">&lt;</span>tbody<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;">$areas</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$area</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: #000088;">$parent</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$area</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getNode</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getParent</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;">&lt;</span>tr id<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;node-&lt;?php echo <span style="color: #006699; font-weight: bold;">$area-&gt;getId</span>() ?&gt;&quot;</span> <span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #990000;">echo</span> <span style="color: #000088;">$parent</span> ? <span style="color: #0000ff;">'class=&quot;child-of-node-'</span><span style="color: #339933;">.</span><span style="color: #000088;">$parent</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getId</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">'&quot;'</span> <span style="color: #339933;">:</span> <span style="color: #0000ff;">''</span> <span style="color: #000000; font-weight: bold;">?&gt;</span><span style="color: #339933;">&gt;</span>
          <span style="color: #339933;">&lt;</span>td<span style="color: #339933;">&gt;</span><span style="color: #000000; font-weight: bold;">&lt;?</span>php <span style="color: #990000;">echo</span> <span style="color: #000088;">$area</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getName</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;">&lt;/</span>td<span style="color: #339933;">&gt;</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: #990000;">echo</span> link_to<span style="color: #009900;">&#40;</span>__<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'module_area_list_action_add_children'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'area_new'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$area</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getType</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'query_string'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'parent_id='</span><span style="color: #339933;">.</span><span style="color: #000088;">$area</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getId</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>td<span style="color: #339933;">&gt;</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: #990000;">echo</span> link_to<span style="color: #009900;">&#40;</span>__<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'text_action_delete'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'@area_delete?id='</span><span style="color: #339933;">.</span><span style="color: #000088;">$area</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getId</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'method'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'delete'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'confirm'</span> <span style="color: #339933;">=&gt;</span> __<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'text_action_delete_confirm'</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>td<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;">endforeach</span> <span style="color: #000000; font-weight: bold;">?&gt;</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>
  <span style="color: #339933;">&lt;/</span>div<span style="color: #339933;">&gt;</span></pre></td></tr></table></div>

<p>Voici le résultat : </p>
<p><img src="http://www.lexik.fr/blog/symfony/wp-content/uploads/2010/04/treeTableArea1.png" alt="treeTableArea" title="treeTableArea" width="975" height="528" class="alignnone size-full wp-image-1115" /></p>
<p>Le rendu correspond à ce que souhaite mon client, lorsque l&#8217;on clique sur une branche, par exemple Afrique ou Europe les lignes suivantes se déplient et laissent apparaitre les enfants. Tout va pour le mieux dans le meilleur des mondes, simplement quand on regarde de plus près on s&#8217;aperçoit que pour un listing de simplement 12 zones je fais 29 requêtes, que va t&#8217;il se passer lorsque je vais avoir toutes mes zones renseignées, la réponse est : +700 requêtes. Le souci vient de la méthode <a href="http://www.doctrine-project.org/Doctrine_Node_NestedSet/1_2#method_getparent">getNode()->getParent()</a> qui exécute une nouvelle requête à chaque appel. En effet pour lier une ligne à son parent j&#8217;ai besoin de connaître l&#8217;ID du père.</p>
<p>Pour remédier à cela je vais donc utiliser l&#8217;Hydratation hiérarchisée, que l&#8217;on peut créer à l&#8217;aide d&#8217;une Doctrine_Query qu&#8217;on exécutera comme suit :</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('p1109code91'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table width="100%" ><tr id="p110991"><td class="code" id="p1109code91"><pre class="php" style="font-family:monospace;">Doctrine_Core<span style="color: #339933;">::</span><span style="color: #004000;">getTable</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Area'</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;">'a'</span><span style="color: #009900;">&#41;</span>
  execute<span style="color: #009900;">&#40;</span><span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> Doctrine_Core<span style="color: #339933;">::</span><span style="color: #004000;">HYDRATE_RECORD_HIERARCHY</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>Sinon on peut directement utiliser la méthode toHierarchy() sur notre collection, qui nous hydratera directement cette dernière. Chaque élément contenant alors des enfants aura une clé __children, qui n&#8217;est autre qu&#8217;un tableau des éléments enfants.</p>
<p>Je vais donc remplacer mon code et utiliser un helper pour le rendu d&#8217;une ligne ce qui permettra de faire de la récursivité plus facilement dans mon code.</p>
<p>Je crée donc l&#8217;helper suivant :</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('p1109code92'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table width="100%" ><tr id="p110992"><td class="code" id="p1109code92"><pre class="php" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">// apps/backend/lib/helper/AreaHierarchyHelper.php</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #009933; font-style: italic;">/**
 * Permet l'affichage en arbre dans le listing des zones
 *
 * @param Area $area
 * @param int $parent
 * @return string
 */</span>
<span style="color: #000000; font-weight: bold;">function</span> render_row<span style="color: #009900;">&#40;</span><span style="color: #000088;">$area</span><span style="color: #339933;">,</span> <span style="color: #000088;">$parent</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
&nbsp;
  <span style="color: #666666; font-style: italic;">// Formatage du HTML</span>
  <span style="color: #000088;">$html</span> <span style="color: #339933;">=</span> <span style="color: #990000;">sprintf</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'
&lt;tr id=&quot;node-%s&quot;%s&gt;
  &lt;td&gt;%s&lt;/td&gt;
  &lt;td&gt;%s&lt;/td&gt;
  &lt;td&gt;%s&lt;/td&gt;
  &lt;td&gt;%s&lt;/td&gt;
  &lt;td&gt;%s&lt;/td&gt;
&lt;/tr&gt;'</span><span style="color: #339933;">,</span>
      <span style="color: #000088;">$area</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getid</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">null</span> <span style="color: #339933;">===</span> <span style="color: #000088;">$parent</span> ? <span style="color: #0000ff;">''</span> <span style="color: #339933;">:</span> <span style="color: #990000;">sprintf</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">' class=&quot;child-of-node-%s&quot;'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$parent</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
      <span style="color: #000088;">$area</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getName</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
      <span style="color: #0000ff;">'ID : '</span><span style="color: #339933;">.</span><span style="color: #000088;">$area</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getId</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
      link_to<span style="color: #009900;">&#40;</span>__<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'module_area_list_action_add_children'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'area_new'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$area</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getType</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'query_string'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'parent_id='</span><span style="color: #339933;">.</span><span style="color: #000088;">$area</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getId</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: #339933;">,</span>
      link_to<span style="color: #009900;">&#40;</span>__<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'text_action_edit'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'area_edit'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$area</span><span style="color: #339933;">,</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'class'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'area-edit'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
      link_to<span style="color: #009900;">&#40;</span>__<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'text_action_delete'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'@area_delete?id='</span><span style="color: #339933;">.</span><span style="color: #000088;">$area</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getId</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'method'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'delete'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'confirm'</span> <span style="color: #339933;">=&gt;</span> __<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'text_action_delete_confirm'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #666666; font-style: italic;">// Récursivité pour créer les lignes des fils</span>
  <span style="color: #b1b100;">foreach</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$area</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">get</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'__children'</span><span style="color: #009900;">&#41;</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$child</span><span style="color: #009900;">&#41;</span>
  <span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$html</span> <span style="color: #339933;">.=</span> render_row<span style="color: #009900;">&#40;</span><span style="color: #000088;">$child</span><span style="color: #339933;">,</span> <span style="color: #000088;">$area</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getid</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: #009900;">&#125;</span>
&nbsp;
  <span style="color: #b1b100;">return</span> <span style="color: #000088;">$html</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>Désormais sur ma page, il me suffit de charger mon helper et d&#8217;écrire le code suivant :</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('p1109code93'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table width="100%" ><tr id="p110993"><td class="code" id="p1109code93"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span> use_helper<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'AreaHierarchy'</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>
&nbsp;
<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;content-box&quot;</span><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;content-box-header&quot;</span><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;content-box-content&quot;</span><span style="color: #339933;">&gt;</span>
    <span style="color: #339933;">&lt;</span>table id<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;tree&quot;</span><span style="color: #339933;">&gt;</span>
      <span style="color: #339933;">&lt;</span>tbody<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;">$areas</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">toHierarchy</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$area</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: #990000;">echo</span> render_row<span style="color: #009900;">&#40;</span><span style="color: #000088;">$area</span><span style="color: #009900;">&#41;</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: #000000; font-weight: bold;">?&gt;</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>
  <span style="color: #339933;">&lt;/</span>div<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;/</span>div<span style="color: #339933;">&gt;</span></pre></td></tr></table></div>

<p>Le résultat est exactement le même au niveau de l&#8217;affichage en revanche je passe désormais à 5 requêtes et sur les données définitives,  j&#8217;obtiens toujours 5 requêtes et ce quelque soit le nombre de résultat dans ma collection. J&#8217;ai donc bénéficié des avantages de Doctrine et surtout des Doctrine_Hydrator pour diminuer le nombre de requêtes. </p>
<a href="http://www.addtoany.com/add_to/twitter?linkurl=http%3A%2F%2Fwww.lexik.fr%2Fblog%2Fsymfony%2Fsymfony%2Fdoctrine_collection-et-hydration-hierarchisee-1109&amp;linkname=Doctrine_Collection%20et%20hydration%20hi%C3%A9rarchis%C3%A9e." title="Twitter" rel="nofollow" target="_blank"><img src="http://www.lexik.fr/blog/symfony/wp-content/plugins/add-to-any/icons/twitter.png" alt="Twitter"/></a> <a href="http://www.addtoany.com/add_to/facebook?linkurl=http%3A%2F%2Fwww.lexik.fr%2Fblog%2Fsymfony%2Fsymfony%2Fdoctrine_collection-et-hydration-hierarchisee-1109&amp;linkname=Doctrine_Collection%20et%20hydration%20hi%C3%A9rarchis%C3%A9e." title="Facebook" rel="nofollow" target="_blank"><img src="http://www.lexik.fr/blog/symfony/wp-content/plugins/add-to-any/icons/facebook.png" alt="Facebook"/></a> <a href="http://www.addtoany.com/add_to/delicious?linkurl=http%3A%2F%2Fwww.lexik.fr%2Fblog%2Fsymfony%2Fsymfony%2Fdoctrine_collection-et-hydration-hierarchisee-1109&amp;linkname=Doctrine_Collection%20et%20hydration%20hi%C3%A9rarchis%C3%A9e." title="Delicious" rel="nofollow" target="_blank"><img src="http://www.lexik.fr/blog/symfony/wp-content/plugins/add-to-any/icons/delicious.png" alt="Delicious"/></a> <a href="http://www.addtoany.com/add_to/viadeo?linkurl=http%3A%2F%2Fwww.lexik.fr%2Fblog%2Fsymfony%2Fsymfony%2Fdoctrine_collection-et-hydration-hierarchisee-1109&amp;linkname=Doctrine_Collection%20et%20hydration%20hi%C3%A9rarchis%C3%A9e." title="Viadeo" rel="nofollow" target="_blank"><img src="http://www.lexik.fr/blog/symfony/wp-content/plugins/add-to-any/icons/viadeo.png" alt="Viadeo"/></a> <a href="http://www.addtoany.com/add_to/technorati_favorites?linkurl=http%3A%2F%2Fwww.lexik.fr%2Fblog%2Fsymfony%2Fsymfony%2Fdoctrine_collection-et-hydration-hierarchisee-1109&amp;linkname=Doctrine_Collection%20et%20hydration%20hi%C3%A9rarchis%C3%A9e." title="Technorati Favorites" rel="nofollow" target="_blank"><img src="http://www.lexik.fr/blog/symfony/wp-content/plugins/add-to-any/icons/technorati.png" alt="Technorati Favorites"/></a> <a href="http://www.addtoany.com/add_to/netvibes_share?linkurl=http%3A%2F%2Fwww.lexik.fr%2Fblog%2Fsymfony%2Fsymfony%2Fdoctrine_collection-et-hydration-hierarchisee-1109&amp;linkname=Doctrine_Collection%20et%20hydration%20hi%C3%A9rarchis%C3%A9e." title="Netvibes Share" rel="nofollow" target="_blank"><img src="http://www.lexik.fr/blog/symfony/wp-content/plugins/add-to-any/icons/netvibes.png" alt="Netvibes Share"/></a> <a href="http://www.addtoany.com/add_to/gmail?linkurl=http%3A%2F%2Fwww.lexik.fr%2Fblog%2Fsymfony%2Fsymfony%2Fdoctrine_collection-et-hydration-hierarchisee-1109&amp;linkname=Doctrine_Collection%20et%20hydration%20hi%C3%A9rarchis%C3%A9e." title="Gmail" rel="nofollow" target="_blank"><img src="http://www.lexik.fr/blog/symfony/wp-content/plugins/add-to-any/icons/gmail.png" alt="Gmail"/></a> <a href="http://www.addtoany.com/add_to/yoolink?linkurl=http%3A%2F%2Fwww.lexik.fr%2Fblog%2Fsymfony%2Fsymfony%2Fdoctrine_collection-et-hydration-hierarchisee-1109&amp;linkname=Doctrine_Collection%20et%20hydration%20hi%C3%A9rarchis%C3%A9e." title="Yoolink" rel="nofollow" target="_blank"><img src="http://www.lexik.fr/blog/symfony/wp-content/plugins/add-to-any/icons/yoolink.png" alt="Yoolink"/></a> <a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save?linkurl=http%3A%2F%2Fwww.lexik.fr%2Fblog%2Fsymfony%2Fsymfony%2Fdoctrine_collection-et-hydration-hierarchisee-1109&amp;linkname=Doctrine_Collection%20et%20hydration%20hi%C3%A9rarchis%C3%A9e.">Partager cet article</a>]]></content:encoded>
			<wfw:commentRss>http://www.lexik.fr/blog/symfony/symfony/doctrine_collection-et-hydration-hierarchisee-1109/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Retour sur le plugin Commentaires, partie administration</title>
		<link>http://www.lexik.fr/blog/symfony/non-classe/retour-sur-le-plugin-commentaires-partie-administration-1087</link>
		<comments>http://www.lexik.fr/blog/symfony/non-classe/retour-sur-le-plugin-commentaires-partie-administration-1087#comments</comments>
		<pubDate>Wed, 31 Mar 2010 08:39:13 +0000</pubDate>
		<dc:creator>Nikaw</dc:creator>
				<category><![CDATA[1.2.x]]></category>
		<category><![CDATA[1.3.x]]></category>
		<category><![CDATA[1.4.x]]></category>
		<category><![CDATA[Admingenerator 1.2.x]]></category>
		<category><![CDATA[Admingenerator 1.3.x]]></category>
		<category><![CDATA[Admingenerator 1.4.x]]></category>
		<category><![CDATA[Non classé]]></category>
		<category><![CDATA[Symfony]]></category>
		<category><![CDATA[admin]]></category>
		<category><![CDATA[backend]]></category>
		<category><![CDATA[commentable]]></category>
		<category><![CDATA[plugin]]></category>

		<guid isPermaLink="false">http://www.lexik.fr/blog/symfony/?p=1087</guid>
		<description><![CDATA[J&#8217;espère que vous vous souvenez du post concernant le plugin / behavior sfLexikDoctrineActAsCommentablePlugin. Je vous avais promis une deuxième partie sur l&#8217;administration des commentaires, et bien après quelques mois d&#8217;absence me revoilà pour tenir ma promesse. Tout d&#8217;abord un peu de configuration, puis nous verront quelles sont l&#8217;utilité et la problématique principales de cette partie. [...]]]></description>
			<content:encoded><![CDATA[<p>J&#8217;espère que vous vous souvenez du <a href="http://www.lexik.fr/blog/symfony/symfony/sflexikdoctrineactascommentableplugin-ou-comment-rendre-des-objets-commentables-via-un-behavior-plugin-1002">post concernant le plugin / behavior sfLexikDoctrineActAsCommentablePlugin</a>.<br />
Je vous avais promis une deuxième partie sur l&#8217;administration des commentaires, et bien après quelques mois d&#8217;absence me revoilà pour tenir ma promesse.<br />
<span id="more-1087"></span><br />
Tout d&#8217;abord un peu de configuration, puis nous verront quelles sont l&#8217;utilité et la problématique principales de cette partie.<br />
Nous verrons l&#8217;administration des commentaires dans leur ensemble, puis la gestion des commentaires depuis l&#8217;objet porteur.</p>
<p><strong><em>Configuration</em></strong><br />
Tout d&#8217;abord il ne faut pas oublier de charger le module dans les settings.<br />
apps/backend/config/settings.yml :</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('p1087code108'); return false;">View Code</a> YAML</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table width="100%" ><tr id="p1087108"><td class="code" id="p1087code108"><pre class="yaml" style="font-family:monospace;">all:
  .settings:
    enabled_modules: [..., comment]</pre></td></tr></table></div>

<p><strong><em>Problématique</em></strong></p>
<p>On veut ici pouvoir gérer facilement et rapidement les commentaires déposés par les utilisateurs.<br />
Une des problématiques sera donc la façon de lister les commentaires sur l&#8217;index.<br />
J&#8217;ai choisi par défaut de regrouper les commentaires par type d&#8217;objet, puis par objet, puis par date de modification.</p>
<p>Exemple : je liste tous les commentaires déposé sur les objets Message, je les regroupe par message, et je les classe par date de modification (la plupart du temps, un commentaire ne pouvant être modifié, on aura une concordance entre la date d&#8217;ajout et la date de modification).</p>
<p>Pour ceci, je spécifie dans le fichier de génération la méthode utilisée pour récupérer les commentaires :<br />
plugins/sfLexikDoctrineActAsCommentablePlugin/module/comment/config/generator.yml :</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('p1087code109'); return false;">View Code</a> YAML</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table width="100%" ><tr id="p1087109"><td class="code" id="p1087code109"><pre class="yaml" style="font-family:monospace;">generator:
  config:
    list:
      table_method: retrieveBackendCommentList</pre></td></tr></table></div>

<p>La méthode en question :<br />
plugins/sfLexikDoctrineActAsCommentablePlugin/lib/model/doctrine/PluginCommentTable.class.php :</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('p1087code110'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table width="100%" ><tr id="p1087110"><td class="code" id="p1087code110"><pre class="php" style="font-family:monospace;">  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> retrieveBackendCommentList<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
  <span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$query</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">createQuery</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'c'</span><span style="color: #009900;">&#41;</span>
            <span style="color: #339933;">-&gt;</span><span style="color: #004000;">orderBy</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'c.parent_class_name ASC'</span><span style="color: #009900;">&#41;</span>
            <span style="color: #339933;">-&gt;</span><span style="color: #004000;">addOrderBy</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'c.parent_id ASC'</span><span style="color: #009900;">&#41;</span>
            <span style="color: #339933;">-&gt;</span><span style="color: #004000;">addOrderBy</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'c.updated_at DESC'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #b1b100;">return</span> <span style="color: #000088;">$query</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>Nous allons maintenant nous pencher sur les différents traitements que je propose.<br />
Il s&#8217;agit ici de publier / censurer, marquer comme spam / non spam, nettoyer les spams, et bien sûr supprimer des commentaires.</p>
<p>Tout d&#8217;abord je vous donne la configuration que j&#8217;ai choisie pour l&#8217;index, évidemment vous pouvez choisir de la changer, elle représente mes préférences rapportées au projet pour lequel j&#8217;ai créé ce plugin.<br />
plugins/sfLexikDoctrineActAsCommentablePlugin/module/comment/config/generator.yml :</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('p1087code111'); return false;">View Code</a> YAML</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table width="100%" ><tr id="p1087111"><td class="code" id="p1087code111"><pre class="yaml" style="font-family:monospace;">generator:
  class: sfDoctrineGenerator
  param:
    model_class:           Comment
    theme:                 admin
    non_verbose_templates: true
    with_show:             false
    singular:              ~
    plural:                ~
    route_prefix:          comment
    with_doctrine_route:     1
&nbsp;
    config:
      actions: ~
      fields:
      list:
        table_method: retrieveBackendCommentList
        display: [author_name, author_mail, text, is_published, is_spam]
        fields:
          author_name: {label: 'Nom auteur'}
        actions:
          _delete: ~
          CleanSpam: {label: 'Nettoyer les spams'}
        object_actions:
          Publish: {label: 'Publier'}
          UnPublish: {label: 'Censurer'}
          MakeSpam: {label: 'Spam !'}
          UnmakeSpam: {label: 'Pas spam'}
          _delete: ~
        batch_actions:
          _delete: ~
          Publish: {label: 'Publier'}
          UnPublish: {label: 'Censurer'}
          MakeSpam: {label: 'Marquer comme spams'}
          UnmakeSpam: {label: 'Retirer des spams'}
      filter: ~
      form: ~
      edit: ~
      new:     ~</pre></td></tr></table></div>

<p>Laissez moi maintenant vous décrire rapidement chaque méthode :<br />
<strong>Publish: {label: &#8216;Publier&#8217;}</strong><br />
permet d&#8217;afficher un commentaire</p>
<p><strong>UnPublish: {label: &#8216;Censurer&#8217;}</strong><br />
permet de censurer un commentaire, c&#8217;est à dire de bloquer sa publication</p>
<p><strong>MakeSpam: {label: &#8216;Spam !&#8217;}</strong><br />
permet de marquer un commentaire en tant que spam</p>
<p><strong>UnmakeSpam: {label: &#8216;Pas spam&#8217;}</strong><br />
permet de marquer un commentaire en tant que non spam</p>
<p>Ces méthodes dans la &laquo;&nbsp;list&nbsp;&raquo; s&#8217;appliquent sur un seul commentaire, et dans le &laquo;&nbsp;batch&nbsp;&raquo; sur un groupe de commentaires sélectionnés.</p>
<p>La méthode <strong>CleanSpam: {label: &#8216;Nettoyer les spams&#8217;}</strong> quant à elle permet de supprimer d&#8217;un coup tous les commentaires notifiés comme étant des spams.</p>
<p>Voici le code des méthodes ci-dessus :<br />
plugins/sfLexikDoctrineActAsCommentablePlugin/modules/comment/actions/actions.class.php :</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('p1087code112'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table width="100%" ><tr id="p1087112"><td class="code" id="p1087code112"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> executeListCleanSpam<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;">$nb</span> <span style="color: #339933;">=</span> Doctrine<span style="color: #339933;">::</span><span style="color: #004000;">getTable</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Comment'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">cleanSpam</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$nb</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;">getUser</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setFlash</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'notice'</span><span style="color: #339933;">,</span> <span style="color: #990000;">sprintf</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'%d spam'</span><span style="color: #339933;">.</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$nb</span> <span style="color: #339933;">&gt;</span> <span style="color: #cc66cc;">1</span> ? <span style="color: #0000ff;">'s'</span> <span style="color: #339933;">:</span> <span style="color: #0000ff;">''</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">' supprimé'</span><span style="color: #339933;">.</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$nb</span> <span style="color: #339933;">&gt;</span> <span style="color: #cc66cc;">1</span> ? <span style="color: #0000ff;">'s'</span> <span style="color: #339933;">:</span> <span style="color: #0000ff;">''</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$nb</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: #b1b100;">else</span>
    <span style="color: #009900;">&#123;</span>
      <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getUser</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setFlash</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'notice'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'Pas de spams.'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">redirect</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'@comment'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> executeListPublish<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;">$page</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$request</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getParameter</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: #339933;">;</span>
&nbsp;
    <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">comment</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getRoute</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getObject</span><span style="color: #009900;">&#40;</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;">comment</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setIsPublished</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">true</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;">comment</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">save</span><span style="color: #009900;">&#40;</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;">redirect</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'@comment?page='</span><span style="color: #339933;">.</span><span style="color: #000088;">$page</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> executeListUnPublish<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;">$page</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$request</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getParameter</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: #339933;">;</span>
&nbsp;
    <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">comment</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getRoute</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getObject</span><span style="color: #009900;">&#40;</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;">comment</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setIsPublished</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">false</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;">comment</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">save</span><span style="color: #009900;">&#40;</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;">redirect</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'@comment?page='</span><span style="color: #339933;">.</span><span style="color: #000088;">$page</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> executeListMakeSpam<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;">$page</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$request</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getParameter</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: #339933;">;</span>
&nbsp;
    <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">comment</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getRoute</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getObject</span><span style="color: #009900;">&#40;</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;">comment</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setIsSpam</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">true</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;">comment</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">save</span><span style="color: #009900;">&#40;</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;">redirect</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'@comment?page='</span><span style="color: #339933;">.</span><span style="color: #000088;">$page</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> executeListUnmakeSpam<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;">$page</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$request</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getParameter</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: #339933;">;</span>
&nbsp;
    <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">comment</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getRoute</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getObject</span><span style="color: #009900;">&#40;</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;">comment</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setIsSpam</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">false</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;">comment</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">save</span><span style="color: #009900;">&#40;</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;">redirect</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'@comment?page='</span><span style="color: #339933;">.</span><span style="color: #000088;">$page</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> executeBatchPublish<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;">$page</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$request</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getParameter</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: #339933;">;</span>
&nbsp;
    <span style="color: #000088;">$ids</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$request</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getParameter</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'ids'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">foreach</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$ids</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$id</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;">comment</span> <span style="color: #339933;">=</span> Doctrine<span style="color: #339933;">::</span><span style="color: #004000;">getTable</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Comment'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">find</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$id</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;">comment</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setIsPublished</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">true</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;">comment</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">save</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">redirect</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'@comment?page='</span><span style="color: #339933;">.</span><span style="color: #000088;">$page</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> executeBatchUnPublish<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;">$page</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$request</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getParameter</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: #339933;">;</span>
&nbsp;
    <span style="color: #000088;">$ids</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$request</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getParameter</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'ids'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">foreach</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$ids</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$id</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;">comment</span> <span style="color: #339933;">=</span> Doctrine<span style="color: #339933;">::</span><span style="color: #004000;">getTable</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Comment'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">find</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$id</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;">comment</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setIsPublished</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">false</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;">comment</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">save</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">redirect</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'@comment?page='</span><span style="color: #339933;">.</span><span style="color: #000088;">$page</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> executeBatchMakeSpam<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;">$page</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$request</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getParameter</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: #339933;">;</span>
&nbsp;
    <span style="color: #000088;">$ids</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$request</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getParameter</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'ids'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">foreach</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$ids</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$id</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;">comment</span> <span style="color: #339933;">=</span> Doctrine<span style="color: #339933;">::</span><span style="color: #004000;">getTable</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Comment'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">find</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$id</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;">comment</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setIsSpam</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">true</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;">comment</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">save</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">redirect</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'@comment?page='</span><span style="color: #339933;">.</span><span style="color: #000088;">$page</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> executeBatchUnmakeSpam<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;">$page</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$request</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getParameter</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: #339933;">;</span>
&nbsp;
    <span style="color: #000088;">$ids</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$request</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getParameter</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'ids'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">foreach</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$ids</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$id</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;">comment</span> <span style="color: #339933;">=</span> Doctrine<span style="color: #339933;">::</span><span style="color: #004000;">getTable</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Comment'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">find</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$id</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;">comment</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setIsSpam</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">false</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;">comment</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">save</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">redirect</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'@comment?page='</span><span style="color: #339933;">.</span><span style="color: #000088;">$page</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>et la méthode cleanSpam() dans le modèle :<br />
plugins/sfLexikDoctrineActAsCommentablePlugin/lib/model/doctrine/PluginCommentTable.class.php :</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('p1087code113'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table width="100%" ><tr id="p1087113"><td class="code" id="p1087code113"><pre class="php" style="font-family:monospace;">  <span style="color: #009933; font-style: italic;">/**
   * supprime tous les commentaires classifiés comme spam et retourne le nombre d'enregistrements supprimés
   *
   * @return int
   */</span>
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> cleanSpam<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
  <span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$q</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">createQuery</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'c'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">delete</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">where</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'c.is_spam = ?'</span><span style="color: #339933;">,</span> <span style="color: #000000; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #b1b100;">return</span> <span style="color: #000088;">$q</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">execute</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>Je n&#8217;ai pas touché au filtre, mais chacun peut décider que tel ou tel champ sera utile ou pas, ça dépendra très souvent du projet.</p>
<p>Donc voilà pour la première étape. Jusque là on peut afficher et traiter les commentaires un par un ou par lots, directement depuis leur index.</p>
<p>Voyons maintenant comment ajouter la gestion des commentaires depuis les objets porteurs.<br />
Attention, cette partie est une nouveauté par rapport au premier post, voici les nouvelles sources : plugin.</p>
<p>Tout d&#8217;abord il faut ajouter dans l&#8217;édition de l&#8217;objet porteur le partial qui permettra de gérer les commentaires.<br />
Pour éviter de devoir dupliquer le même template pour chaque type d&#8217;objet, on va créer un partial dans le module comment du plugin puis l&#8217;appeler dans le partial de l&#8217;objet porteur :<br />
/home/lexik/sfprojects/kelsms/plugins/sfLexikDoctrineActAsCommentablePlugin/modules/comment/templates/_backend_comments_list.php :</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('p1087code114'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table width="100%" ><tr id="p1087114"><td class="code" id="p1087code114"><pre class="php" style="font-family:monospace;"><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;container_16&quot;</span><span style="color: #339933;">&gt;</span>
  Commentaires <span style="color: #339933;">:</span>
<span style="color: #339933;">&lt;</span>ul <span style="color: #000000; font-weight: bold;">class</span><span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;order&quot;</span><span style="color: #339933;">&gt;</span>
    <span style="color: #000088;">$comment</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>li id<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;&lt;?php echo <span style="color: #006699; font-weight: bold;">$comment-&gt;getId</span>() ?&gt;&quot;</span> <span style="color: #000000; font-weight: bold;">class</span><span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;row_&lt;?php echo <span style="color: #006699; font-weight: bold;">$key</span>%2 ?&gt;&quot;</span><span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;</span>div id<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;show_&lt;?php echo <span style="color: #006699; font-weight: bold;">$comment-&gt;getId</span>() ?&gt;&quot;</span> <span style="color: #000000; font-weight: bold;">class</span><span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;grid_10&quot;</span><span style="color: #339933;">&gt;</span>
          <span style="color: #000088;">$comment</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>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;grid_4&quot;</span><span style="color: #339933;">&gt;</span>
          <span style="color: #000088;">$comment</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>div<span style="color: #339933;">&gt;&lt;/</span>li<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;/</span>ul<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>script type<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;text/javascript&quot;</span><span style="color: #339933;">&gt;&lt;!--</span>mce<span style="color: #339933;">:</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">--&gt;&lt;/</span>script<span style="color: #339933;">&gt;</span></pre></td></tr></table></div>

<p>Il sera appelé de la manière suivant dans le partial de notre objet, par exemple ici un Message, grâce à la configuration du generator.yml :</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('p1087code115'); return false;">View Code</a> YAML</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table width="100%" ><tr id="p1087115"><td class="code" id="p1087code115"><pre class="yaml" style="font-family:monospace;">generator:
  config:
      edit:
        display: [..., _handle_comments]</pre></td></tr></table></div>

<p>apps/backend/modules/message/template/_handle_comments.php :</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('p1087code116'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table width="100%" ><tr id="p1087116"><td class="code" id="p1087code116"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$form</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getObject</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">findComments</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></pre></td></tr></table></div>

<p>Bref.<br />
Dans le partial _backend_comments_list.php on remarque 3 choses :<br />
1) l&#8217;appel du partial backend_show<br />
2) l&#8217;appel du partial backend_comment_actions<br />
3) les fonctions javascript</p>
<p>1) le partial backend_show affiche tout simplement le contenu du commentaire :<br />
_backend_show.php</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('p1087code117'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table width="100%" ><tr id="p1087117"><td class="code" id="p1087code117"><pre class="php" style="font-family:monospace;"><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;show_comment_class&quot;</span><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;infos_class&quot;</span><span style="color: #339933;">&gt;</span>
    <span style="color: #339933;">&lt;</span>span <span style="color: #000000; font-weight: bold;">class</span><span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;author_name_class&quot;</span><span style="color: #339933;">&gt;</span>
      getAuthorName<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;">&lt;/</span>span<span style="color: #339933;">&gt;</span>
&nbsp;
    <span style="color: #339933;">&lt;</span>span <span style="color: #000000; font-weight: bold;">class</span><span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;time_and_date_class&quot;</span><span style="color: #339933;">&gt;</span>
      <span style="color: #339933;">-</span> le <span style="color: #339933;">&lt;</span>span <span style="color: #000000; font-weight: bold;">class</span><span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;date_class&quot;</span><span style="color: #339933;">&gt;</span>getCreatedAt<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'d/MM/yyyy'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'fr'</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span><span style="color: #339933;">&lt;/</span>span<span style="color: #339933;">&gt;</span>
      à <span style="color: #339933;">&lt;</span>span <span style="color: #000000; font-weight: bold;">class</span><span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;time_class&quot;</span><span style="color: #339933;">&gt;</span>getCreatedAt<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'hh:mm'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'fr'</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span><span style="color: #339933;">&lt;/</span>span<span style="color: #339933;">&gt;</span>
    <span style="color: #339933;">&lt;/</span>span<span style="color: #339933;">&gt;&lt;/</span>div<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;</span>hr <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;text_class&quot;</span><span style="color: #339933;">&gt;</span>
    <span style="color: #990000;">getText</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;">&lt;/</span>div<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;/</span>div<span style="color: #339933;">&gt;</span></pre></td></tr></table></div>

<p>2) le partial _backend_comment_actions.php contient les actions pouvant être effectuées sur un commentaire :</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('p1087code118'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table width="100%" ><tr id="p1087118"><td class="code" id="p1087code118"><pre class="php" style="font-family:monospace;"><span style="color: #339933;">&lt;</span>a onclick<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;publishComment(&lt;?php echo <span style="color: #006699; font-weight: bold;">$comment-&gt;getId</span>()?&gt;)&quot;</span> href<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;#&quot;</span><span style="color: #339933;">&gt;</span>publier<span style="color: #339933;">&lt;/</span>a<span style="color: #339933;">&gt;</span> <span style="color: #339933;">|</span>
<span style="color: #339933;">&lt;</span>a onclick<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;unPublishComment(&lt;?php echo <span style="color: #006699; font-weight: bold;">$comment-&gt;getId</span>()?&gt;)&quot;</span> href<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;#&quot;</span><span style="color: #339933;">&gt;</span>censurer<span style="color: #339933;">&lt;/</span>a<span style="color: #339933;">&gt;</span> <span style="color: #339933;">|</span>
<span style="color: #339933;">&lt;</span>a onclick<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;makeSpamComment(&lt;?php echo <span style="color: #006699; font-weight: bold;">$comment-&gt;getId</span>()?&gt;)&quot;</span> href<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;#&quot;</span><span style="color: #339933;">&gt;</span>spam<span style="color: #339933;">&lt;/</span>a<span style="color: #339933;">&gt;</span> <span style="color: #339933;">|</span>
<span style="color: #339933;">&lt;</span>a onclick<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;unMakeSpamComment(&lt;?php echo <span style="color: #006699; font-weight: bold;">$comment-&gt;getId</span>()?&gt;)&quot;</span> href<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;#&quot;</span><span style="color: #339933;">&gt;</span>pas spam<span style="color: #339933;">&lt;/</span>a<span style="color: #339933;">&gt;</span> <span style="color: #339933;">|</span>
<span style="color: #339933;">&lt;</span>a onclick<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;deleteComment(&lt;?php echo <span style="color: #006699; font-weight: bold;">$comment-&gt;getId</span>()?&gt;)&quot;</span> href<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;#&quot;</span><span style="color: #339933;">&gt;</span>supprimer<span style="color: #339933;">&lt;/</span>a<span style="color: #339933;">&gt;</span> <span style="color: #339933;">|</span>
<span style="color: #339933;">&lt;</span>a href<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;&lt;?php echo url_for('@comment_edit?id='.<span style="color: #006699; font-weight: bold;">$comment-&gt;getId</span>()) ?&gt;&quot;</span><span style="color: #339933;">&gt;</span>editer<span style="color: #339933;">&lt;/</span>a<span style="color: #339933;">&gt;</span></pre></td></tr></table></div>

<p>3) les fonctions javascript : elles permettent d&#8217;appeler respectivement les méthodes permettant d&#8217;effectuer les actions correspondante, tout ça en AJAX.</p>
<p>Ces différentes actions vont effectuer le même travail que les ListAction, à la différence près qu&#8217;elle sont appelée en Ajax, et n&#8217;ont donc pas le même template de retour.</p>
<p>Voilà d&#8217;abord le routing, puis les actions :</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('p1087code119'); return false;">View Code</a> YAML</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table width="100%" ><tr id="p1087119"><td class="code" id="p1087code119"><pre class="yaml" style="font-family:monospace;">comment_ajax_publish:
  url:   /comment_ajax_publish
  params: {module: comment, action: ajaxPublish}
comment_ajax_unpublish:
  url:   /comment_ajax_unpublish
  params: {module: comment, action: ajaxUnPublish}
comment_ajax_makespam:
  url:   /comment_ajax_markspam
  params: {module: comment, action: ajaxMakeSpam}
comment_ajax_unmakespam:
  url:   /comment_ajax_unmarkspam
  params: {module: comment, action: ajaxUnMakeSpam}
comment_ajax_delete:
  url:   /comment_ajax_delete
  params: {module: comment, action: ajaxDelete}</pre></td></tr></table></div>

<p>actions :</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('p1087code120'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table width="100%" ><tr id="p1087120"><td class="code" id="p1087code120"><pre class="php" style="font-family:monospace;">  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> executeAjaxPublish<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;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">comment</span> <span style="color: #339933;">=</span> Doctrine<span style="color: #339933;">::</span><span style="color: #004000;">getTable</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Comment'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">find</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$request</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getParameter</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'id'</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;">forward404Unless</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">comment</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;">publish</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">comment</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;">setTemplate</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'ajaxAction'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> executeAjaxUnPublish<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;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">comment</span> <span style="color: #339933;">=</span> Doctrine<span style="color: #339933;">::</span><span style="color: #004000;">getTable</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Comment'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">find</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$request</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getParameter</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'id'</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;">forward404Unless</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">comment</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;">unpublish</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">comment</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;">setTemplate</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'ajaxAction'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> executeAjaxMakeSpam<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;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">comment</span> <span style="color: #339933;">=</span> Doctrine<span style="color: #339933;">::</span><span style="color: #004000;">getTable</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Comment'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">find</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$request</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getParameter</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'id'</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;">forward404Unless</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">comment</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;">makespam</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">comment</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;">setTemplate</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'ajaxAction'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> executeAjaxUnMakeSpam<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;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">comment</span> <span style="color: #339933;">=</span> Doctrine<span style="color: #339933;">::</span><span style="color: #004000;">getTable</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Comment'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">find</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$request</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getParameter</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'id'</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;">forward404Unless</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">comment</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;">unmakespam</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">comment</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;">setTemplate</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'ajaxAction'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> executeAjaxDelete<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;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">comment</span> <span style="color: #339933;">=</span> Doctrine<span style="color: #339933;">::</span><span style="color: #004000;">getTable</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Comment'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">find</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$request</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getParameter</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'id'</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;">forward404Unless</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">comment</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;">comment</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">delete</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
  protected <span style="color: #000000; font-weight: bold;">function</span> publish<span style="color: #009900;">&#40;</span><span style="color: #000088;">$comment</span><span style="color: #009900;">&#41;</span>
  <span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$comment</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setIsPublished</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$comment</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">save</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
  protected <span style="color: #000000; font-weight: bold;">function</span> unpublish<span style="color: #009900;">&#40;</span><span style="color: #000088;">$comment</span><span style="color: #009900;">&#41;</span>
  <span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$comment</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setIsPublished</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$comment</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">save</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
  protected <span style="color: #000000; font-weight: bold;">function</span> makespam<span style="color: #009900;">&#40;</span><span style="color: #000088;">$comment</span><span style="color: #009900;">&#41;</span>
  <span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$comment</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setIsSpam</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$comment</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">save</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
  protected <span style="color: #000000; font-weight: bold;">function</span> unmakespam<span style="color: #009900;">&#40;</span><span style="color: #000088;">$comment</span><span style="color: #009900;">&#41;</span>
  <span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$comment</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setIsSpam</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$comment</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">save</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><em>On remarque une certaine redondance de code dans le actions.class.php,c&#8217;est pour ça que j&#8217;ai factorisé. Pensez à corriger les autres actions (elles sont ok si vous récupérez les sources).</em></p>
<p>Et pour finir, dans les templates correspondant à ces actions, on reload la ligne du commentaire en cours, ce qui permettra de mette à jour ses état de publication et spam dans la liste des commentaires.</p>
<p>ajaxActionSuccess.php :</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('p1087code121'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table width="100%" ><tr id="p1087121"><td class="code" id="p1087code121"><pre class="php" style="font-family:monospace;">include_partial<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'comment/backend_show'</span><span style="color: #339933;">,</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'comment'</span><span style="color: #339933;">=&gt;</span><span style="color: #000088;">$comment</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span></pre></td></tr></table></div>

<p>Voilà, si vous avez des questions ou des incompréhensions, n&#8217;hésitez pas !</p>
<a href="http://www.addtoany.com/add_to/twitter?linkurl=http%3A%2F%2Fwww.lexik.fr%2Fblog%2Fsymfony%2Fnon-classe%2Fretour-sur-le-plugin-commentaires-partie-administration-1087&amp;linkname=Retour%20sur%20le%20plugin%20Commentaires%2C%20partie%20administration" title="Twitter" rel="nofollow" target="_blank"><img src="http://www.lexik.fr/blog/symfony/wp-content/plugins/add-to-any/icons/twitter.png" alt="Twitter"/></a> <a href="http://www.addtoany.com/add_to/facebook?linkurl=http%3A%2F%2Fwww.lexik.fr%2Fblog%2Fsymfony%2Fnon-classe%2Fretour-sur-le-plugin-commentaires-partie-administration-1087&amp;linkname=Retour%20sur%20le%20plugin%20Commentaires%2C%20partie%20administration" title="Facebook" rel="nofollow" target="_blank"><img src="http://www.lexik.fr/blog/symfony/wp-content/plugins/add-to-any/icons/facebook.png" alt="Facebook"/></a> <a href="http://www.addtoany.com/add_to/delicious?linkurl=http%3A%2F%2Fwww.lexik.fr%2Fblog%2Fsymfony%2Fnon-classe%2Fretour-sur-le-plugin-commentaires-partie-administration-1087&amp;linkname=Retour%20sur%20le%20plugin%20Commentaires%2C%20partie%20administration" title="Delicious" rel="nofollow" target="_blank"><img src="http://www.lexik.fr/blog/symfony/wp-content/plugins/add-to-any/icons/delicious.png" alt="Delicious"/></a> <a href="http://www.addtoany.com/add_to/viadeo?linkurl=http%3A%2F%2Fwww.lexik.fr%2Fblog%2Fsymfony%2Fnon-classe%2Fretour-sur-le-plugin-commentaires-partie-administration-1087&amp;linkname=Retour%20sur%20le%20plugin%20Commentaires%2C%20partie%20administration" title="Viadeo" rel="nofollow" target="_blank"><img src="http://www.lexik.fr/blog/symfony/wp-content/plugins/add-to-any/icons/viadeo.png" alt="Viadeo"/></a> <a href="http://www.addtoany.com/add_to/technorati_favorites?linkurl=http%3A%2F%2Fwww.lexik.fr%2Fblog%2Fsymfony%2Fnon-classe%2Fretour-sur-le-plugin-commentaires-partie-administration-1087&amp;linkname=Retour%20sur%20le%20plugin%20Commentaires%2C%20partie%20administration" title="Technorati Favorites" rel="nofollow" target="_blank"><img src="http://www.lexik.fr/blog/symfony/wp-content/plugins/add-to-any/icons/technorati.png" alt="Technorati Favorites"/></a> <a href="http://www.addtoany.com/add_to/netvibes_share?linkurl=http%3A%2F%2Fwww.lexik.fr%2Fblog%2Fsymfony%2Fnon-classe%2Fretour-sur-le-plugin-commentaires-partie-administration-1087&amp;linkname=Retour%20sur%20le%20plugin%20Commentaires%2C%20partie%20administration" title="Netvibes Share" rel="nofollow" target="_blank"><img src="http://www.lexik.fr/blog/symfony/wp-content/plugins/add-to-any/icons/netvibes.png" alt="Netvibes Share"/></a> <a href="http://www.addtoany.com/add_to/gmail?linkurl=http%3A%2F%2Fwww.lexik.fr%2Fblog%2Fsymfony%2Fnon-classe%2Fretour-sur-le-plugin-commentaires-partie-administration-1087&amp;linkname=Retour%20sur%20le%20plugin%20Commentaires%2C%20partie%20administration" title="Gmail" rel="nofollow" target="_blank"><img src="http://www.lexik.fr/blog/symfony/wp-content/plugins/add-to-any/icons/gmail.png" alt="Gmail"/></a> <a href="http://www.addtoany.com/add_to/yoolink?linkurl=http%3A%2F%2Fwww.lexik.fr%2Fblog%2Fsymfony%2Fnon-classe%2Fretour-sur-le-plugin-commentaires-partie-administration-1087&amp;linkname=Retour%20sur%20le%20plugin%20Commentaires%2C%20partie%20administration" title="Yoolink" rel="nofollow" target="_blank"><img src="http://www.lexik.fr/blog/symfony/wp-content/plugins/add-to-any/icons/yoolink.png" alt="Yoolink"/></a> <a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save?linkurl=http%3A%2F%2Fwww.lexik.fr%2Fblog%2Fsymfony%2Fnon-classe%2Fretour-sur-le-plugin-commentaires-partie-administration-1087&amp;linkname=Retour%20sur%20le%20plugin%20Commentaires%2C%20partie%20administration">Partager cet article</a>]]></content:encoded>
			<wfw:commentRss>http://www.lexik.fr/blog/symfony/non-classe/retour-sur-le-plugin-commentaires-partie-administration-1087/feed</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Développeur Web Symfony Montpellier</title>
		<link>http://www.lexik.fr/blog/symfony/non-classe/travailler-chez-lexik-developpeur-web-symfony-1069</link>
		<comments>http://www.lexik.fr/blog/symfony/non-classe/travailler-chez-lexik-developpeur-web-symfony-1069#comments</comments>
		<pubDate>Fri, 26 Feb 2010 10:41:35 +0000</pubDate>
		<dc:creator>Samuel Breton</dc:creator>
				<category><![CDATA[Non classé]]></category>
		<category><![CDATA[developpeur]]></category>
		<category><![CDATA[montpellier]]></category>
		<category><![CDATA[Symfony]]></category>

		<guid isPermaLink="false">http://www.lexik.fr/blog/symfony/?p=1069</guid>
		<description><![CDATA[Lexik est une société spécialisée dans le développement Symfony et la conception de solutions orientées web. Sous la responsabilité d&#8217;un chef de projet technique, au sein d&#8217;une équipe de développeurs, vous aurez à charge de concevoir et de développer des solutions Intranet / Internet. De formation initiale informatique (de bac à +5), vous êtes un [...]]]></description>
			<content:encoded><![CDATA[<p>Lexik est une société spécialisée dans le développement Symfony et la conception de solutions orientées web.<br />
Sous la responsabilité d&#8217;un chef de projet technique, au sein d&#8217;une équipe de développeurs, vous aurez à charge de concevoir et de développer des solutions Intranet / Internet.</p>
<p>De formation initiale informatique (de bac à +5), vous êtes un développeur confirmé en PHP/MySQL (environnement LAMP et WAMP) avec une expérience d&#8217;au moins 1 ans au sein d&#8217;une SSII ou d&#8217;une web agency.</p>
<p>Compétences requises :<br />
- Framework PHP Symfony<br />
- bonnes connaissances des environnements WAMP (Windows) et LAMP<br />
- MySQL et SQL<br />
- PHP (programmation objet)<br />
- (X)HTML, DHTML , JavaScript, CSS.<br />
- AJAX<br />
- Javascript, jQuery<br />
- standards du web (W3C)<br />
- Connaissance de subversion</p>
<p>Qualités recherchées :<br />
- curiosité<br />
- force de proposition<br />
- sens du travail en équipe</p>
<p>Disponibilité :<br />
- ASAP</p>
<p>Poste à Montpellier 34000</p>
<p>Contactez Samuel Breton, Tél : 04 67 99 63 19<br />
Email : contact@lexik.fr<br />
CV + LM </p>
<a href="http://www.addtoany.com/add_to/twitter?linkurl=http%3A%2F%2Fwww.lexik.fr%2Fblog%2Fsymfony%2Fnon-classe%2Ftravailler-chez-lexik-developpeur-web-symfony-1069&amp;linkname=D%C3%A9veloppeur%20Web%20Symfony%20Montpellier" title="Twitter" rel="nofollow" target="_blank"><img src="http://www.lexik.fr/blog/symfony/wp-content/plugins/add-to-any/icons/twitter.png" alt="Twitter"/></a> <a href="http://www.addtoany.com/add_to/facebook?linkurl=http%3A%2F%2Fwww.lexik.fr%2Fblog%2Fsymfony%2Fnon-classe%2Ftravailler-chez-lexik-developpeur-web-symfony-1069&amp;linkname=D%C3%A9veloppeur%20Web%20Symfony%20Montpellier" title="Facebook" rel="nofollow" target="_blank"><img src="http://www.lexik.fr/blog/symfony/wp-content/plugins/add-to-any/icons/facebook.png" alt="Facebook"/></a> <a href="http://www.addtoany.com/add_to/delicious?linkurl=http%3A%2F%2Fwww.lexik.fr%2Fblog%2Fsymfony%2Fnon-classe%2Ftravailler-chez-lexik-developpeur-web-symfony-1069&amp;linkname=D%C3%A9veloppeur%20Web%20Symfony%20Montpellier" title="Delicious" rel="nofollow" target="_blank"><img src="http://www.lexik.fr/blog/symfony/wp-content/plugins/add-to-any/icons/delicious.png" alt="Delicious"/></a> <a href="http://www.addtoany.com/add_to/viadeo?linkurl=http%3A%2F%2Fwww.lexik.fr%2Fblog%2Fsymfony%2Fnon-classe%2Ftravailler-chez-lexik-developpeur-web-symfony-1069&amp;linkname=D%C3%A9veloppeur%20Web%20Symfony%20Montpellier" title="Viadeo" rel="nofollow" target="_blank"><img src="http://www.lexik.fr/blog/symfony/wp-content/plugins/add-to-any/icons/viadeo.png" alt="Viadeo"/></a> <a href="http://www.addtoany.com/add_to/technorati_favorites?linkurl=http%3A%2F%2Fwww.lexik.fr%2Fblog%2Fsymfony%2Fnon-classe%2Ftravailler-chez-lexik-developpeur-web-symfony-1069&amp;linkname=D%C3%A9veloppeur%20Web%20Symfony%20Montpellier" title="Technorati Favorites" rel="nofollow" target="_blank"><img src="http://www.lexik.fr/blog/symfony/wp-content/plugins/add-to-any/icons/technorati.png" alt="Technorati Favorites"/></a> <a href="http://www.addtoany.com/add_to/netvibes_share?linkurl=http%3A%2F%2Fwww.lexik.fr%2Fblog%2Fsymfony%2Fnon-classe%2Ftravailler-chez-lexik-developpeur-web-symfony-1069&amp;linkname=D%C3%A9veloppeur%20Web%20Symfony%20Montpellier" title="Netvibes Share" rel="nofollow" target="_blank"><img src="http://www.lexik.fr/blog/symfony/wp-content/plugins/add-to-any/icons/netvibes.png" alt="Netvibes Share"/></a> <a href="http://www.addtoany.com/add_to/gmail?linkurl=http%3A%2F%2Fwww.lexik.fr%2Fblog%2Fsymfony%2Fnon-classe%2Ftravailler-chez-lexik-developpeur-web-symfony-1069&amp;linkname=D%C3%A9veloppeur%20Web%20Symfony%20Montpellier" title="Gmail" rel="nofollow" target="_blank"><img src="http://www.lexik.fr/blog/symfony/wp-content/plugins/add-to-any/icons/gmail.png" alt="Gmail"/></a> <a href="http://www.addtoany.com/add_to/yoolink?linkurl=http%3A%2F%2Fwww.lexik.fr%2Fblog%2Fsymfony%2Fnon-classe%2Ftravailler-chez-lexik-developpeur-web-symfony-1069&amp;linkname=D%C3%A9veloppeur%20Web%20Symfony%20Montpellier" title="Yoolink" rel="nofollow" target="_blank"><img src="http://www.lexik.fr/blog/symfony/wp-content/plugins/add-to-any/icons/yoolink.png" alt="Yoolink"/></a> <a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save?linkurl=http%3A%2F%2Fwww.lexik.fr%2Fblog%2Fsymfony%2Fnon-classe%2Ftravailler-chez-lexik-developpeur-web-symfony-1069&amp;linkname=D%C3%A9veloppeur%20Web%20Symfony%20Montpellier">Partager cet article</a>]]></content:encoded>
			<wfw:commentRss>http://www.lexik.fr/blog/symfony/non-classe/travailler-chez-lexik-developpeur-web-symfony-1069/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Utilisation de sfUser et de ses &#171;&#160;namespace&#160;&#187;(s)</title>
		<link>http://www.lexik.fr/blog/symfony/symfony/utilisation-de-sfuser-et-de-ses-namespaces-1053</link>
		<comments>http://www.lexik.fr/blog/symfony/symfony/utilisation-de-sfuser-et-de-ses-namespaces-1053#comments</comments>
		<pubDate>Mon, 08 Feb 2010 15:06:29 +0000</pubDate>
		<dc:creator>yoye</dc:creator>
				<category><![CDATA[1.4.x]]></category>
		<category><![CDATA[Symfony]]></category>
		<category><![CDATA[méthode magique]]></category>
		<category><![CDATA[sfUser]]></category>
		<category><![CDATA[__call]]></category>

		<guid isPermaLink="false">http://www.lexik.fr/blog/symfony/?p=1053</guid>
		<description><![CDATA[Récemment j&#8217;ai du développer un outil permettant de créer des tournois, cet outil a la particularité d&#8217;être accessible par les personnes connectés ou non. Je devais conserver d&#8217;une page à l&#8217;autre les informations saisies par mon utilisateur, pour quelqu&#8217;un de connecté on peut toujours passer par la base de donnée mais pour les autres on [...]]]></description>
			<content:encoded><![CDATA[<p>Récemment j&#8217;ai du développer un outil permettant de créer des tournois, cet outil a la particularité d&#8217;être accessible par les personnes connectés ou non. Je devais conserver d&#8217;une page à l&#8217;autre les informations saisies par mon utilisateur, pour quelqu&#8217;un de connecté on peut toujours passer par la base de donnée mais pour les autres on doit jouer avec la classe sfUser.<br />
<span id="more-1053"></span><br />
Le scénario est le suivant :<br />
- Etape 1 : je saisis des informations dans un formulaire<br />
- Etape 2 : je saisis de nouvelles informations en fonction des précédentes</p>
<p>En réalité il y a bien plus d&#8217;étapes que cela mais ce scénario me permet de poser les bases de mon article. </p>
<p>Pour rester dans la simplicité voici le schéma que nous allons utiliser :</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('p1053code126'); return false;">View Code</a> YML</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table width="100%" ><tr id="p1053126"><td class="code" id="p1053code126"><pre class="yml" style="font-family:monospace;">ModelA:
  columns:
    id:         { type: integer(4), autoincrement: true, unsigned: true, primary: true }
    title:      { type: string(255), notnull: true }
    body:       { type: clob, notnull: true }
&nbsp;
ModelB:
  columns:
    id:         { type: integer(4), autoincrement: true, unsigned: true, primary: true }
    title:      { type: string(255), notnull: true }
    body:       { type: clob, notnull: true }</pre></td></tr></table></div>

<p>Tout d&#8217;abord j&#8217;ai un formulaire lié à un modèle, je dois conserver cet objet dans ma session pour me permettre d&#8217;y accéder lors des étapes suivantes. Voilà comment j&#8217;ai procédé au niveau de ma class myUser :</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('p1053code127'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table width="100%" ><tr id="p1053127"><td class="code" id="p1053code127"><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> myUser <span style="color: #000000; font-weight: bold;">extends</span> sfBasicSecurityUser
<span style="color: #009900;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">const</span>
  MY_NAMESPACE <span style="color: #339933;">=</span> <span style="color: #0000ff;">'mynamespace'</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #009933; font-style: italic;">/**
   * __call
   *
   * @param string $method
   * @param Array $arguments
   */</span>
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> __call<span style="color: #009900;">&#40;</span><span style="color: #000088;">$method</span><span style="color: #339933;">,</span> <span style="color: #000088;">$arguments</span><span style="color: #009900;">&#41;</span>
  <span style="color: #009900;">&#123;</span>
&nbsp;
    <span style="color: #b1b100;">switch</span> <span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
      <span style="color: #b1b100;">case</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'set'</span> <span style="color: #339933;">===</span> <span style="color: #000088;">$prefix</span> <span style="color: #339933;">=</span> <span style="color: #990000;">substr</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$method</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">3</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
          <span style="color: #339933;">&amp;&amp;</span> <span style="color: #009900;">&#40;</span>Doctrine_Core<span style="color: #339933;">::</span><span style="color: #004000;">isValidModelClass</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$class</span> <span style="color: #339933;">=</span> <span style="color: #990000;">substr</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$method</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">3</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</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>_set<span style="color: #009900;">&#40;</span><span style="color: #000088;">$class</span><span style="color: #339933;">,</span> <span style="color: #000088;">$arguments</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #b1b100;">break</span><span style="color: #339933;">;</span>
&nbsp;
      <span style="color: #b1b100;">case</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'getInstance'</span> <span style="color: #339933;">===</span> <span style="color: #000088;">$prefix</span> <span style="color: #339933;">=</span> <span style="color: #990000;">substr</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$method</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">11</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
          <span style="color: #339933;">&amp;&amp;</span> <span style="color: #009900;">&#40;</span>Doctrine_Core<span style="color: #339933;">::</span><span style="color: #004000;">isValidModelClass</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$class</span> <span style="color: #339933;">=</span> <span style="color: #990000;">substr</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$method</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">11</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">:</span>
        <span style="color: #b1b100;">return</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getModelInstance</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$class</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #b1b100;">break</span><span style="color: #339933;">;</span>
&nbsp;
      <span style="color: #000000; font-weight: bold;">default</span><span style="color: #339933;">:</span>
        <span style="color: #b1b100;">return</span> parent<span style="color: #339933;">::</span>__call<span style="color: #009900;">&#40;</span><span style="color: #000088;">$method</span><span style="color: #339933;">,</span> <span style="color: #000088;">$arguments</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #b1b100;">break</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #009933; font-style: italic;">/**
   * Model storage
   *
   * @param string $class
   * @param Doctrine_Record $argument
   */</span>
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> _set<span style="color: #009900;">&#40;</span><span style="color: #000088;">$class</span><span style="color: #339933;">,</span> <span style="color: #000088;">$argument</span><span style="color: #009900;">&#41;</span>
  <span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #000088;">$argument</span> instanceof <span style="color: #000088;">$class</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
      throw <span style="color: #000000; font-weight: bold;">new</span> Exception<span style="color: #009900;">&#40;</span><span style="color: #990000;">sprintf</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'You must specify a valid %s'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$class</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setAttribute</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$class</span><span style="color: #339933;">,</span> <span style="color: #000088;">$argument</span><span style="color: #339933;">,</span> <span style="color: #000000; font-weight: bold;">self</span><span style="color: #339933;">::</span><span style="color: #004000;">MY_NAMESPACE</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #009933; font-style: italic;">/**
   * Return model
   *
   * @param string $modelName
   * @return Doctrine_Record
   */</span>
  protected <span style="color: #000000; font-weight: bold;">function</span> getModelInstance<span style="color: #009900;">&#40;</span><span style="color: #000088;">$modelName</span><span style="color: #009900;">&#41;</span>
  <span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">return</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getAttribute</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$modelName</span><span style="color: #339933;">,</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #000000; font-weight: bold;">self</span><span style="color: #339933;">::</span><span style="color: #004000;">MY_NAMESPACE</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>Ayant surchargé ma méthode __call(), je peux désormais appeler les méthodes suivantes :</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('p1053code128'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table width="100%" ><tr id="p1053128"><td class="code" id="p1053code128"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getUser</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setModelA</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> ModelA<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: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getUser</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getInstanceModelA</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>Pour être sûr de ne passer que des objets définis au niveau de mon schéma j&#8217;utilise la méthode &laquo;&nbsp;Doctrine_Core::isValidModelClass&nbsp;&raquo;, ce code me permet également de ne conserver qu&#8217;une seule fois un objet au niveau de mon objet myUser. De plus, je regroupe toutes ces données dans un &laquo;&nbsp;namespace&nbsp;&raquo; ce qui me permettra de les effacer très simplement par exemple si mon utilisateur souhaite retourner sur la première étape.</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('p1053code129'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table width="100%" ><tr id="p1053129"><td class="code" id="p1053code129"><pre class="php" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">// apps/MON_APPS/modules/MON_MODULE/actions/action.class.php</span>
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> executeStep1<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;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getUser</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">cleanMyNameSpace</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// apps/MON_APPS/lin/myUser.class.php</span>
  <span style="color: #009933; font-style: italic;">/**
   * Clean Mynamespace
   *
   * @param $exception Valeur à ne pas enlever
   * @return void
   */</span>
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> cleanMyNameSpace<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;">getAttributeHolder</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">removeNamespace</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">self</span><span style="color: #339933;">::</span><span style="color: #004000;">MY_NAMESPACE</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>Ce code m&#8217;a permis de conserver une flexibilité lorsque je passe d&#8217;une étape à l&#8217;autre, en effet, j&#8217;aurai pu choisir de passer simplement les attributs que je souhaite conserver.  Depuis la mise en production les paramètres nécessaires d&#8217;une étape à l&#8217;autre ont changé en conservant un objet entier je n&#8217;ai pas eu à m&#8217;en soucier, de plus je ne suis pas obligé de stocker ces objets en base de donnée.</p>
<a href="http://www.addtoany.com/add_to/twitter?linkurl=http%3A%2F%2Fwww.lexik.fr%2Fblog%2Fsymfony%2Fsymfony%2Futilisation-de-sfuser-et-de-ses-namespaces-1053&amp;linkname=Utilisation%20de%20sfUser%20et%20de%20ses%20%26laquo%3B%26nbsp%3Bnamespace%26nbsp%3B%26raquo%3B%28s%29" title="Twitter" rel="nofollow" target="_blank"><img src="http://www.lexik.fr/blog/symfony/wp-content/plugins/add-to-any/icons/twitter.png" alt="Twitter"/></a> <a href="http://www.addtoany.com/add_to/facebook?linkurl=http%3A%2F%2Fwww.lexik.fr%2Fblog%2Fsymfony%2Fsymfony%2Futilisation-de-sfuser-et-de-ses-namespaces-1053&amp;linkname=Utilisation%20de%20sfUser%20et%20de%20ses%20%26laquo%3B%26nbsp%3Bnamespace%26nbsp%3B%26raquo%3B%28s%29" title="Facebook" rel="nofollow" target="_blank"><img src="http://www.lexik.fr/blog/symfony/wp-content/plugins/add-to-any/icons/facebook.png" alt="Facebook"/></a> <a href="http://www.addtoany.com/add_to/delicious?linkurl=http%3A%2F%2Fwww.lexik.fr%2Fblog%2Fsymfony%2Fsymfony%2Futilisation-de-sfuser-et-de-ses-namespaces-1053&amp;linkname=Utilisation%20de%20sfUser%20et%20de%20ses%20%26laquo%3B%26nbsp%3Bnamespace%26nbsp%3B%26raquo%3B%28s%29" title="Delicious" rel="nofollow" target="_blank"><img src="http://www.lexik.fr/blog/symfony/wp-content/plugins/add-to-any/icons/delicious.png" alt="Delicious"/></a> <a href="http://www.addtoany.com/add_to/viadeo?linkurl=http%3A%2F%2Fwww.lexik.fr%2Fblog%2Fsymfony%2Fsymfony%2Futilisation-de-sfuser-et-de-ses-namespaces-1053&amp;linkname=Utilisation%20de%20sfUser%20et%20de%20ses%20%26laquo%3B%26nbsp%3Bnamespace%26nbsp%3B%26raquo%3B%28s%29" title="Viadeo" rel="nofollow" target="_blank"><img src="http://www.lexik.fr/blog/symfony/wp-content/plugins/add-to-any/icons/viadeo.png" alt="Viadeo"/></a> <a href="http://www.addtoany.com/add_to/technorati_favorites?linkurl=http%3A%2F%2Fwww.lexik.fr%2Fblog%2Fsymfony%2Fsymfony%2Futilisation-de-sfuser-et-de-ses-namespaces-1053&amp;linkname=Utilisation%20de%20sfUser%20et%20de%20ses%20%26laquo%3B%26nbsp%3Bnamespace%26nbsp%3B%26raquo%3B%28s%29" title="Technorati Favorites" rel="nofollow" target="_blank"><img src="http://www.lexik.fr/blog/symfony/wp-content/plugins/add-to-any/icons/technorati.png" alt="Technorati Favorites"/></a> <a href="http://www.addtoany.com/add_to/netvibes_share?linkurl=http%3A%2F%2Fwww.lexik.fr%2Fblog%2Fsymfony%2Fsymfony%2Futilisation-de-sfuser-et-de-ses-namespaces-1053&amp;linkname=Utilisation%20de%20sfUser%20et%20de%20ses%20%26laquo%3B%26nbsp%3Bnamespace%26nbsp%3B%26raquo%3B%28s%29" title="Netvibes Share" rel="nofollow" target="_blank"><img src="http://www.lexik.fr/blog/symfony/wp-content/plugins/add-to-any/icons/netvibes.png" alt="Netvibes Share"/></a> <a href="http://www.addtoany.com/add_to/gmail?linkurl=http%3A%2F%2Fwww.lexik.fr%2Fblog%2Fsymfony%2Fsymfony%2Futilisation-de-sfuser-et-de-ses-namespaces-1053&amp;linkname=Utilisation%20de%20sfUser%20et%20de%20ses%20%26laquo%3B%26nbsp%3Bnamespace%26nbsp%3B%26raquo%3B%28s%29" title="Gmail" rel="nofollow" target="_blank"><img src="http://www.lexik.fr/blog/symfony/wp-content/plugins/add-to-any/icons/gmail.png" alt="Gmail"/></a> <a href="http://www.addtoany.com/add_to/yoolink?linkurl=http%3A%2F%2Fwww.lexik.fr%2Fblog%2Fsymfony%2Fsymfony%2Futilisation-de-sfuser-et-de-ses-namespaces-1053&amp;linkname=Utilisation%20de%20sfUser%20et%20de%20ses%20%26laquo%3B%26nbsp%3Bnamespace%26nbsp%3B%26raquo%3B%28s%29" title="Yoolink" rel="nofollow" target="_blank"><img src="http://www.lexik.fr/blog/symfony/wp-content/plugins/add-to-any/icons/yoolink.png" alt="Yoolink"/></a> <a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save?linkurl=http%3A%2F%2Fwww.lexik.fr%2Fblog%2Fsymfony%2Fsymfony%2Futilisation-de-sfuser-et-de-ses-namespaces-1053&amp;linkname=Utilisation%20de%20sfUser%20et%20de%20ses%20%26laquo%3B%26nbsp%3Bnamespace%26nbsp%3B%26raquo%3B%28s%29">Partager cet article</a>]]></content:encoded>
			<wfw:commentRss>http://www.lexik.fr/blog/symfony/symfony/utilisation-de-sfuser-et-de-ses-namespaces-1053/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>
