<?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; behavior</title>
	<atom:link href="http://www.amicalement-web.net/tag/behavior/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>Wed, 04 Jan 2012 14:54:26 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Actualité Symfony, Doctrine et Propel</title>
		<link>http://www.amicalement-web.net/actualite-symfony-doctrine-et-propel/2009/09/21/</link>
		<comments>http://www.amicalement-web.net/actualite-symfony-doctrine-et-propel/2009/09/21/#comments</comments>
		<pubDate>Mon, 21 Sep 2009 09:00:32 +0000</pubDate>
		<dc:creator>Tim</dc:creator>
				<category><![CDATA[Actualité]]></category>
		<category><![CDATA[behavior]]></category>
		<category><![CDATA[doctrine]]></category>
		<category><![CDATA[propel]]></category>
		<category><![CDATA[Symfony]]></category>

		<guid isPermaLink="false">http://www.amicalement-web.net/?p=659</guid>
		<description><![CDATA[Comme souvent après la rentrée, on assiste au débarquement des nouvelles versions en tout genre, comme Apple avec Snow Leopard ou Karmic Koala chez Canonical (bon ok eux c&#8217;est tous les mois d&#8217;octobre :p). Et forcément, chez les développeurs Symfony, ca frétille d&#8217;impatience car chaque jour nous rapproche de la sortie de la version 1.3 [...]]]></description>
			<content:encoded><![CDATA[<p><img src="http://www.amicalement-web.net/wp-content/uploads/journal-cafe-netbook.png" alt="journal-cafe-netbook" title="journal-cafe-netbook" width="614" height="100" class="alignnone size-full wp-image-745" /> Comme souvent après la rentrée, on assiste au débarquement des nouvelles versions en tout genre, comme Apple avec Snow Leopard ou Karmic Koala chez Canonical (bon ok eux c&#8217;est tous les mois d&#8217;octobre :p). </p>
<p>Et forcément, chez les développeurs Symfony, ca frétille d&#8217;impatience car chaque jour nous rapproche de la sortie de la version 1.3 de notre framework PHP préféré, dernière release majeure avant le passage en 2.0 (oué pour le coup, ils sont pas encore à la mode).</p>
<p>Je me suis dis que ca valait le coup de faire un petit tour d&#8217;horizon des nouveautés attendues (au moins par moi) que se soit Symfony, Doctrine et même Propel:<br />
<span id="more-659"></span></p>
<h3>Propel</h3>
<p>On commence avec le feuilleton de l&#8217;été. Propel, mourra, mourra pas?</p>
<p>L&#8217;info a très vite circulé, et Propel a été enterré très vite après <a  href="http://groups.google.com/group/propel-development/msg/829895b081ec7873">l&#8217;annonce de son créateur Hans Lellelid</a> d&#8217;arrêter là les frais. </p>
<p>Mais dans son message,<strong> il y avait bien un appel au passage de relai</strong>, appel auquel François Zaninotto (himself, ex de la core team Symfony) et Sven Teitje ont répondu. Le premier prenant en charge l&#8217;évolution de la branche 1.x et le second le développement de la nouvelle branche 2.x.</p>
<p>D&#8217;ailleurs ils n&#8217;ont pas trainé car <strong>on parle déjà de Propel 1.4</strong>! On suivra bien sûr ça de très près.</p>
<h3>Doctrine</h3>
<p>Avec la mode 2.0, Mister Wage prépare lui aussi une version 2.0 de son ORM Doctrine. Peu d&#8217;infos encore si ce n&#8217;est qu&#8217;il sera plus beau, plus fort et surtout plus rapide! Grâce à PHP 5.3 en particulier.<br />
Mais on pourra également retrouver <strong>des messages d&#8217;erreurs localisés</strong>! Et ça c&#8217;est la classe.</p>
<p>Mais ceci reste du futur pas proche. Et on va s&#8217;intéresser plutôt à la version 1.2, celle qui accompagnera la version 1.3 de Symfony (oui j&#8217;avoue, ils auraient pu se mettre d&#8217;accord sur les numéros de version, ça aurait été plus simple).</p>
<p>Et la nouveauté majeure pour moi, c&#8217;est la<strong> possibilité d&#8217;étendre les classe de Doctrine</strong> facilement et en particulier Doctrine_Query et Doctrine_Collection. On pourra alors généraliser certains comportements et ajouter de nouvelles fonctionnalités comme des tri personnalisés dans les collections ou adapter <a  href="http://prendreuncafe.com/blog/post/Optimize-your-Doctrine-Workflow-with-Specialized-Queries">l&#8217;idée de NiKo</a> pour les query.</p>
<p>On notera également, l&#8217;ajout d&#8217;un<strong> système d&#8217;extension</strong> directement via doctrine qui sont en fait des behaviors. Vous retrouverez <a  href="http://www.doctrine-project.org/extensions">la liste sur le site lui même</a>. J&#8217;ai d&#8217;ailleurs de mon côté, une petite idée d&#8217;extension, mais on en reparlera le moment voulu ;)</p>
<h3>Symfony</h3>
<p>Gros programme pour cette dernière release (comme annoncée, la 1.4 ne sera qu&#8217;une 1.3 débarrassé des méthodes/classes dépréciées). Beaucoup de choses, mais voici un résumé de celles que j&#8217;attends vraiment:</p>
<ul>
<li>La vrai nouveauté, un<strong> Mailer de série</strong>! Basé sur Swift, très bonne librairie, cela fera toujours un plugin de moins à utiliser</li>
<li>Le système de formulaire pluggué sur le système d&#8217;évènement avec <strong>4 nouveaux évènements</strong> qui va permettre enfin de généraliser des comportements pour certains formulaires très facilement.</li>
<li>Pouvoir <strong>ne pas générer les formulaires ou filtres d&#8217;un modèle</strong> lors de sa définition dans le schema.yml (seulement doctrine). Nan parce que, c&#8217;est vrai que des fois, on en a vraiment pas besoin.</li>
<li>Petit détail enfin corrigé, les <strong>pager intègrent enfin les interfaces Countable et Iterator</strong>.</li>
<li>Autre détail, à la génération automatique d&#8217;un label d&#8217;un champ de formulaire, <strong>les _id seront supprimés</strong>! Ca permettra d&#8217;avoir quelque chose de présentable plus facilement, pcq les _id nous on les voit pas, mais les clients, ca leur saute toujours aux yeux ;)</li>
<li>Petite révolution dans les définitions de formulaire. Actuellement, il faut retirer du formulaire les champs qu&#8217;on ne souhaite pas voir s&#8217;afficher. Mais le coup classique du &laquo;&nbsp;je rajoute un champ et il apparait automatiquement alors que je voulais pas&nbsp;&raquo; ça agace. Dans la 1.3, ca sera donc l&#8217;inverse,<strong> il faudra spécifier les champs qu&#8217;on voudra afficher</strong> grâce à la nouvelle méthode useFields</li>
<li>Niveau CLI aussi beaucoup de nouveautés, dont une assez formidable, la possibilité de <strong>rejouer seulement les tests qui auront échoué</strong>! Il suffira de rajouter l&#8217;option &#8211;only-failed.</li>
<li>Pour Doctrine aussi, beaucoup de rajouts bien pratiques, comme la possibilité de regénérer/supprimer les fichiers liés à un model donné. Et même de <strong>resynchroniser tous les modèles avec le schema.yml</strong> (entendre par là qu&#8217;il supprimera ceux qui n&#8217;existe plus). Des petits gains de temps qui ne sont pas de refus, surtout dans les débuts d&#8217;un nouveau projet.</li>
<li>Et bien sûr, la plus attendue pour la fin. <a  href="http://www.symfony-project.org/blog/2009/06/10/new-in-symfony-1-3-project-creation-customization">Fabien en avait déjà parlé sur le blog</a>, la <strong>possibilité de créer un installer</strong> pour la génération de nouveau projet qui fera automatiquement toutes les tâches de personnalisation que vous faisiez avant à la main ou en maintenant votre skeleton de projet.</li>
</ul>
<p>En tout cas, ça fait envie, et ca va dans le bon sens selon moi. Pour tous les détails:</p>
<ul>
<li><a  href="http://www.slideshare.net/jwage/symfony-13-doctrine-12">Slides de Jonathan Wage  sur Symfony 1.3 et Doctrine 1.2</a></li>
<li><a  href="http://www.symfony-project.org/tutorial/1_3/en/whats-new">Page officielle des nouveautés de la v1.3</a></li>
</ul>
<p><small>Crédit photo: http://www.flickr.com/photos/mfobrien</small></p>
]]></content:encoded>
			<wfw:commentRss>http://www.amicalement-web.net/actualite-symfony-doctrine-et-propel/2009/09/21/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Développement Web: Les bons plans de la semaine #11</title>
		<link>http://www.amicalement-web.net/developpement-web-les-bons-plans-de-la-semaine-11/2009/08/17/</link>
		<comments>http://www.amicalement-web.net/developpement-web-les-bons-plans-de-la-semaine-11/2009/08/17/#comments</comments>
		<pubDate>Mon, 17 Aug 2009 09:00:35 +0000</pubDate>
		<dc:creator>Tim</dc:creator>
				<category><![CDATA[Bons plans]]></category>
		<category><![CDATA[behavior]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[jquery]]></category>
		<category><![CDATA[json]]></category>
		<category><![CDATA[Symfony]]></category>
		<category><![CDATA[twitter]]></category>
		<category><![CDATA[wordpress]]></category>

		<guid isPermaLink="false">http://www.amicalement-web.net/?p=565</guid>
		<description><![CDATA[Une nouvelle semaine qui commence. En espérant que ce billet soit le dernier sous ce thème graphique! D&#8217;ailleurs n&#8217;oubliez pas que vous pouvez encore donner votre avis sur les modifications que vous aimeriez voir sur amicalement-web. Pour vous ouvrir un peu l&#8217;esprit en ce lundi matin toujours très difficile, voici ma liste de bons liens [...]]]></description>
			<content:encoded><![CDATA[<p><img src="http://www.amicalement-web.net/wp-content/uploads/computer.png" alt="computer" title="computer" width="48" height="48" class="alignleft size-full wp-image-32" /> Une nouvelle semaine qui commence. En espérant que ce billet soit le dernier sous ce thème graphique! D&#8217;ailleurs n&#8217;oubliez pas que vous pouvez encore <a  href="/ce-que-vous-aimeriez-changer/2009/08/14/">donner votre avis sur les modifications que vous aimeriez voir sur amicalement-web</a>.<br />
<span id="more-565"></span><br />
Pour vous ouvrir un peu l&#8217;esprit en ce lundi matin toujours très difficile, voici ma liste de bons liens à parcourir sans plus attendre:</p>
<ul>
<li><a  href="http://www.neathighlighter.com/">Un colorateur synthaxique multilangage</a> (via <a  href="http://blogmotion.fr/internet/colorisateur-syntaxique-3791">blogmotion</a>)<br />
Le monde des colorateur synthaxique, c&#8217;est un peu comme les modalbox en jquery, il y en a partout. Pourtant celui-ci a encore réussi à innover, dans le sens où il arrive à colorer chaque langage présent dans une même page, très pratique pour le cas de code web alliant html, css, javascript et php. On attend une version portable avec impatience.</li>
<li><a  href="http://code.centresource.com/2009/08/how-to-write-a-doctrine-behavior/">Comment écrire un behavior pour doctrine</a><br />
Et oui, doctrine on va en parler de plus en plus. Et avec ces behavior bien pratique, on va forcément avoir la question, comment qu&#8217;on fait? J&#8217;anticipe donc! A noter qu&#8217;une rumeur suggèrerait que notre ami <a  href="http://blog.devorigin.fr/">Vincent pourrait en parler prochainement sur son blog</a></li>
<li><a  href="http://ennuidesign.com/blog/JSON%3A+What+It+Is%2C+How+It+Works%2C+and+How+to+Use+It/">JSON? Comment ca marche?</a><br />
Avec l&#8217;avènement du javascript, un format de données est sorti de sa cachette: le JSON. Il permet de modéliser des objets javascript, très simplement et est maintenant utilisé régulièrement comme format d&#8217;échange en ajax. Mais pour en savoir plus, il suffit de jeter un oeil au lien ci-dessus.</li>
<li><a  href="http://www.funstaff.ch/2009/08/14/passage-de-votre-site-en-maintenance-lors-de-vos-mises-a-jour">Passez votre site symfony en maintenance</a><br />
Un site web vivant, c&#8217;est un site qu&#8217;on met à jour. Mais quand ces modifications peuvent prendre du temps, plutôt que de donner un mauvais rendu ou des erreurs à vos visiteurs, il est préférable de leur fournir une page d&#8217;attente. Ce billet donne la marche à suivre pour le faire sous Symfony!</li>
<li><a  href="http://theodin.co.uk/blog/javascript/tweetable-jquery-plugin.html">Un plugin jquery pour récupérer des tweets très facilement</a><br />
C&#8217;est la mode, mettre ses derniers tweets sur son site. Voilà un moyen très simple d&#8217;y parvenir</li>
<li><a  href="http://theodin.co.uk/blog/development/truncatable-jquery-plugin.html">Un plugin jquery pour tronquer des textes</a><br />
Du même auteur, voici un autre plugin jquery qui permet de tronquer très facilement des textes sur notre site.</li>
<li><a  href="http://www.robertspeer.com/blog/symfony-refactor-of-the-zend-quick-start-tutorial/?dzone">Comparons Zend et Symfony</a><br />
Un très bon comparatif qui sort des benchmarks classiques où l&#8217;auteur montre les différences lors d&#8217;une création d&#8217;un nouveau projet sous ces 2 framework</li>
<li><a  href="http://www.wpbeginner.com/wp-tutorials/11-vital-tips-and-hacks-to-protect-your-wordpress-admin-area/">11 astuces pour protéger votre admin wordpress</a><br />
En plein d&#8217;actualité avec la faille découverte dans wordpress 2.8.3, voici un récapitulatif des meilleures techniques pour protéger votre admin wordpress.</li>
</ul>
<p>Une semaine très riche j&#8217;ai trouvé, comme quoi ca sentirait presque la fin des vacances déjà&#8230; </p>
<p>A demain si tout va bien sous de nouvelles couleurs!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.amicalement-web.net/developpement-web-les-bons-plans-de-la-semaine-11/2009/08/17/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Doctrine et son Behavior Geographical</title>
		<link>http://www.amicalement-web.net/doctrine-et-son-behavior-geographical/2009/08/05/</link>
		<comments>http://www.amicalement-web.net/doctrine-et-son-behavior-geographical/2009/08/05/#comments</comments>
		<pubDate>Wed, 05 Aug 2009 12:00:18 +0000</pubDate>
		<dc:creator>Tim</dc:creator>
				<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[behavior]]></category>
		<category><![CDATA[doctrine]]></category>
		<category><![CDATA[Symfony]]></category>

		<guid isPermaLink="false">http://www.amicalement-web.net/?p=485</guid>
		<description><![CDATA[Retour un peu à la technique, mais n&#8217;oubliez pas, il vous reste seulement jusqu&#8217;à demain pour gagner une BD. En ce qui nous concerne, avec les annonces faites lors de la dernière Symfony Conference Live, il faut bien commencer à se lancer dans Doctrine. Et c&#8217;est l&#8217;occasion de découvrir petit à petit cet ORM qui [...]]]></description>
			<content:encoded><![CDATA[<p><img src="http://www.amicalement-web.net/wp-content/uploads/edit-cut.png" alt="edit-cut" title="edit-cut" width="48" height="48" class="alignleft size-full wp-image-34" /> Retour un peu à la technique, mais n&#8217;oubliez pas, il vous reste seulement <a  href="/6-mois-deja-bilan-passe-objectifs-futurs-et-des-cadeaux/2009/07/31/">jusqu&#8217;à demain pour gagner une BD</a>.</p>
<p>En ce qui nous concerne, avec les annonces faites lors de la dernière Symfony Conference Live, il faut bien commencer à se lancer dans Doctrine. Et c&#8217;est l&#8217;occasion de découvrir petit à petit cet ORM qui change beaucoup de Propel, pas toujours en bien à mon goût, mais il y a quand même des choses très bien pensées.<br />
En parcourant la <a  class="extern" href="http://www.doctrine-project.org/documentation">doc</a>, je suis donc tombé sur un behavior que j&#8217;ai trouvé fort intéressant: <a  class="extern" href="http://www.doctrine-project.org/documentation/manual/1_1/en/behaviors#core-behaviors:geographical">Geographical</a>.</p>
<p>Je me suis donc laissé tenté par un test de celui-ci!<br />
<span id="more-485"></span><br />
Le principe est simple, il rajoute 2 champs à notre table, latitude et longitude, qui outre le fait de donner les coordonnées GPS de notre adresse, pouvant ainsi alimenter une googlemap, permet également de faire des calculs de distance (à vol d&#8217;oiseau évidemment).<br />
Et d&#8217;ailleurs apparemment, c&#8217;est sa seule fonction, car le remplissage des champs est à notre charge. Mais ça reste une fonctionnalité intéressante.</p>
<p>On se lance donc dans une application à un projet, symfony bien évidemment, avec une table classique d&#8217;adresse avec le behavior de déclaré.<br />
Voici notre schema.yml</p>

<div class="wp_syntax"><div class="code"><pre class="yaml" style="font-family:monospace;">adresses:
  actAs: [ Geographical]
  tableName: adresses
  columns:
    aid: { type: integer(4), primary: true,  autoincrement: true }
    adresse1: { type: string(255), notnull: true }
    adresse2: { type: string(255) }
    cp: { type: string(5), notnull: true }
    ville: { type: string(100), notnull: true }</pre></div></div>

<p>Ce qui donne en base une fois notre table créé:</p>
<pre>+------------+--------------+------+-----+---------+----------------+
| Field      | Type         | Null | Key | Default | Extra          |
+------------+--------------+------+-----+---------+----------------+
| aid        | int(11)      | NO   | PRI | NULL    | auto_increment |
| adresse1   | varchar(255) | NO   |     | NULL    |                |
| adresse2   | varchar(255) | YES  |     | NULL    |                |
| cp         | varchar(5)   | NO   |     | NULL    |                |
| ville      | varchar(100) | NO   |     | NULL    |                |
| latitude   | double       | YES  |     | NULL    |                |
| longitude  | double       | YES  |     | NULL    |                |
+------------+--------------+------+-----+---------+----------------+</pre>
<p>Maintenant que les bases sont posées, on va créer une petite classe rapide qui va nous permettre d&#8217;alimenter les champs latitude et longitude de notre table. Pour cela on va se servir de l&#8217;inévitable googlemap. Il vous faudra pour cela une clé pour utiliser leur API.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">class</span> Geoloc
<span style="color: #009900;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">protected</span> 
    <span style="color: #000088;">$adr</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">null</span><span style="color: #339933;">,</span>
    <span style="color: #000088;">$key</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">null</span><span style="color: #339933;">,</span>
    <span style="color: #000088;">$url</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">null</span><span style="color: #339933;">,</span>
    <span style="color: #000088;">$context</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">null</span><span style="color: #339933;">,</span>
    <span style="color: #000088;">$format</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">null</span><span style="color: #339933;">,</span>
    <span style="color: #000088;">$x</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">null</span><span style="color: #339933;">,</span>
    <span style="color: #000088;">$y</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">null</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> execute<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
  <span style="color: #009900;">&#123;</span>
    <span style="color: #666666; font-style: italic;">// On utilise un stream_context pour avoir la main sur certaines options plus facilement, comme le timeout de notre connexion</span>
    <span style="color: #000088;">$context</span> <span style="color: #339933;">=</span> <span style="color: #990000;">stream_context_create</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
      <span style="color: #0000ff;">'http'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
        <span style="color: #0000ff;">'method'</span>  <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'GET'</span><span style="color: #339933;">,</span>
        <span style="color: #0000ff;">'header'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">&quot;Content-type: application/x-www-form-urlencoded<span style="color: #000099; font-weight: bold;">\r</span><span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">,</span>
        <span style="color: #0000ff;">'timeout'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">5</span><span style="color: #339933;">,</span>
      <span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
    <span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">// Google n'autorise que les requêtes en GET, on construit donc notre liste de paramètre prêt à emploi</span>
    <span style="color: #000088;">$param</span> <span style="color: #339933;">=</span> <span style="color: #990000;">http_build_query</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
          <span style="color: #0000ff;">'q'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">adr</span><span style="color: #339933;">,</span>
          <span style="color: #0000ff;">'output'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">format</span><span style="color: #339933;">,</span>
          <span style="color: #0000ff;">'oe'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'utf8'</span><span style="color: #339933;">,</span>
          <span style="color: #0000ff;">'sensor'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'false'</span><span style="color: #339933;">,</span>
          <span style="color: #0000ff;">'key'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">key</span>
    <span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000088;">$content</span> <span style="color: #339933;">=</span> <span style="color: #990000;">file_get_contents</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">url</span><span style="color: #339933;">.</span><span style="color: #0000ff;">'?'</span><span style="color: #339933;">.</span><span style="color: #000088;">$param</span><span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #339933;">,</span> <span style="color: #000088;">$context</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$content</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
      <span style="color: #666666; font-style: italic;">// Le traitement est prévu ici pour du csv</span>
      <span style="color: #000088;">$retour</span> <span style="color: #339933;">=</span> <span style="color: #990000;">explode</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">','</span><span style="color: #339933;">,</span><span style="color: #000088;">$content</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
      <span style="color: #666666; font-style: italic;">// Si le retour est bon, on récupère les coordonnées</span>
      <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$retour</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">==</span> <span style="color: #0000ff;">'200'</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;">x</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$retour</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">2</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">y</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$retour</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">3</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #b1b100;">return</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">x</span><span style="color: #339933;">,</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">y</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #b1b100;">return</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> getX<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;">x</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> getY<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;">y</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Voilà, il suffit de placer cette classe dans un dossier Lib de Symfony, où on bon vous semble. Maintenant on va créer dans notre fichier adresses.class.php 2 méthodes:<br />
- la première qui lancera la mise à jour des champs via google<br />
- la deuxième qui automatisera cette mise à jour, lors d&#8217;une nouvelle insertion et seulement dans ce cas. (et non pas à chaque mise à jour)</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> updateCoordonnees<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$completeAdr</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;;</span>adresse1<span style="color: #339933;">.</span><span style="color: #0000ff;">' '</span><span style="color: #339933;">.</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #990000;">empty</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">adresse2</span><span style="color: #009900;">&#41;</span>?<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">adresse2</span><span style="color: #339933;">.</span><span style="color: #0000ff;">' '</span><span style="color: #339933;">.:</span><span style="color: #0000ff;">''</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">.</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">cp</span><span style="color: #339933;">.</span><span style="color: #0000ff;">' '</span><span style="color: #339933;">.</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">ville</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// Ajout adresse2 seulement si non vide</span>
    <span style="color: #000088;">$geoloc</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Geoloc<span style="color: #009900;">&#40;</span><span style="color: #000088;">$completeAdr</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'maCleGoogleMap'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$data</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$geoloc</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: #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;">latitude</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$data</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
      <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">longitude</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$data</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #009933; font-style: italic;">/**
 *  Méthode appelé avant l'insertion d'un nouvel élément. Elle est à surcharger au besoin.
 */</span>
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> preInsert<span style="color: #009900;">&#40;</span><span style="color: #000088;">$event</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;">updateCoordonnees</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>Et voilà, avec un fichier de fixtures.yml du genre:</p>

<div class="wp_syntax"><div class="code"><pre class="yaml" style="font-family:monospace;">adresses:
  adresse_1:
    adresse1: 165 avenue du prado
    cp: 13008
    ville: Marseille
  adresse_2:
    adresse1: 7 rue de verdun
    cp: 13005
    ville: Marseille
  adresse_3:
    adresse1: 57 Bd Romain Rolland 
    cp: 13010
    ville: Marseille
  adresse_4:
    adresse1: Route de Gemenos 
    cp: 13400
    ville: Aubagne</pre></div></div>

<p>En le chargeant via symfony, on obtient en base:</p>
<pre>
select * from adresses ;
+-----+----------------------+----------+-------+-----------+------------+-----------+
| aid | adresse1             | address2 | cp    | ville     | latitude   | longitude |
+-----+----------------------+----------+-------+-----------+------------+-----------+
|   1 | 165 avenue du prado  | NULL   | 13008 | Marseille | 43.2783515 | 5.3883703 |
|   2 | 7 rue de verdun      | NULL     | 13005 | Marseille |  43.296542 | 5.3958405 |
|   3 | 57 Bd Romain Rolland | NULL     | 13010 | Marseille | 43.2774579 | 5.4202946 |
|   4 | Route de Gemenos     | NULL     | 13400 | Aubagne  | 43.2975544 | 5.5888222 |
+-----+----------------------+----------+-------+-----------+------------+-----------+
</pre>
<p>Un petit bout de code dans une action, rapidement pour tester:</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> executeTest<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;">$adr1</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;">'adresses'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">find</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$adr2</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;">'adresses'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">find</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">2</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">echo</span> <span style="color: #990000;">number_format</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$adr1</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getDistance</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$adr2</span><span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">2</span><span style="color: #339933;">,</span><span style="color: #0000ff;">','</span><span style="color: #339933;">,</span><span style="color: #0000ff;">''</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">'Km'</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// Par défaut, retourne en miles, il faut mettre le 2e paramètre à true pour avoir le résultat en km</span>
&nbsp;
    <span style="color: #b1b100;">return</span> sfView<span style="color: #339933;">::</span><span style="color: #004000;">NONE</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Et on obtient la distance en km entre nos 2 premières adresses. (Ici la distance entre chez moi et mon boulot :p)<br />
Comme l&#8217;indique la doc, on peut aller plus loin, en récupérant pour une adresse donnée, les N adresses les plus proches, ce qui peut donner lieu à des fonctionnalités très intéressantes:</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> executeTest2<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;">$adr1</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;">'adresses'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">find</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$q</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$adr1</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getDistanceQuery</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$result</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$q</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">orderby</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'miles asc'</span><span style="color: #009900;">&#41;</span> <span style="color: #666666; font-style: italic;">// On les trie par distance</span>
              <span style="color: #339933;">-&gt;</span><span style="color: #004000;">addWhere</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$q</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getRootAlias</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">.</span> <span style="color: #0000ff;">'.aid != ?'</span><span style="color: #339933;">,</span><span style="color: #000088;">$adr1</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">aid</span><span style="color: #009900;">&#41;</span> <span style="color: #666666; font-style: italic;">// On exclut l'adresse de référence</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>
&nbsp;
    <span style="color: #b1b100;">foreach</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$result</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$res</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
       <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$res</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">cp</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot; - &quot;</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$res</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">kilometers</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;&lt;br/&gt;&quot;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #b1b100;">return</span> sfView<span style="color: #339933;">::</span><span style="color: #004000;">NONE</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span></pre></div></div>

<p>A noter que le code du behavior est très simple en fait, il ajoute juste une formule mathématique pour ces calculs de distance. Mais c&#8217;est ce genre de petites fonctionnalités qui me font apprécier la découverte de Doctrine!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.amicalement-web.net/doctrine-et-son-behavior-geographical/2009/08/05/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
	</channel>
</rss>
<!-- WP Super Cache is installed but broken. The path to wp-cache-phase1.php in wp-content/advanced-cache.php must be fixed! -->
