<?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>Amicalement Web - Astuces et Bons plans dans le développement web &#187; Astuces</title>
	<atom:link href="http://www.amicalement-web.net/categorie/astuces/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.amicalement-web.net</link>
	<description>Astuces et bons plans d&#039;un web developpeur</description>
	<lastBuildDate>Thu, 03 Jun 2010 10:06:47 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Symfony: Une utilisation du type array de Doctrine</title>
		<link>http://www.amicalement-web.net/symfony-une-utilisation-du-type-array-de-doctrine/2009/11/04/</link>
		<comments>http://www.amicalement-web.net/symfony-une-utilisation-du-type-array-de-doctrine/2009/11/04/#comments</comments>
		<pubDate>Wed, 04 Nov 2009 10:00:47 +0000</pubDate>
		<dc:creator>Tim</dc:creator>
				<category><![CDATA[Astuces]]></category>
		<category><![CDATA[astuce]]></category>
		<category><![CDATA[doctrine]]></category>
		<category><![CDATA[form]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Symfony]]></category>

		<guid isPermaLink="false">http://www.amicalement-web.net/?p=925</guid>
		<description><![CDATA[ Oui, les billets Symfony commençaient à me manquer. Mais difficile de trouver et le temps et la ressource pour en faire. Mais voici une petite astuce illustrée qui permet de trouver une application au type array de Doctrine tout en simplifiant une problématique. Tout ce que j&#8217;aime.

Commençons par rendre à César ce qui est [...]]]></description>
			<content:encoded><![CDATA[<p><img src="http://www.amicalement-web.net/wp-content/uploads/check.png" alt="check" title="check" width="614" height="100" class="alignnone size-full wp-image-927" /> Oui, les billets Symfony commençaient à me manquer. Mais difficile de trouver et le temps et la ressource pour en faire. Mais voici une petite astuce illustrée qui permet de trouver une application au <a  href="http://www.doctrine-project.org/documentation/manual/1_1/en/defining-models#columns:data-types:array">type array de Doctrine</a> tout en simplifiant une problématique. Tout ce que j&#8217;aime.<br />
<span id="more-925"></span><br />
Commençons par rendre à César ce qui est à César, j&#8217;avais dans l&#8217;idée d&#8217;essayer ce type mais les exemples sur la doc ou sur le net n&#8217;était pas légion. En bon fainéant que je suis, j&#8217;ai demandé un peu d&#8217;aide et c&#8217;est mon nouveau voisin de droite, <a  href="http://twitter.com/colinux">Colin</a> qui m&#8217;a soufflé un bout de code où il l&#8217;utilise.</p>
<h3>Problématique</h3>
<p>Il me fallait stocker le résultat d&#8217;une question à choix multiple sans pour autant avoir à manipuler ensuite ses résultats autrement que par simple visualisation. Du coup, je voulais éviter d&#8217;avoir à créer une table pour stocker ces réponses et une table d&#8217;association avec ma table miroir de mon formulaire.</p>
<p>C&#8217;est là où le type array intervient. Rien de compliqué en fait, il ne fait que simplifier un traitement tout bête qui consiste à stocker un tableau php en base après sérialisation. Et qui le désérialise quand on le récupère depuis un enregistrement de la base.<br />
Une chose de moins à faire en fait et c&#8217;est bien là ce qu&#8217;on recherche nous autres développeurs, en faire le moins possible ;)</p>
<h3>La solution codée</h3>
<p>Niveau code, c&#8217;est très simple. On défini notre champ dans notre <code>schema.yml</code> comme n&#8217;importe quel autre champ</p>

<div class="wp_syntax"><div class="code"><pre class="yaml" style="font-family:monospace;">meschoix: { type: array(1000) }</pre></div></div>

<p>Qui donnera un champ text en mysql.</p>
<p>Ensuite, il suffit de modifier le formulaire obtenu pour transformer notre champ en liste de checkbox de manière très classique:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><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;">'meschoix'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> sfWidgetFormChoice<span style="color: #009900;">&#40;</span><span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
      <span style="color: #0000ff;">'choices'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
        <span style="color: #0000ff;">'0'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'Choix1'</span><span style="color: #339933;">,</span>
        <span style="color: #0000ff;">'1'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'Choix2'</span><span style="color: #339933;">,</span>
        <span style="color: #0000ff;">'2'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'Choix3'</span><span style="color: #339933;">,</span>
        <span style="color: #0000ff;">'3'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'Choix4'</span>
      <span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
      <span style="color: #0000ff;">'multiple'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #339933;">,</span>
      <span style="color: #0000ff;">'expanded'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #009900; font-weight: bold;">true</span>
    <span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Et puis c&#8217;est tout! Maintenant, lors de l&#8217;enregistrement, Doctrine va sérialiser le résultat de votre sélection et le stocker en base. Et quand vous peuplerez votre formulaire avec des données en base, il le désérialisera et donnera les bonnes valeurs à notre liste de checkbox.</p>
<p>Bien sûr, ca nous oblige à ne pas changer l&#8217;ordre des questions et les rend difficilement modifiables. Mais c&#8217;est une décision qui doit être prise en connaissance de cette limitation.</p>
<p><small>Crédit photo: http://www.flickr.com/photos/40491122@N03/3738306829/</small></p>
]]></content:encoded>
			<wfw:commentRss>http://www.amicalement-web.net/symfony-une-utilisation-du-type-array-de-doctrine/2009/11/04/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Symfony et les tests</title>
		<link>http://www.amicalement-web.net/symfony-et-les-tests/2009/10/13/</link>
		<comments>http://www.amicalement-web.net/symfony-et-les-tests/2009/10/13/#comments</comments>
		<pubDate>Tue, 13 Oct 2009 09:00:41 +0000</pubDate>
		<dc:creator>Vince</dc:creator>
				<category><![CDATA[Astuces]]></category>
		<category><![CDATA[Symfony]]></category>
		<category><![CDATA[tests]]></category>

		<guid isPermaLink="false">http://www.amicalement-web.net/?p=840</guid>
		<description><![CDATA[
On entend partout que les tests unitaires sont indispensables, que si tu n&#8217;en fais pas c&#8217;est que tu es sale, voire même que tu codes sous Windows avec Wordpad (quoi il n&#8217;y a pas de rapport ?). Si vous aussi vous faites partie des gens qui sont persuadés que oui c&#8217;est bien (sinon je peux [...]]]></description>
			<content:encoded><![CDATA[<p><img class="aligncenter size-full wp-image-877" src="http://www.amicalement-web.net/wp-content/uploads/Capture-d’écran-2009-10-12-à-17.17.12.png" alt="Capture d’écran 2009-10-12 à 17.17.12" width="614" height="100" /><br />
On entend partout que les tests unitaires sont indispensables, que si tu n&#8217;en fais pas c&#8217;est que tu es sale, voire même que tu codes sous Windows avec Wordpad (quoi il n&#8217;y a pas de rapport ?). Si vous aussi vous faites partie des gens qui sont persuadés que oui c&#8217;est bien (sinon je peux plus rien pour vous), mais je vois vraiment pas comment faire et par où commencer avec mon code, peut être que je vais pouvoir vous éclairer.<br />
<span id="more-840"></span><br />
Commencez par aller faire un tour sur le site de symfony pour lire la partie sur les <a  href="http://www.symfony-project.org/jobeet/1_2/Doctrine/en/08">tests unitaires dans Jobeet</a> ou dans<a  href="http://www.symfony-project.org/book/1_2/15-Unit-and-Functional-Testing"> le livre de référence</a> si ce n&#8217;est pas encore fait, et lisez ce qui suit !</p>
<h3>Le modèle MVC</h3>
<p>Le fameux Modèle Vue Controlleur. Si vous vous dites qu&#8217;à part avec un test fonctionnel (c&#8217;est à dire un test qui simule le comportement utilisateur via des clicks, avec un sfBrowser ou <a  href="http://seleniumhq.org/" target="_blank">Selenium</a>) ça va pas être possible de tester votre appli, c&#8217;est surement vrai ; que les tests fonctionnels ça se fait vers la fin de l&#8217;écriture de votre module, quand votre code HTML bouge plus trop c&#8217;est vrai aussi ; et que donc dans le coup vous voyez pas trop comment tester votre application avant de l&#8217;avoir finie, c&#8217;est vrai aussi.</p>
<h3>Du M dans le C</h3>
<p>Mais si vous vous dites tout ça, c&#8217;est qu&#8217;il y a du M dans le C. C&#8217;est à dire qu&#8217;il y a des parties de votre Modèle dans votre Controlleur et dans le coup, vous ne pouvez tester votre modèle sans votre Controlleur (et dans le coup sans la Vue aussi). C&#8217;est typiquement le cas quand vous faites quelque chose comme ça :</p>

<div class="wp_syntax"><div class="code"><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> 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;">$objects</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;">'Objects'</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;">'o'</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;">'o.titi=?'</span><span style="color: #339933;">,</span><span style="color: #000088;">$titi</span><span style="color: #009900;">&#41;</span>
		<span style="color: #339933;">-&gt;</span><span style="color: #004000;">andWhere</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'...'</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;">'...'</span><span style="color: #009900;">&#41;</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></div></div>

<p>Faites pas style c&#8217;est pas vous, je vous ai vu. Pourquoi ne pas faire ça dans l&#8217;action :</p>

<div class="wp_syntax"><div class="code"><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> 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;">$objects</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;">'Objects'</span><span style="color: #009900;">&#41;</span>
		<span style="color: #339933;">-&gt;</span><span style="color: #004000;">getObjectsByTiti</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$titi</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span></pre></div></div>

<p>et ça dans la classe ObjectTable :</p>

<div class="wp_syntax"><div class="code"><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> getObjectsByTiti<span style="color: #009900;">&#40;</span><span style="color: #000088;">$titi</span><span style="color: #009900;">&#41;</span>
  <span style="color: #009900;">&#123;</span>
	<span style="color: #b1b100;">return</span> Doctrine<span style="color: #339933;">::</span><span style="color: #004000;">getTable</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Objects'</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;">'o'</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;">'o.titi=?'</span><span style="color: #339933;">,</span><span style="color: #000088;">$toto</span><span style="color: #009900;">&#41;</span>
		<span style="color: #339933;">-&gt;</span><span style="color: #004000;">andWhere</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'...'</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;">'...'</span><span style="color: #009900;">&#41;</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></div></div>

<p>Vous serez d&#8217;accord avec moi que le résultat est le même mais que c&#8217;est un peu plus découplé. On pourra reparler plus tard du fait que le summum serait de retourner un tableau (le fameux <a  href="/benchmark-apache-doctrine-hydrate-object-vs-array/2009/10/08/">HYDRATE_ARRAY</a> de Doctrine passé à la requête) dans la méthode getObjectsByTiti afin de ne pas se retrouver avec quelque chose dépendant de dépendant de l&#8217;ORM dans le controlleur (ici une Doctrine_Collection), mais ce n&#8217;est pas le sujet ici ;-)</p>
<h3>Si c&#8217;est pareil, pourquoi c&#8217;est mieux alors ?</h3>
<p>Parce que je vous le dis ! Oui bon d&#8217;accord. Plusieurs avantages, vous n&#8217;avez qu&#8217;un endroit à aller voir en cas de souci de logique métier : le Modèle. Mais surtout plus vous en mettrez dans le modèle (le premier que je vois écrire du HTML dans le modèle devra coder avec Wordpad), plus vous pourrez en tester ! Et plus vous écrirez de tests unitaires pour vos modèles, plus vous serez confiant dans votre code et plus votre code sera de qualité. Vos controlleurs devraient faire environ 10 lines de code, pas plus. Essayez de vous y tenir, ça vous changera la vie (mettez ce qui concerne la sessions dans votre classe myUser par exemple).</p>
<p>Maintenant nous pourrions écrire un test unitaire du style :</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$t</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">ok</span><span style="color: #009900;">&#40;</span>
  <span style="color: #990000;">count</span><span style="color: #009900;">&#40;</span>Doctrine<span style="color: #339933;">::</span><span style="color: #004000;">getTable</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Object'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getObjectsByTiti</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'mogito'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">&gt;</span><span style="color: #cc66cc;">4</span><span style="color: #339933;">,</span>
  <span style="color: #0000ff;">'-&gt; Getting object by mogito returns more than 4 objects'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Ici je teste que notre méthode retourne au moins 4 objets si je lui demande un mogito.</p>
<h3>Vers la rédemption</h3>
<p>Si vous faites bien attention de ne pas avoir de M dans le C, vous pourrez coder toute votre logique applicative sans ouvrir un seul navigateur internet, et c&#8217;est même ce que vous devrez vous forcer à faire. Vous pouvez, voire même devez, faire marcher votre logique métier sans la moindre page web, juste en faisant tourner vos tests unitaires qui sont suffisants (et nécessaires) pour voir si votre code est bon et fait ce qu&#8217;il doit faire.</p>
<p>En faisant ça, vous pourrez ne vous concentrer que sur le modèle, sur le coeur de la logique métier, sans être parasité par des besoins webs (que le formulaire soit valide pour pouvoir récupérer l&#8217;objet, c&#8217;est pas le problème du modèle ça !).</p>
<p>Une fois que vous aurez les principales fonctions métier de codées dans votre modèle vous pourrez ensuite écrire vos controlleurs et pages web et à la fin les tests fonctionnels. Mais avant de faire ça, vous saurez qu&#8217;il y aura déjà une bonne partie de votre code qui sera écrit et testé.</p>
<h3>Et après ?</h3>
<p>Quand vous aurez pris l&#8217;habitude d&#8217;écrire des tests unitaires et que vous en serez devenu accrocs, vous pourrez passer au développement guidé par les tests (le TDD). Là vous écrivez avant les tests (donc en gros ce que vous attendez de votre modèle) et ensuite vous écrivez le modèle : vous serez assuré que tout votre modèle est couvert par les tests, et vous aurez écrit ce que doit faire votre modèle avant de l&#8217;avoir implémenté (c&#8217;est toujours bien de réfléchir avant de coder, sisi).</p>
<p>Quand vos tests commencerons à prendre du temps à s&#8217;exécuter, vous commencerez à vous intéresser à des outils d&#8217;intégration continue comme phpUnderControl ou Hudson, dont NiKo a récemment décrit<a  href="http://prendreuncafe.com/blog/post/2009/10/06/Simple-Continuous-Integration-of-a-Symfony-Project-using-Hudson" target="_blank"> l&#8217;intégration avec symfony</a> dans un de ses articles.</p>
<p>Pour finir, en effet ça va vous demander plus de temps de réaliser votre application web en écrivant des tests unitaires. Comme vous êtes Freelance et que le temps c&#8217;est de l&#8217;argent, vous vous dites que vous ne le ferez pas. Oui mais quand vous aurez livré et que le client reviendra 10 fois vers vous ensuite à cause de bugs, ou que vous aurez changé quelque chose qui aura tout cassé sans que vous ne vous en rendiez compte, et que ça commencera à se savoir que ce que vous faites est buggé : vous penserez fortement à une méthode qui vous permettra d&#8217;être beaucoup plus confiant dans votre code, à savoir l&#8217;écriture de tests !</p>
<p>Si vous êtes stagiaire ça marche aussi pour prendre de bonnes habitudes et ne pas vous faire taper dessus par votre supérieur, si vous êtes salarié reportez vous au cas du stagiaire ;-)</p>
]]></content:encoded>
			<wfw:commentRss>http://www.amicalement-web.net/symfony-et-les-tests/2009/10/13/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Benchmark Apache: doctrine hydrate object vs array</title>
		<link>http://www.amicalement-web.net/benchmark-apache-doctrine-hydrate-object-vs-array/2009/10/08/</link>
		<comments>http://www.amicalement-web.net/benchmark-apache-doctrine-hydrate-object-vs-array/2009/10/08/#comments</comments>
		<pubDate>Thu, 08 Oct 2009 09:00:59 +0000</pubDate>
		<dc:creator>Tim</dc:creator>
				<category><![CDATA[Astuces]]></category>
		<category><![CDATA[apache]]></category>
		<category><![CDATA[benchmark]]></category>
		<category><![CDATA[doctrine]]></category>
		<category><![CDATA[hydrate]]></category>
		<category><![CDATA[performance]]></category>
		<category><![CDATA[Symfony]]></category>

		<guid isPermaLink="false">http://www.amicalement-web.net/?p=806</guid>
		<description><![CDATA[ Il faut se rendre à l&#8217;évidence, défendre une idée sans argument, c&#8217;est bien souvent très compliqué. Et quand il a fallu que j&#8217;arrive à quantifier le réel apport d&#8217;une hydratation en tableau plutôt qu&#8217;en object, c&#8217;était un peu difficile. Rappelez-vous, j&#8217;en avais parlé dans les secrets de doctrine.
Je me suis donc dis que j&#8217;allais [...]]]></description>
			<content:encoded><![CDATA[<p><img src="http://www.amicalement-web.net/wp-content/uploads/clock.png" alt="clock" title="clock" width="614" height="100" class="alignnone size-full wp-image-829" /> Il faut se rendre à l&#8217;évidence, défendre une idée sans argument, c&#8217;est bien souvent très compliqué. Et quand il a fallu que j&#8217;arrive à quantifier le réel apport d&#8217;une hydratation en tableau plutôt qu&#8217;en object, c&#8217;était un peu difficile. Rappelez-vous, j&#8217;en avais parlé <a  href="/doctrine-quelques-petits-secrets/2009/08/26/">dans les secrets de doctrine</a>.</p>
<p>Je me suis donc dis que j&#8217;allais tout simplement le benchmarker grosso modo, afin d&#8217;avoir une idée du gain qu&#8217;on peut avoir à passer par des tableaux. Car en fait, on sait très bien que le 2e est plus rapide, mais la question, c&#8217;est de combien.<br />
<span id="more-806"></span><br />
J&#8217;ai donc pris un modèle assez classique. Un couple de Departement/Region de France.</p>
<p>Dans mon fichier Table de mes départements, je rajoute la méthode suivante pour faire ma jointure sur régions</p>

<div class="wp_syntax"><div class="code"><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> getAll<span style="color: #009900;">&#40;</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;">createQuery</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'d'</span><span style="color: #009900;">&#41;</span>
      <span style="color: #339933;">-&gt;</span><span style="color: #004000;">leftJoin</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'d.Regions r'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span></pre></div></div>

<h3>Hydratation en objet</h3>
<p>Parfait, maintenant, je crée une action qui récupère seulement tous les items de ma table, soit environ <strong>100 départements</strong>, en relation avec une région chacun.</p>

<div class="wp_syntax"><div class="code"><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> 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;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">items</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;">'Departements'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getAll</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</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></div></div>

<p>Ca reste un cas tout a fait commun. J&#8217;ai donc lancé un <code>ab</code>, un programme livré avec apache qui permet de lancer l&#8217;exécution d&#8217;une page web, n fois avec n requêtes concurrentielles.  j&#8217;ai décidé de le lancer <strong>100 fois avec 10 requêtes à la fois</strong>. Et voici le résultat:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">Document Path:          <span style="color: #000000; font-weight: bold;">/</span>hydrate<span style="color: #000000; font-weight: bold;">/</span>index
Document Length:        <span style="color: #000000;">2400</span> bytes
&nbsp;
Concurrency Level:      <span style="color: #000000;">10</span>
Time taken <span style="color: #000000; font-weight: bold;">for</span> tests:  <span style="color: #000000;">16.862</span> seconds
Complete requests:      <span style="color: #000000;">100</span>
Failed requests:        <span style="color: #000000;">0</span>
Write errors:           <span style="color: #000000;">0</span>
Total transferred:      <span style="color: #000000;">269400</span> bytes
HTML transferred:       <span style="color: #000000;">240000</span> bytes
Requests per second:    <span style="color: #000000;">5.93</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span><span style="color: #666666; font-style: italic;">#/sec] (mean)</span>
Time per request:       <span style="color: #000000;">1686.205</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span>ms<span style="color: #7a0874; font-weight: bold;">&#93;</span> <span style="color: #7a0874; font-weight: bold;">&#40;</span>mean<span style="color: #7a0874; font-weight: bold;">&#41;</span>
Time per request:       <span style="color: #000000;">168.621</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span>ms<span style="color: #7a0874; font-weight: bold;">&#93;</span> <span style="color: #7a0874; font-weight: bold;">&#40;</span>mean, across all concurrent requests<span style="color: #7a0874; font-weight: bold;">&#41;</span>
Transfer rate:          <span style="color: #000000;">15.60</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span>Kbytes<span style="color: #000000; font-weight: bold;">/</span>sec<span style="color: #7a0874; font-weight: bold;">&#93;</span> received
&nbsp;
Connection Times <span style="color: #7a0874; font-weight: bold;">&#40;</span>ms<span style="color: #7a0874; font-weight: bold;">&#41;</span>
              min  mean<span style="color: #7a0874; font-weight: bold;">&#91;</span>+<span style="color: #000000; font-weight: bold;">/</span>-sd<span style="color: #7a0874; font-weight: bold;">&#93;</span> median   max
Connect:        <span style="color: #000000;">0</span>    <span style="color: #000000;">0</span>   <span style="color: #000000;">0.5</span>      <span style="color: #000000;">0</span>       <span style="color: #000000;">4</span>
Processing:  <span style="color: #000000;">1172</span> <span style="color: #000000;">1665</span> <span style="color: #000000;">167.3</span>   <span style="color: #000000;">1662</span>    <span style="color: #000000;">2337</span>
Waiting:     <span style="color: #000000;">1172</span> <span style="color: #000000;">1663</span> <span style="color: #000000;">166.5</span>   <span style="color: #000000;">1661</span>    <span style="color: #000000;">2337</span>
Total:       <span style="color: #000000;">1172</span> <span style="color: #000000;">1665</span> <span style="color: #000000;">167.4</span>   <span style="color: #000000;">1662</span>    <span style="color: #000000;">2338</span>
&nbsp;
Percentage of the requests served within a certain <span style="color: #000000; font-weight: bold;">time</span> <span style="color: #7a0874; font-weight: bold;">&#40;</span>ms<span style="color: #7a0874; font-weight: bold;">&#41;</span>
  <span style="color: #000000;">50</span><span style="color: #000000; font-weight: bold;">%</span>   <span style="color: #000000;">1662</span>
  <span style="color: #000000;">66</span><span style="color: #000000; font-weight: bold;">%</span>   <span style="color: #000000;">1709</span>
  <span style="color: #000000;">75</span><span style="color: #000000; font-weight: bold;">%</span>   <span style="color: #000000;">1750</span>
  <span style="color: #000000;">80</span><span style="color: #000000; font-weight: bold;">%</span>   <span style="color: #000000;">1783</span>
  <span style="color: #000000;">90</span><span style="color: #000000; font-weight: bold;">%</span>   <span style="color: #000000;">1860</span>
  <span style="color: #000000;">95</span><span style="color: #000000; font-weight: bold;">%</span>   <span style="color: #000000;">1928</span>
  <span style="color: #000000;">98</span><span style="color: #000000; font-weight: bold;">%</span>   <span style="color: #000000;">2102</span>
  <span style="color: #000000;">99</span><span style="color: #000000; font-weight: bold;">%</span>   <span style="color: #000000;">2338</span>
 <span style="color: #000000;">100</span><span style="color: #000000; font-weight: bold;">%</span>   <span style="color: #000000;">2338</span> <span style="color: #7a0874; font-weight: bold;">&#40;</span>longest request<span style="color: #7a0874; font-weight: bold;">&#41;</span></pre></div></div>

<h3>Hydratation en tableau</h3>
<p>2e action, le même code, le même résultat HTML souhaité, mais on passe dans le execute le fameux paramètre. Notre résultat ne sera donc plus une Doctrine_Collection, mais un tableau associatif avec en clé, les noms des champs.</p>

<div class="wp_syntax"><div class="code"><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> executeIndex2<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;">items</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;">'Departements'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getAll</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">execute</span><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<span style="color: #339933;">::</span><span style="color: #004000;">HYDRATE_ARRAY</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span></pre></div></div>

<p>Et voici le résultat</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">Document Path:          <span style="color: #000000; font-weight: bold;">/</span>hydrate<span style="color: #000000; font-weight: bold;">/</span>index2
Document Length:        <span style="color: #000000;">2400</span> bytes
&nbsp;
Concurrency Level:      <span style="color: #000000;">10</span>
Time taken <span style="color: #000000; font-weight: bold;">for</span> tests:   <span style="color: #000000;">10.086</span> seconds
Complete requests:      <span style="color: #000000;">100</span>
Failed requests:        <span style="color: #000000;">0</span>
Write errors:           <span style="color: #000000;">0</span>
Total transferred:      <span style="color: #000000;">269400</span> bytes
HTML transferred:       <span style="color: #000000;">240000</span> bytes
Requests per second:    <span style="color: #000000;">9.92</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span><span style="color: #666666; font-style: italic;">#/sec] (mean)</span>
Time per request:       <span style="color: #000000;">1008.569</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span>ms<span style="color: #7a0874; font-weight: bold;">&#93;</span> <span style="color: #7a0874; font-weight: bold;">&#40;</span>mean<span style="color: #7a0874; font-weight: bold;">&#41;</span>
Time per request:       <span style="color: #000000;">100.857</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span>ms<span style="color: #7a0874; font-weight: bold;">&#93;</span> <span style="color: #7a0874; font-weight: bold;">&#40;</span>mean, across all concurrent requests<span style="color: #7a0874; font-weight: bold;">&#41;</span>
Transfer rate:          <span style="color: #000000;">26.09</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span>Kbytes<span style="color: #000000; font-weight: bold;">/</span>sec<span style="color: #7a0874; font-weight: bold;">&#93;</span> received
&nbsp;
Connection Times <span style="color: #7a0874; font-weight: bold;">&#40;</span>ms<span style="color: #7a0874; font-weight: bold;">&#41;</span>
              min  mean<span style="color: #7a0874; font-weight: bold;">&#91;</span>+<span style="color: #000000; font-weight: bold;">/</span>-sd<span style="color: #7a0874; font-weight: bold;">&#93;</span> median   max
Connect:        <span style="color: #000000;">0</span>    <span style="color: #000000;">1</span>   <span style="color: #000000;">1.4</span>      <span style="color: #000000;">0</span>       <span style="color: #000000;">6</span>
Processing:   <span style="color: #000000;">598</span>  <span style="color: #000000;">992</span> <span style="color: #000000;">112.9</span>   <span style="color: #000000;">1003</span>    <span style="color: #000000;">1239</span>
Waiting:      <span style="color: #000000;">598</span>  <span style="color: #000000;">991</span> <span style="color: #000000;">113.0</span>   <span style="color: #000000;">1002</span>    <span style="color: #000000;">1239</span>
Total:        <span style="color: #000000;">599</span>  <span style="color: #000000;">993</span> <span style="color: #000000;">113.0</span>   <span style="color: #000000;">1003</span>    <span style="color: #000000;">1239</span>
&nbsp;
Percentage of the requests served within a certain <span style="color: #000000; font-weight: bold;">time</span> <span style="color: #7a0874; font-weight: bold;">&#40;</span>ms<span style="color: #7a0874; font-weight: bold;">&#41;</span>
  <span style="color: #000000;">50</span><span style="color: #000000; font-weight: bold;">%</span>   <span style="color: #000000;">1003</span>
  <span style="color: #000000;">66</span><span style="color: #000000; font-weight: bold;">%</span>   <span style="color: #000000;">1051</span>
  <span style="color: #000000;">75</span><span style="color: #000000; font-weight: bold;">%</span>   <span style="color: #000000;">1081</span>
  <span style="color: #000000;">80</span><span style="color: #000000; font-weight: bold;">%</span>   <span style="color: #000000;">1085</span>
  <span style="color: #000000;">90</span><span style="color: #000000; font-weight: bold;">%</span>   <span style="color: #000000;">1104</span>
  <span style="color: #000000;">95</span><span style="color: #000000; font-weight: bold;">%</span>   <span style="color: #000000;">1128</span>
  <span style="color: #000000;">98</span><span style="color: #000000; font-weight: bold;">%</span>   <span style="color: #000000;">1149</span>
  <span style="color: #000000;">99</span><span style="color: #000000; font-weight: bold;">%</span>   <span style="color: #000000;">1239</span>
 <span style="color: #000000;">100</span><span style="color: #000000; font-weight: bold;">%</span>   <span style="color: #000000;">1239</span> <span style="color: #7a0874; font-weight: bold;">&#40;</span>longest request<span style="color: #7a0874; font-weight: bold;">&#41;</span></pre></div></div>

<h3>Bilan</h3>
<p>Alors évidemment, il faut prendre en compte la différence de temps de réponse qu&#8217;on peut avoir entre 2 lancement de <code>ab</code>. Mais l&#8217;objectif est surtout de montrer que le gain est non négligeable et vraiment réel.</p>
<p>On constate que le HTML reçu est bien le même. On a donc le même rendu!<br />
Et côté performance, le 1er cas nous donne une requête en <strong>1,7sec</strong> quand le 2e nous donne <strong>1s</strong> soit près de<strong> 60% de mieux</strong>.</p>
<p>Conclusion, quand on souhaite réaliser seulement de l&#8217;affichage de données, il faut hydrater en tableau! Même si dans le cas présent, on parle de 0,7sec, il faut voir que c&#8217;est un projet vierge avec rien d&#8217;autre autour. Le gain est donc bien réel et pas seulement gadget.</p>
<p><small>Crédit photo: http://www.flickr.com/photos/don3rdse/3208161023/</small></p>
]]></content:encoded>
			<wfw:commentRss>http://www.amicalement-web.net/benchmark-apache-doctrine-hydrate-object-vs-array/2009/10/08/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>Tips : Configuration de serveurs</title>
		<link>http://www.amicalement-web.net/tips-configuration-de-serveurs/2009/09/22/</link>
		<comments>http://www.amicalement-web.net/tips-configuration-de-serveurs/2009/09/22/#comments</comments>
		<pubDate>Tue, 22 Sep 2009 08:34:45 +0000</pubDate>
		<dc:creator>Benoit</dc:creator>
				<category><![CDATA[Astuces]]></category>
		<category><![CDATA[config]]></category>
		<category><![CDATA[Linux]]></category>
		<category><![CDATA[perl]]></category>
		<category><![CDATA[server]]></category>
		<category><![CDATA[ssh]]></category>
		<category><![CDATA[tcpdump]]></category>
		<category><![CDATA[tips]]></category>

		<guid isPermaLink="false">http://www.amicalement-web.net/?p=750</guid>
		<description><![CDATA[
Pour bien configurer un serveur, il existe plusieurs façons, chaque administrateur ayant la plupart du temps sa propre conf en fonction de ses habitudes et celles de ses users.
Je vous propose ici quelques petits tips à mettre en place sur votre serveur pour voir un peu se qui s&#8217;y passe.

Connexion SSH
Il est toujours intéressant de [...]]]></description>
			<content:encoded><![CDATA[<p><img src="http://www.amicalement-web.net/wp-content/uploads/tips_config_serveur.png" alt="Tips : Configuration de serveurs" width="614" height="100" class="alignnone size-full wp-image-640" /></p>
<p>Pour bien configurer un serveur, il existe plusieurs façons, chaque administrateur ayant la plupart du temps sa propre conf en fonction de ses habitudes et celles de ses users.</p>
<p>Je vous propose ici quelques petits tips à mettre en place sur votre serveur pour voir un peu se qui s&#8217;y passe.</p>
<p><span id="more-750"></span></p>
<h3>Connexion SSH</h3>
<p>Il est toujours intéressant de connaître les <strong>connexions ssh</strong> des utilisateurs sur votre serveur, que ce soit pour des statistiques ou tout simplement surveiller les différentes connexions pour par exemple intervenir rapidement en cas d&#8217;intrusion.</p>
<p>Pour cela, il existe un moyen très simple avec l&#8217;utilisation de <strong>sshrc</strong>. Dans le man, il est dit :</p>

<div class="wp_syntax"><div class="code"><pre class="shell" style="font-family:monospace;">/etc/ssh/sshrc
Commands in this file are executed by ssh when the user logs in,
just before the user's shell (or command) is started.
See the sshd(8) manual page for more information.</pre></div></div>

<p>Nous allons donc y introduire un petit bout de script qui enverra un mail automatiquement.</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">#!/bin/sh</span>
<span style="color: #007800;">DATE</span>=<span style="color: #000000; font-weight: bold;">`</span><span style="color: #c20cb9; font-weight: bold;">date</span> <span style="color: #ff0000;">&quot;+%d-%m-%Y %H:%M&quot;</span><span style="color: #000000; font-weight: bold;">`</span>
<span style="color: #007800;">IP</span>=<span style="color: #000000; font-weight: bold;">`</span><span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #007800;">$SSH_CONNECTION</span> <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">awk</span> <span style="color: #ff0000;">'{print $1}'</span><span style="color: #000000; font-weight: bold;">`</span>
<span style="color: #007800;">REVERSE</span>=<span style="color: #000000; font-weight: bold;">`</span>dig <span style="color: #660033;">-x</span> <span style="color: #007800;">$IP</span> +short<span style="color: #000000; font-weight: bold;">`</span>
&nbsp;
<span style="color: #666666; font-style: italic;"># Message + envoi du mail</span>
<span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;USER : <span style="color: #007800;">$USER</span>
IP : <span style="color: #007800;">$IP</span>
ReverseDNS : <span style="color: #007800;">$REVERSE</span>
Date : <span style="color: #007800;">$DATE</span>
&nbsp;
&quot;</span> <span style="color: #000000; font-weight: bold;">|</span> mail <span style="color: #660033;">-s</span> <span style="color: #ff0000;">&quot;[SSH] <span style="color: #007800;">$DATE</span> - Connexion de <span style="color: #007800;">$USER</span>&quot;</span> your_email<span style="color: #000000; font-weight: bold;">@</span>domaine.com</pre></div></div>

<p>A chaque connexion ssh d&#8217;un utilisateur, vous recevrez un mail contenant diverses informations. Je souligne au passage qu&#8217;un <strong>authentification ssh par clef</strong> est toujours plus sûre ;)</p>
<h3>Modifications de fichiers</h3>
<p>Il existe plusieurs applications (plus ou moins performantes) permettant de voir les fichiers modifiés sur votre serveur. Ici, un petit script tout bête qui va vous permette de voir <strong>les modifications effectuées depuis X jours</strong> sur votre serveur dans un ou plusieurs répertoires donnés.</p>
<p>Bien entendu c&#8217;est une surveillance basique, rien de comparable avec un contrôleur d&#8217;intégrité (tripwire par exemple), mais cela peut s&#8217;avérer très pratique, notamment pour retrouver rapidement une erreur faite dans un script après une modification, mais aussi pour vérifier qu&#8217;aucune modification externe n&#8217;est faite sur des fichiers sensibles.</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">#!/bin/sh</span>
<span style="color: #007800;">DATE</span>=<span style="color: #000000; font-weight: bold;">`</span><span style="color: #c20cb9; font-weight: bold;">date</span> <span style="color: #ff0000;">&quot;+%d-%m-%Y %H:%M&quot;</span><span style="color: #000000; font-weight: bold;">`</span>
<span style="color: #007800;">LAPS</span>=<span style="color: #000000;">1</span>
<span style="color: #007800;">INFOS</span>=<span style="color: #ff0000;">&quot;Liste des fichiers modifiés :<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span>
<span style="color: #000000; font-weight: bold;">for</span> i <span style="color: #000000; font-weight: bold;">in</span> <span style="color: #000000; font-weight: bold;">`</span><span style="color: #c20cb9; font-weight: bold;">find</span> <span style="color: #000000; font-weight: bold;">/</span>etc<span style="color: #000000; font-weight: bold;">/</span> <span style="color: #660033;">-mtime</span> -<span style="color: #007800;">$LAPS</span><span style="color: #000000; font-weight: bold;">`</span> <span style="color: #000000; font-weight: bold;">`</span><span style="color: #c20cb9; font-weight: bold;">find</span> <span style="color: #000000; font-weight: bold;">/</span>home<span style="color: #000000; font-weight: bold;">/</span>user<span style="color: #000000; font-weight: bold;">/</span> <span style="color: #660033;">-mtime</span> -<span style="color: #007800;">$LAPS</span><span style="color: #000000; font-weight: bold;">`</span>; <span style="color: #000000; font-weight: bold;">do</span>
        <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #660033;">-f</span> <span style="color: #007800;">$i</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span>; <span style="color: #000000; font-weight: bold;">then</span>
                <span style="color: #007800;">INFOS</span>=<span style="color: #ff0000;">&quot;<span style="color: #007800;">$INFOS</span><span style="color: #000099; font-weight: bold;">\n</span><span style="color: #007800;">$i</span>&quot;</span>
        <span style="color: #000000; font-weight: bold;">fi</span>;
<span style="color: #000000; font-weight: bold;">done</span>
<span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #007800;">$INFOS</span> <span style="color: #000000; font-weight: bold;">|</span> mail <span style="color: #660033;">-s</span> <span style="color: #ff0000;">&quot;[MODIF] <span style="color: #007800;">$DATE</span> - Fichiers modifies&quot;</span> your_email<span style="color: #000000; font-weight: bold;">@</span>domaine.com</pre></div></div>

<p><em>N.B : Une vérification est faite pour n&#8217;avoir que les fichiers et non les répertoires + fichiers.</em></p>
<p>Dans l&#8217;instruction <strong>for i in &#8230; ; do</strong> vous pouvez mettre plusieurs répertoire en utilisant <strong>`find /rep/ -mtime -$LAPS`</strong>.</p>
<p>Une fois le script créé, il vous suffit de mettre en place un cron (ici à 23h59 tous les jours) qui le lancera automatiquement et vous avertira par mail des changements.</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #000000;">59</span> <span style="color: #000000;">23</span> <span style="color: #000000; font-weight: bold;">*</span> <span style="color: #000000; font-weight: bold;">*</span> <span style="color: #000000; font-weight: bold;">*</span> <span style="color: #000000; font-weight: bold;">/</span>path<span style="color: #000000; font-weight: bold;">/</span>to<span style="color: #000000; font-weight: bold;">/</span>your<span style="color: #000000; font-weight: bold;">/</span>script</pre></div></div>

<h3>Consommation de bande passante</h3>
<p>Savoir qui consomme le plus de bande passante sur un serveur est souvent très intéressant, je vous propose donc ici un petit script qui va vous permettre de recevoir par mail les informations concernant les hôtes qui consomment le plus de bande passante, ici sur les <strong>10000 derniers paquets</strong>.</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">#!/bin/sh</span>
<span style="color: #007800;">DATE</span>=<span style="color: #000000; font-weight: bold;">`</span><span style="color: #c20cb9; font-weight: bold;">date</span> <span style="color: #ff0000;">&quot;+%d-%m-%Y %H:%M&quot;</span><span style="color: #000000; font-weight: bold;">`</span>
tcpdump <span style="color: #660033;">-c</span> <span style="color: #000000;">10000</span> <span style="color: #660033;">-ni</span> eth0 <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">perl</span> <span style="color: #660033;">-lane</span> <span style="color: #ff0000;">'END{@keys=sort{$h{$a}  &lt;=&gt; $h{$b}} keys %h;for(reverse(@keys)){printf(&quot;%32s =&gt; %i\n&quot;, $_, $h{$_}) if($h{$_}&gt;100);}} $h{$F[1]}++;'</span> <span style="color: #000000; font-weight: bold;">|</span> mail <span style="color: #660033;">-s</span> <span style="color: #ff0000;">&quot;[BP] <span style="color: #007800;">$DATE</span> - Consommation bande passante&quot;</span> your_email<span style="color: #000000; font-weight: bold;">@</span>domaine.com</pre></div></div>

<p>A noter que pour faire plaisir à <a  href="http://blog.devorigin.fr/">Vincent</a>, j&#8217;ai utilisé du perl et non du python ^^</p>
<p>Une fois le script en place, un petit ajout dans le cron et c&#8217;est partit. Vous pouvez le faire tourner autant de fois que vous voulez, cela dépend surtout du traffic de votre serveur, ici 4 fois par jour soit toutes les 6 heures.</p>

<div class="wp_syntax"><div class="code"><pre class="shell" style="font-family:monospace;">* */6 * * * /path/to/your/script</pre></div></div>

<p>Voilà, si cela vous intéresse, il y aura surement d&#8217;autres tips à venir et surtout n&#8217;hésitez pas à demander si vous cherchez un petit truc particulier ou si vous avez des remarques.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.amicalement-web.net/tips-configuration-de-serveurs/2009/09/22/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Mini solution antispam, quand le CSS retourne l&#8217;envers du PHP</title>
		<link>http://www.amicalement-web.net/mini-solution-antispam-quand-le-css-retourne-lenvers-du-php/2009/09/03/</link>
		<comments>http://www.amicalement-web.net/mini-solution-antispam-quand-le-css-retourne-lenvers-du-php/2009/09/03/#comments</comments>
		<pubDate>Thu, 03 Sep 2009 11:00:46 +0000</pubDate>
		<dc:creator>Tim</dc:creator>
				<category><![CDATA[Astuces]]></category>
		<category><![CDATA[CSS]]></category>
		<category><![CDATA[mail]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[spam]]></category>

		<guid isPermaLink="false">http://www.amicalement-web.net/?p=711</guid>
		<description><![CDATA[
Dans le monde du web, un seul fléau uni tout le monde contre lui, le spam.
Rien à dire, en plus de ne pas être écologique, on a du mal à y voir un intérêt, pauvres humains que nous sommes. Mais quand vient le moment d&#8217;écrire son adresse mail sur un site web qui sera visible [...]]]></description>
			<content:encoded><![CDATA[<p><img src="http://www.amicalement-web.net/wp-content/uploads/stop-spam.jpg" alt="stop-spam" title="stop-spam" width="614" height="100" class="alignnone size-full wp-image-715" /><br />
Dans le monde du web, un seul fléau uni tout le monde contre lui, le spam.</p>
<p>Rien à dire, en plus de ne pas être écologique, on a du mal à y voir un intérêt, pauvres humains que nous sommes. Mais quand vient le moment d&#8217;écrire son adresse mail sur un site web qui sera visible par tout le monde, on en tremblerait presque.</p>
<p>Du coup, il existe beaucoup de techniques, l&#8217;idéal étant encore de ne pas écrire son adresse mail en toutes lettres sur son site! Mais des fois, cela reste bien pratique. Voici donc une solution très simple à mettre en place, qui sans être parfaite, assure quand même le boulot dans la majorité des cas.<span id="more-711"></span></p>
<p>A noter que cette technique est tiré du site de <a  href="http://www.symfony-project.org">Symfony himself</a>. Comme je ne l&#8217;avais jamais croisé auparavant, ca peut valoir le coup d&#8217;en parler un peu.</p>
<p>Le principe est simpliste mais bien pensé.</p>
<p>On écrit, notre adresse mail à l&#8217;envers en PHP (ou tout autre langage serveur on est d&#8217;accord) et on demande à notre CSS de l&#8217;afficher à l&#8217;envers également. Et vous l&#8217;aurez compris, l&#8217;envers de l&#8217;envers, et bien&#8230; c&#8217;est l&#8217;endroit! Fou je vous l&#8217;accorde.</p>
<p>En terme de code, voilà ce que ca donnera schématiquement:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">&lt;span class=&quot;reverse&quot;&gt;
 <span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">echo</span> <span style="color: #990000;">strrev</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'monmail@monadresse.com'</span><span style="color: #009900;">&#41;</span> <span style="color: #666666; font-style: italic;">// qui va écrire moc.esserdanom@liamnom du coup ?&gt;</span>
&lt;/span&gt;</pre></div></div>

<p>Il suffit maintenant de changer la direction de lecture de notre span reverse via CSS:</p>

<div class="wp_syntax"><div class="code"><pre class="css" style="font-family:monospace;">span<span style="color: #6666ff;">.reverse</span> <span style="color: #00AA00;">&#123;</span>
<span style="color: #000000; font-weight: bold;">direction</span><span style="color: #00AA00;">:</span>rtl<span style="color: #00AA00;">;</span>
<span style="color: #000000; font-weight: bold;">unicode-bidi</span><span style="color: #3333ff;">:bidi-</span>override<span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span></pre></div></div>

<p>Et voilà, notre adresse est maintenant lisible à l&#8217;endroit à tout le monde bénéficiant d&#8217;un support CSS, donc en théorie, tout le monde sauf, les vilains méchants robots spammeurs. Malheureusement ça reste de la théorie mais en pratique, cela reste un manque niveau accessibilité, tout comme on est pas sûr que les robots ne sauront pas interpréter le CSS très bientôt. Mais je pense que ca reste une solution à connaitre.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.amicalement-web.net/mini-solution-antispam-quand-le-css-retourne-lenvers-du-php/2009/09/03/feed/</wfw:commentRss>
		<slash:comments>9</slash:comments>
		</item>
		<item>
		<title>Yahoo aspire Smushit</title>
		<link>http://www.amicalement-web.net/yahoo-aspire-smushit/2009/09/01/</link>
		<comments>http://www.amicalement-web.net/yahoo-aspire-smushit/2009/09/01/#comments</comments>
		<pubDate>Tue, 01 Sep 2009 09:00:55 +0000</pubDate>
		<dc:creator>Tim</dc:creator>
				<category><![CDATA[Astuces]]></category>
		<category><![CDATA[image]]></category>
		<category><![CDATA[service]]></category>
		<category><![CDATA[smushit]]></category>
		<category><![CDATA[yahoo]]></category>
		<category><![CDATA[yslow]]></category>

		<guid isPermaLink="false">http://www.amicalement-web.net/?p=698</guid>
		<description><![CDATA[ SmushIt, j&#8217;en ai parlé à mes débuts, c&#8217;est un des services web que j&#8217;utilise régulièrement. Mais la semaine dernière, j&#8217;ai été étonné de voir que l&#8217;extension firefox ne fonctionnait plus, le site web aussi semblait dans les choux. La cause?  A Yahoo qui avait un peu loupé sa redirection vers la nouvelle URL [...]]]></description>
			<content:encoded><![CDATA[<p><img src="http://www.amicalement-web.net/wp-content/uploads/smushit.png" alt="smushit" title="smushit" width="614" height="100" class="alignnone size-full wp-image-707" /> <a href="pour-un-web-a-son-image-smush-it/2009/01/27/">SmushIt, j&#8217;en ai parlé à mes débuts</a>, c&#8217;est un des services web que j&#8217;utilise régulièrement. Mais la semaine dernière, j&#8217;ai été étonné de voir que l&#8217;extension firefox ne fonctionnait plus, le site web aussi semblait dans les choux. La cause?  A Yahoo qui avait un peu loupé sa redirection vers <a  href="http://developer.yahoo.com/yslow/smushit/">la nouvelle URL de ce service</a>.<br />
<span id="more-698"></span></p>
<p>Mais smushit n&#8217;est pas mort, Yahoo l&#8217;a juste intégré dans son très bon <a  href="http://developer.yahoo.com/yslow/">Yslow</a>. On peut se demander si niveau marketing, c&#8217;est une bonne idée, tant Yslow est déjà très complet. Mais je vais laisser cette analyse aux spécialistes. Pour ma part, je vais me contenter de vous montrer comment on s&#8217;en sert maintenant. Parce que même si les alternatives existent, je l&#8217;aime bien moi ce Smushit!</p>
<p>Première étape, il faut déjà retrouver l&#8217;option dans l&#8217;armada Yahoo: <a  href="http://www.amicalement-web.net/wp-content/uploads/Image-80.png"><img src="http://www.amicalement-web.net/wp-content/uploads/Image-80-300x135.png" alt="Image 80" title="Image 80" width="300" height="135" class="alignnone size-medium wp-image-704" /></a></p>
<p>Il faut donc aller sur l&#8217;onglet Tools et vous aurez accès en bas à l&#8217;option All Smush.it. Un simple clic et on vous envoie vers une nouvelle url où vous aurez accès à toutes les images de la page courante smushé!</p>
<p>Oui rien de bien compliqué, mais ca méritait d&#8217;être dit selon moi ;)</p>
]]></content:encoded>
			<wfw:commentRss>http://www.amicalement-web.net/yahoo-aspire-smushit/2009/09/01/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Doctrine: quelques petits secrets</title>
		<link>http://www.amicalement-web.net/doctrine-quelques-petits-secrets/2009/08/26/</link>
		<comments>http://www.amicalement-web.net/doctrine-quelques-petits-secrets/2009/08/26/#comments</comments>
		<pubDate>Wed, 26 Aug 2009 09:00:38 +0000</pubDate>
		<dc:creator>Tim</dc:creator>
				<category><![CDATA[Astuces]]></category>
		<category><![CDATA[astuce]]></category>
		<category><![CDATA[doctrine]]></category>
		<category><![CDATA[orm]]></category>
		<category><![CDATA[Symfony]]></category>

		<guid isPermaLink="false">http://www.amicalement-web.net/?p=582</guid>
		<description><![CDATA[ Oui, je commence à parler de Doctrine de plus en plus vu que je commence à l&#8217;utiliser à grande échelle au boulot. Et il y a un détail où j&#8217;ai encore du mal, c&#8217;est l&#8217;opacité de cet ORM comparé à son prédécesseur dans Symfony, Propel.
On se rappelle tous (enfin je parle au passé, mais [...]]]></description>
			<content:encoded><![CDATA[<p><img src="http://www.amicalement-web.net/wp-content/uploads/doctrine-secret.png" alt="doctrine-secret" title="doctrine-secret" width="614" height="100" class="alignnone size-full wp-image-640" /> Oui, je <a  href="/doctrine-et-son-behavior-geographical/2009/08/05/">commence à parler de Doctrine</a> de plus en plus vu que je commence à l&#8217;utiliser à grande échelle au boulot. Et il y a un détail où j&#8217;ai encore du mal, c&#8217;est l&#8217;opacité de cet ORM comparé à son prédécesseur dans Symfony, Propel.</p>
<p>On se rappelle tous (enfin je parle au passé, mais Propel n&#8217;est pas mort hein :p) qu&#8217;on avait tout à disposition dans le BasePeer de notre modèle, les accesseurs, les modificateurs et quasi toutes les méthodes dont on pouvait avoir besoin. Avec Doctrine, la donne a changé, tant cet ORM est codé différemment.</p>
<p>On se retrouve du coup un peu perdu parfois, sans trop savoir ce qu&#8217;on peut utiliser. Et là, il faut bien sûr jeter un œil à l&#8217;<a href="http://www.doctrine-project.org/documentation/api/1_1 ">API Doctrine</a>. Mais on le sait tous, on a pas toujours le temps! Du coup, au travers de mes recherches, j&#8217;ai trouvé quelques petites méthodes que j&#8217;utilise régulièrement et dont je vais vous parler.<br />
<span id="more-582"></span></p>
<h3>Le modèle d&#8217;exemple</h3>
<p>Afin d&#8217;illustrer au mieux les différentes fonctions, je vais prendre en exemple un modèle basique, une liste de lien:</p>

<div class="wp_syntax"><div class="code"><pre class="yaml" style="font-family:monospace;">MyLink:
  columns:
    name: { type: string(100), notnull: true }
    link: { type: string(255), notnull: true }
    description: { type: string(4000) }</pre></div></div>

<h3>Un save qui retourne un booléen</h3>
<p>Parfois, lors de petites modifications, on a pas toujours le courage de faire un try/catch sur notre transaction. TrySave répond à cette problématique, en incluant directement le try catch sur le save et en retournant un booléen, true si l&#8217;enregistrement a bien été commité, false si une exception a été levée. Bien pratique!</p>
<ul>
<li> <a  href="http://www.doctrine-project.org/Doctrine_Record/1_1#method_trysave">Documentation trySave</a></li>
<li> Exemple:

<div class="wp_syntax"><div class="code"><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> saveMyLink<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
  <span style="color: #000088;">$link</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> MyLink<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #000088;">$link</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">name</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'Amicalement-web'</span><span style="color: #339933;">;</span>
  <span style="color: #000088;">$link</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">url</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'http://www.amicalement-web.net'</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #b1b100;">return</span> <span style="color: #000088;">$link</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">trySave</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;">// Retournera true si la sauvegarde a été effectuée, false sinon</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

</li>
</ul>
<h3>Le nom de la clé primaire</h3>
<p>Oui des fois, quand on veut généraliser une méthode, on a besoin du nom de la clé primaire de notre model. (Dans le cas d&#8217;un unset en vue d&#8217;un embedForm par exemple).</p>
<ul>
<li><a  href="http://www.doctrine-project.org/Doctrine_Table/1_1#method_getidentifier">Documentation getIdentifier</a></li>
<li> Exemple:

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$link</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> MyLink<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$link</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getTable</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getIdentifier</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

</li>
</ul>
<h3>Exporter votre modèle en différents formats</h3>
<p>Que se soit pour générer des XML destinés à une application tiers, ou du json pour votre module javascript, on a souvent besoin de nos modèles dans un format d&#8217;échange. Et bien Doctrine intègre cette fonctionnalité de base, en permettant des export en tableau php, xml, yml et json.</p>
<ul>
<li><a  href="http://www.doctrine-project.org/Doctrine_Record/1_1#method_exportto">Documentation exportTo</a></li>
<li>Exemple

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$link</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> MyLink<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$link</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">name</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'Symfony Project'</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$link</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">link</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'http://www.symfony-project.org'</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$link</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: #b1b100;">echo</span> <span style="color: #000088;">$link</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">exportTo</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'xml'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// Attention par défaut exporte aussi les relations de l'objets. Il faut mettre à false, le 2e argument si on veut conserver seulement notre objet en question</span></pre></div></div>

</li>
<li>
Rendu:</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;?xml</span> <span style="color: #000066;">version</span>=<span style="color: #ff0000;">&quot;1.0&quot;</span> <span style="color: #000066;">encoding</span>=<span style="color: #ff0000;">&quot;utf-8&quot;</span><span style="color: #000000; font-weight: bold;">?&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;data<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;id<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>1<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/id<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>Symfony Project<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;url<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>http://www.symfony-project.org<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/url<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;description<span style="color: #000000; font-weight: bold;">&gt;</span></span><span style="color: #000000; font-weight: bold;">&lt;/description<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/data<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

</li>
</ul>
<h3>Importer votre modèle en différents formats</h3>
<p>Et oui parce que Doctrine fait les choses biens, la réciproque existe aussi et est toute aussi utile. Vous pouvez ainsi hydrater  un objet à partir d&#8217;un xml, json, ou yml grâce à la méthode importFrom</p>
<ul>
<li><a  href="http://www.doctrine-project.org/Doctrine_Record/1_1#method_importfrom">Documentation importFrom</a></li>
<li>Exemple:

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$json</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'{&quot;id&quot;:&quot;5&quot;,&quot;name&quot;:&quot;Doctrine Project&quot;,&quot;url&quot;:&quot;http:\/\/www.doctrine-project.org&quot;,&quot;description&quot;:&quot;Orm PHP&quot;}'</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$link</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> MyLink<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$link</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">importFrom</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'json'</span><span style="color: #339933;">,</span><span style="color: #000088;">$json</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$link</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">name</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// Affiche &quot;Doctrine Project&quot;</span></pre></div></div>

</li>
</ul>
<p>Pour ce qui est d&#8217;hydrater à partir d&#8217;un tableau php, il y a 2 méthodes pour ça: fromArray et hydrate. Leurs différences, c&#8217;est la façon de faire. La première utilise les modificateurs de notre modèle (permettant ainsi de passer par des modifications qu&#8217;on aurait pu apporter à ces méthodes &laquo;&nbsp;set&nbsp;&raquo;) alors que la seconde remplie directement les attributs de notre modèle.</p>
<ul>
<li><a  href="http://www.doctrine-project.org/Doctrine_Record/1_1#method_fromarray">Documentation fromArray</a></li>
<li><a  href="http://www.doctrine-project.org/Doctrine_Record/1_1#method_hydrate">Documentation hydrate</a></li>
<li>
Exemple:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">// On surcharge le modificateur de la propriété $url pour rajouter 'http://' devant</span>
<span style="color: #000000; font-weight: bold;">class</span> MyLink <span style="color: #000000; font-weight: bold;">extends</span> BaseMyLink
<span style="color: #009900;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> setUrl<span style="color: #009900;">&#40;</span><span style="color: #000088;">$url</span><span style="color: #009900;">&#41;</span>
  <span style="color: #009900;">&#123;</span>
    parent<span style="color: #339933;">::</span>_set<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'url'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'http://'</span><span style="color: #339933;">.</span><span style="color: #000088;">$url</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></div></div>

<p>Puis maintenant nos 2 appels:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$data</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'name'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'Amicalement Web'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'url'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'www.amicalement-web.net'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$link1</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> MyLink<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$link1</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">fromArray</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$data</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'link1='</span><span style="color: #339933;">.</span><span style="color: #000088;">$link1</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">url</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// Affiche http://www.amicalement-web.net</span>
&nbsp;
<span style="color: #000088;">$link2</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> MyLink<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$link2</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">hydrate</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$data</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'link2='</span><span style="color: #339933;">.</span><span style="color: #000088;">$link2</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">url</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// Affiche www.amicalement-web.net</span></pre></div></div>

</li>
</ul>
<h3>Post/Pre methodes</h3>
<p>Comme je l&#8217;avais utilisé dans l&#8217;exemple du behavior Geographical, Doctrine met à disposition toute une série de méthodes en post et pré traitement de nombreuses fonctionnalités comme</p>
<ul>
<li>Sérialisation</li>
<li>Désérialisation</li>
<li>Sauvegarde</li>
<li>Suppression</li>
<li>Mise à jour</li>
<li>Insertion</li>
<li>Validation</li>
<li>Hydratation</li>
</ul>
<p><a  href="http://www.doctrine-project.org/Doctrine_Record/1_2#method_postdelete">Leur documentation</a> à la suite les unes des autre.</p>
<h3>Libérer de la mémoire</h3>
<p>La performance, sans être un axe principal a mon goût, doit rester dans la tête de chacun. Pour ça, Doctrine propose une méthode pour effacer un objet ainsi que toutes ses références de la mémoire de l&#8217;ORM. A utiliser principalement dans des boucles quand on veut juste faire un traitement sur un ensemble d&#8217;objets dont on ne voudra pas se resservir ensuite.</p>
<ul>
<li><a  href="http://www.doctrine-project.org/Doctrine_Record/1_1#method_free">Documentation free</a></li>
<li>Exemple:

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">// Exemple tiré de la documentation doctrine</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;">1000</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: #000088;">$object</span> <span style="color: #339933;">=</span> createBigObject<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$object</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: #000088;">$object</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">free</span><span style="color: #009900;">&#40;</span><span style="color: #009900; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// true pour libérer même les relations</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

</li>
</ul>
<h3>Hydrater plus efficacement</h3>
<p>Et oui, on le crie pas sur tous les toits, mais on est pas obligé de récupérer des objets complets quand on fait une requête avec doctrine même si c&#8217;est le comportement par défaut.<br />
Quand on veut simplement faire de l&#8217;affichage, parfois seules les données nous intéresse et donc dans un gain de mémoire, on peut changer le mode d&#8217;hydratation:</p>
<ul>
<li>Doctrine::HYDRATE_RECORD</li>
<li>Doctrine::HYDRATE_ARRAY</li>
<li>Doctrine::HYDRATE_NONE</li>
<li>Exemple:

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$q</span> <span style="color: #339933;">=</span> Doctrine_Query<span style="color: #339933;">::</span><span style="color: #004000;">create</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">from</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'MyLink'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Comportement par défaut, retourne un tableau d'objet</span>
<span style="color: #000088;">$results</span> <span style="color: #339933;">=</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: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>Doctrine<span style="color: #339933;">::</span><span style="color: #004000;">HYDRATE_RECORD</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Retourne un tableau associatif où les clés sont les noms des champs</span>
<span style="color: #000088;">$results</span> <span style="color: #339933;">=</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: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>Doctrine<span style="color: #339933;">::</span><span style="color: #004000;">HYDRATE_ARRAY</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Retourne un simple tableau où les champs sont dans l'ordre qu'ils ont été appelés</span>
<span style="color: #000088;">$results</span> <span style="color: #339933;">=</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: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>Doctrine<span style="color: #339933;">::</span><span style="color: #004000;">HYDRATE_NONE</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

</li>
</ul>
<p>Cela reste le fruit de mes tests et études, si vous avez des remarques ou des corrections n&#8217;hésitez pas!</p>
<p>Et vous, vous avez quelque chose à partager sur les méandres de Doctrine?</p>
]]></content:encoded>
			<wfw:commentRss>http://www.amicalement-web.net/doctrine-quelques-petits-secrets/2009/08/26/feed/</wfw:commentRss>
		<slash:comments>15</slash:comments>
		</item>
		<item>
		<title>IE qui joue à cache cache avec l&#8217;ajax</title>
		<link>http://www.amicalement-web.net/ie-qui-joue-a-cache-cache-avec-lajax/2009/08/12/</link>
		<comments>http://www.amicalement-web.net/ie-qui-joue-a-cache-cache-avec-lajax/2009/08/12/#comments</comments>
		<pubDate>Wed, 12 Aug 2009 12:00:15 +0000</pubDate>
		<dc:creator>Tim</dc:creator>
				<category><![CDATA[Astuces]]></category>
		<category><![CDATA[AJAX]]></category>
		<category><![CDATA[cache]]></category>
		<category><![CDATA[IE6]]></category>
		<category><![CDATA[javascript]]></category>

		<guid isPermaLink="false">http://www.amicalement-web.net/?p=563</guid>
		<description><![CDATA[ Oui je sais encore un nouveau billet sans le nouveau thème. Mais il est bien fini à 90%. Il manque quelques tests et un petit module qui me tient à coeur.
Bref, en attendant, pas de raison que je ne publie pas! Voici donc une mini astuce, sur un problème rencontré cette semaine, qui m&#8217;aura [...]]]></description>
			<content:encoded><![CDATA[<p><img src="http://www.amicalement-web.net/wp-content/uploads/applications-other.png" alt="applications-other" title="applications-other" width="48" height="48" class="alignleft size-full wp-image-30" /> Oui je sais encore un nouveau billet sans le nouveau thème. Mais il est bien fini à 90%. Il manque quelques tests et un petit module qui me tient à coeur.</p>
<p>Bref, en attendant, pas de raison que je ne publie pas! Voici donc une mini astuce, sur un problème rencontré cette semaine, qui m&#8217;aura un peu usé, surtout quand on voit la solution finale.</p>
<p>On va donc parler de IE et de l&#8217;AJAX.<br />
<span id="more-563"></span><br />
Commençons par poser le décor comme d&#8217;habitude. Quand on fait de l&#8217;AJAX avec symfony, il n&#8217;est pas rare d&#8217;essayer de le faire non intrusif (c&#8217;est même conseillé, mais ça dépend si le site a un intérêt à fonctionner sans javascript), et donc de rendre une même action symfony différemment selon qu&#8217;on l&#8217;attaque en normal ou en AJAX.</p>
<p>Pour cela, on utilise donc un code proche de celui-ci:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><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;">isXmlHttpRequest</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;">return</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">renderPartial</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'panier/widget'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Au final pour le navigateur, la requête reste la même. Demandez donc maintenant à un navigateur obsolète du genre IE de gérer se genre de mécanisme.<br />
Vous vous attendez au résultat, et je confirme pour vous. De façon non systématique, mais il arrive que si vous chargez d&#8217;abord l&#8217;action par un appel GET classique et que vous exécuez ensuite une requête ajax sur cette même action, IE a tendance à vous renvoyer la page entière et non pas seulement notre partial comme on l&#8217;a défini.</p>
<p>On peut penser que le souci vient de nos entêtes non détectées, mais on s&#8217;aperçoit que d&#8217;autres requêtes AJAX fonctionne&#8230; Et là, on essaye une petite ligne jquery avant notre requête AJAX:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">$.<span style="color: #660066;">ajaxSetup</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>
    cache<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">false</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Et par magie, IE arrête son jeu de cache cache!</p>
<p>C&#8217;est tout bête, mais il fallait le savoir.</p>
<p>Maintenant, je le sais, et vous aussi ;)</p>
]]></content:encoded>
			<wfw:commentRss>http://www.amicalement-web.net/ie-qui-joue-a-cache-cache-avec-lajax/2009/08/12/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Symfony: Personnaliser le nom du fichier lors d&#8217;un upload avec sfWidgetFormInputFileEditable</title>
		<link>http://www.amicalement-web.net/symfony-personnaliser-le-nom-du-fichier-lors-dun-upload-avec-sfwidgetforminputfileeditable/2009/07/29/</link>
		<comments>http://www.amicalement-web.net/symfony-personnaliser-le-nom-du-fichier-lors-dun-upload-avec-sfwidgetforminputfileeditable/2009/07/29/#comments</comments>
		<pubDate>Wed, 29 Jul 2009 09:00:10 +0000</pubDate>
		<dc:creator>Tim</dc:creator>
				<category><![CDATA[Astuces]]></category>
		<category><![CDATA[astuce]]></category>
		<category><![CDATA[Symfony]]></category>
		<category><![CDATA[upload]]></category>

		<guid isPermaLink="false">http://www.amicalement-web.net/?p=477</guid>
		<description><![CDATA[ On emploie tous je pense, assez couramment maintenant, le widget sfWidgetFormInputFileEditable qui permet de rajouter quelques fonctionnalités à un widget d&#8217;upload classique, en l&#8217;occurrence visualisation et suppression. Le souci que j&#8217;ai rencontré récemment, c&#8217;est par contre la personnalisation du nom du fichier ainsi généré.
En effet, par défaut celui-ci est une empreinte sha1 généré aléatoirement, [...]]]></description>
			<content:encoded><![CDATA[<p><img src="http://www.amicalement-web.net/wp-content/uploads/system-software-update.png" alt="system-software-update" title="system-software-update" width="48" height="48" class="alignleft size-full wp-image-40" /> On emploie tous je pense, assez couramment maintenant, le widget <a  href="http://www.symfony-project.org/api/1_2/sfWidgetFormInputFileEditable" class="extern">sfWidgetFormInputFileEditable</a> qui permet de rajouter quelques fonctionnalités à un widget d&#8217;upload classique, en l&#8217;occurrence visualisation et suppression. Le souci que j&#8217;ai rencontré récemment, c&#8217;est par contre la personnalisation du nom du fichier ainsi généré.</p>
<p>En effet, par défaut celui-ci est une empreinte sha1 généré aléatoirement, ce qui convient pour la majeure partie des cas, mais parfois ne suffit pas. Mais là encore Symfony étonne par le mécanisme mis en place pour contourner cette problématique, encore faut-il le savoir malheureusement.<br />
<span id="more-477"></span></p>
<p>Nous allons prendre un cas classique, une class Medias avec un champ path qui stockera le nom du fichier ainsi uploadé:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">class</span> MediasForm <span style="color: #000000; font-weight: bold;">extends</span> BaseMediasForm
<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;">'path'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> sfWidgetFormInputFileEditable<span style="color: #009900;">&#40;</span><span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
      <span style="color: #0000ff;">'file_src'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'/uploads/medias/'</span><span style="color: #339933;">.</span><span style="color: #000088;">$this</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;">getPath</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
      <span style="color: #0000ff;">'is_image'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #339933;">,</span>
      <span style="color: #0000ff;">'edit_mode'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #339933;">!</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">isNew</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
      <span style="color: #0000ff;">'with_delete'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #009900; 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;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">validatorSchema</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'path'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> sfValidatorFile<span style="color: #009900;">&#40;</span><span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
    	<span style="color: #0000ff;">'required'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #339933;">,</span>
    	<span style="color: #0000ff;">'path'</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;">'sf_upload_dir'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">'/medias'</span><span style="color: #339933;">,</span>
        <span style="color: #0000ff;">'mime_types'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'web_images'</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;">validatorSchema</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'path_delete'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> sfValidatorBoolean<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span></pre></div></div>

<p>Maintenant, si on veut avoir la main sur le nom du fichier généré, il suffit tout simplement dans la class Medias de rajouter la méthode suivante:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">// Ou Path est le nom du champ ciblé lors de l'upload</span>
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> generatePathFilename<span style="color: #009900;">&#40;</span>sfValidatedFile <span style="color: #000088;">$file</span><span style="color: #009900;">&#41;</span>
  <span style="color: #009900;">&#123;</span>
   <span style="color: #666666; font-style: italic;">// On a maintenant accès à notre fichier, on peut donc lui donner un nom basé sur son id ou son slug ou tout autre chose.</span>
    <span style="color: #b1b100;">return</span> <span style="color: #000088;">$this</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: #000088;">$file</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getExtension</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$file</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getOriginalExtension</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></pre></div></div>

<p>Tout simplement! C&#8217;est ce genre de détails qui me font apprécier la constante découverte de ce framework ;)</p>
]]></content:encoded>
			<wfw:commentRss>http://www.amicalement-web.net/symfony-personnaliser-le-nom-du-fichier-lors-dun-upload-avec-sfwidgetforminputfileeditable/2009/07/29/feed/</wfw:commentRss>
		<slash:comments>14</slash:comments>
		</item>
		<item>
		<title>Symfony: Faites passer le message</title>
		<link>http://www.amicalement-web.net/symfony-faites-passer-le-message/2009/07/15/</link>
		<comments>http://www.amicalement-web.net/symfony-faites-passer-le-message/2009/07/15/#comments</comments>
		<pubDate>Wed, 15 Jul 2009 16:00:46 +0000</pubDate>
		<dc:creator>Tim</dc:creator>
				<category><![CDATA[Astuces]]></category>
		<category><![CDATA[communication]]></category>
		<category><![CDATA[ihm]]></category>
		<category><![CDATA[Symfony]]></category>
		<category><![CDATA[user]]></category>

		<guid isPermaLink="false">http://www.amicalement-web.net/?p=448</guid>
		<description><![CDATA[ Petit retour sur Symfony pour débattre d&#8217;un sujet que je traine dans mes cartons depuis le début de ce blog quasiment mais dont je n&#8217;avais jamais vraiment eu le temps de parler: La communication entre l&#8217;application et l&#8217;utilisateur.
Je profite donc de n&#8217;avoir pas de sujets brûlants à vous faire part, pour attaquer ce billet.
En [...]]]></description>
			<content:encoded><![CDATA[<p><img src="http://www.amicalement-web.net/wp-content/uploads/accessories-text-editor.png" alt="accessories-text-editor" title="accessories-text-editor" width="48" height="48" class="alignleft size-full wp-image-28" /> Petit retour sur Symfony pour débattre d&#8217;un sujet que je traine dans mes cartons depuis le début de ce blog quasiment mais dont je n&#8217;avais jamais vraiment eu le temps de parler: La communication entre l&#8217;application et l&#8217;utilisateur.</p>
<p>Je profite donc de n&#8217;avoir pas de sujets brûlants à vous faire part, pour attaquer ce billet.</p>
<p>En effet, c&#8217;est souvent un détail qu&#8217;on oublie très vite et il n&#8217;y a qu&#8217;à voir les maquettes qu&#8217;on reçoit pour nos devs, je pense que les bon élèves qui pensent à faire un petit mockup d&#8217;un message utilisateur sont pas forcément légions. Et pourtant, dans une optique purement IHM, l&#8217;application se doit de communiquer avec l&#8217;utilisateur pour montrer qu&#8217;il comprend bien la requête. </p>
<p>Difficilement d&#8217;imaginer d&#8217;appuyer sur un bouton et ne pas savoir si l&#8217;application a bien saisi et effectué notre demande. Et pourtant ce n&#8217;est pas rare de croiser encore de telles applications même si la tendance s&#8217;est inversée depuis l&#8217;avènement du deux point zéro.</p>
<p>Le souci, c&#8217;est que c&#8217;est souvent un peu fastidieux à mettre en place. Heureusement, nous on utilise Symfony et il va nous simplifier les choses pour peu qu&#8217;on pose les bonnes bases.<br />
<span id="more-448"></span><br />
Je ne suis pas allé chercher midi à quatorze heures pour implémenter un système que je reproduis dans tous mes projets, je me suis simplement inspiré de l&#8217;admin generator.</p>
<p>Première phase, parmi toutes les possibilités de Symfony, laquelle utiliser pour ce genre de message?<br />
Commençons par cerner nos besoins:</p>
<ul>
<li>Il faut pouvoir passer un message à l&#8217;utilisateur après une requête de sa part</li>
<li>qu&#8217;elle que soit la page où il se retrouvera après</li>
<li>seulement sur cette page</li>
<li>pouvoir distinguer 2 types de messages au minimum: information et erreur
</ul>
<p>On se rend vite compte qu&#8217;il va falloir utiliser les sessions et en jetant un oeil à la classe qui gère ces dernières, <a  href="http://www.symfony-project.org/api/1_2/sfUser" class="extern">sfUser</a>, on découvre la méthode <a  href="http://www.symfony-project.org/api/1_2/sfUser#method_setflash">setFlash</a> qui semble parfaitement correspondre à nos besoins et sans aucun effort, car maintenant le principe revient à afficher les messages, vu que le setFlash s&#8217;occupera de les faire disparaitre une fois qu&#8217;on les aura affichés.</p>
<p>Pour ce faire, et bien on va utiliser tout bêtement un partial qu&#8217;on va inclure dans notre layout.php! Ainsi, il sera appelé pour chacune de nos pages. En général on obtient donc quelque chose comme ceci dans notre layout:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span> include_partial<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'global/messages'</span><span style="color: #009900;">&#41;</span> <span style="color: #666666; font-style: italic;">// global défini le répertoire ou se trouve le layout.php ?&gt;</span>
<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$sf_content</span> <span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p>Et à l&#8217;intérieur de ce partial, voilà ce qu&#8217;il suffit de mettre:</p>

<div class="wp_syntax"><div class="code"><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;">$sf_user</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">hasFlash</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'error'</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">||</span> <span style="color: #000088;">$sf_user</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">hasFlash</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'notice'</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>
  &lt;div id=&quot;feedback&quot;&gt;
    <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;">$sf_user</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">hasFlash</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'error'</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>
    	&lt;p class=&quot;error-box&quot;&gt;
    		<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$sf_user</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getFlash</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'error'</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>
    	&lt;/p&gt;
    <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>
&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: #000088;">$sf_user</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">hasFlash</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'notice'</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>
    	&lt;p class=&quot;notice-box&quot;&gt;
    		<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$sf_user</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getFlash</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'notice'</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>
    	&lt;/p&gt;
    <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>
  &lt;/div&gt;
<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></div></div>

<p>Voilà, on teste la présence de flash de type &#8216;error&#8217; et/ou &#8216;notice&#8217; et les affiche le cas échéant.</p>
<p>Maintenant dans n&#8217;importe quelle action de notre application, un simple appel comme ci-dessous suffit:</p>

<div class="wp_syntax"><div class="code"><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;">setFlash</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'error'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'Une erreur est survenue'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// Pour les erreurs</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;">'Inscription prise en compte'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// Pour les messages</span></pre></div></div>

<p>L&#8217;étape suivante pourrait être de découpler le type d&#8217;erreur de leur namespace utilisé, mais ça je vous laisse faire ;)</p>
]]></content:encoded>
			<wfw:commentRss>http://www.amicalement-web.net/symfony-faites-passer-le-message/2009/07/15/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
	</channel>
</rss>
