<?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; mysql</title>
	<atom:link href="http://www.amicalement-web.net/tag/mysql/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>Tips : Configuration de serveurs #2</title>
		<link>http://www.amicalement-web.net/tips-configuration-de-serveurs-2/2010/12/21/</link>
		<comments>http://www.amicalement-web.net/tips-configuration-de-serveurs-2/2010/12/21/#comments</comments>
		<pubDate>Tue, 21 Dec 2010 09:39:35 +0000</pubDate>
		<dc:creator>Benoit</dc:creator>
				<category><![CDATA[Astuces]]></category>
		<category><![CDATA[astuces]]></category>
		<category><![CDATA[Linux]]></category>
		<category><![CDATA[mysql]]></category>
		<category><![CDATA[tips]]></category>

		<guid isPermaLink="false">http://www.amicalement-web.net/?p=1581</guid>
		<description><![CDATA[Suite à quelques demandes, notamment de @Ouark lors d&#8217;une soirée sur Paris, je vais essayer de publier régulièrement quelques petites astuces que j&#8217;ai trouvé et qui m&#8217;ont bien aidé/dépanné. Voici donc la partie #2 des Tips concernant la configuration de serveurs et le développement. Reset du pass root MySQL Qui n&#8217;a jamais perdu son mot [...]]]></description>
			<content:encoded><![CDATA[<p><a  href="http://www.amicalement-web.net/wp-content/uploads/tips_server_2.png"><img src="http://www.amicalement-web.net/wp-content/uploads/tips_server_2.png" alt="" title="tips_server_2" width="614" height="100" class="aligncenter size-full wp-image-1597" /></a></p>
<p>Suite à quelques demandes, notamment de <a  href="http://twitter.com/#!/Ouark">@Ouark</a> lors d&#8217;une soirée sur Paris, je vais essayer de publier régulièrement quelques petites astuces que j&#8217;ai trouvé et qui m&#8217;ont bien aidé/dépanné.</p>
<p>Voici donc la partie #2 des Tips concernant la configuration de serveurs et le développement.</p>
<p><span id="more-1581"></span></p>
<h3>Reset du pass root MySQL</h3>
<p>Qui n&#8217;a jamais perdu son mot de passe root MySQL, ce mot de passe que l&#8217;on vous demande à l&#8217;installation et dont vous allez vous servir ponctuellement, préférant par sécurité utiliser des utilisateurs plus précis et associés à une BDD spécifique. Je ne suis surement pas le seul en tout cas ^^</p>
<p>Ne stockant jamais mes mots de passe (question de sécurité), le mot de passe root MySQL est celui que j&#8217;ai le plus souvent oublié, même si ca ne m&#8217;arrive pas toutes les semaine non plus. Voici donc une petite astuce pour le reset et en créer un nouveau sans devoir tout réinstaller.</p>
<h4>1 &#8211; Arrêter votre serveur MySQL</h4>
<p>La première chose à faire bien entendu, est d&#8217;arrêter MySQL, le tout en étant root sur la machine bien entendu (comme pour toutes les commandes qui suivront)</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">/</span>etc<span style="color: #000000; font-weight: bold;">/</span>init.d<span style="color: #000000; font-weight: bold;">/</span>mysql stop</pre></div></div>

<h4>2 -MySQL en safe mode</h4>
<p>Il faut maintenant lancer MySQL en safe mode avec l&#8217;option <strong>&laquo;&nbsp;&#8211;skip-grant-tables&nbsp;&raquo;</strong> pour ne pas prendre en compte la table des privilèges.</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">mysqld_safe <span style="color: #660033;">--skip-grant-tables</span> <span style="color: #000000; font-weight: bold;">&amp;</span></pre></div></div>

<p>A noter aussi le<strong> &laquo;&nbsp;&&nbsp;&raquo;</strong> qui permet de lancer en background le serveur et ainsi continuer à modifier le pass root.</p>
<h4>3 &#8211; Login</h4>
<p>Maintenant on peut se loguer au serveur MySQL en root de manière &laquo;&nbsp;anonyme&nbsp;&raquo; ou plutôt sans avoir le soucis des privilèges</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">mysql <span style="color: #660033;">--user</span>=root mysql</pre></div></div>

<h4>4 &#8211; Modifier le pass root</h4>
<p>Une fois connecté, il suffit d&#8217;effectuer une requête pour mettre à jour le pass de la table user.</p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">UPDATE</span> <span style="color: #993333; font-weight: bold;">USER</span> <span style="color: #993333; font-weight: bold;">SET</span> Password<span style="color: #66cc66;">=</span>PASSWORD<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'mon_nouveau_pass'</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">WHERE</span> <span style="color: #993333; font-weight: bold;">USER</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">'root'</span>;
<span style="color: #993333; font-weight: bold;">FLUSH</span> PRIVILEGES;</pre></div></div>

<h4>5 &#8211; C&#8217;est repartit !</h4>
<p>Maintenant, on arrête le safe mode puis on relance le serveur MySQL</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">/</span>etc<span style="color: #000000; font-weight: bold;">/</span>init.d<span style="color: #000000; font-weight: bold;">/</span>mysql restart</pre></div></div>

<p>Vous pouvez à nouveau vous connecter en root et gérer vos bases.</p>
<p><i>N.B: Cette manipulation à été testé sur Ubuntu, Debian et Slackware, et, même si le principe reste le même quelque soit la distribution, il peut varier un petit peu ;)</i></p>
<p></p>
<h3>Nombre de visiteurs à partir des logs apache</h3>
<p>Régulièrement, il m&#8217;arrive de vouloir connaitre le nombre de visiteurs unique qui sont passé la veille sur les sites hébergés sur mon serveur, le tout sans devoir consulter des outils de stats que je n&#8217;ai pas forcément à disposition (pas installé, pas autorisé &#8230;).</p>
<p>Voici donc un petit script qui va calculer tout ca directement à partir des<strong> logs Apache</strong>.</p>
<p>Pour infos, la plupart de mes logs Apache sont sous le format suivant (il est important de connaître son <strong>LogFormat</strong> pour bien ajuster le script) :</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #ff0000;">&quot;%V %h %{PHPSESSID}C %l %u %t <span style="color: #000099; font-weight: bold;">\&quot;</span>%r<span style="color: #000099; font-weight: bold;">\&quot;</span> %&gt;s %b <span style="color: #000099; font-weight: bold;">\&quot;</span>%{Referer}i<span style="color: #000099; font-weight: bold;">\&quot;</span> <span style="color: #000099; font-weight: bold;">\&quot;</span>%{User-Agent}i<span style="color: #000099; font-weight: bold;">\&quot;</span>&quot;</span></pre></div></div>

<p>Et le petit script qui va calculer le nombre de visiteurs uniques pour le <strong>LogFormat</strong> ci-dessus et en prenant les fichiers de logs placé dans <strong>&laquo;&nbsp;/var/www/&nbsp;&raquo;</strong> et <strong>nommé avec le nom du domaine</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: #000000; font-weight: bold;">for</span> k <span style="color: #000000; font-weight: bold;">in</span> <span style="color: #000000; font-weight: bold;">`</span><span style="color: #c20cb9; font-weight: bold;">ls</span> <span style="color: #000000; font-weight: bold;">/</span>var<span style="color: #000000; font-weight: bold;">/</span>www<span style="color: #000000; font-weight: bold;">/*</span>.log.1<span style="color: #000000; font-weight: bold;">`</span>; <span style="color: #000000; font-weight: bold;">do</span>
     <span style="color: #007800;">domaine</span>=<span style="color: #000000; font-weight: bold;">`</span><span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #007800;">$k</span> <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">cut</span> <span style="color: #660033;">-f4</span> <span style="color: #660033;">-d</span><span style="color: #ff0000;">'/'</span> <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">sed</span> <span style="color: #ff0000;">'s/.log.1//g'</span><span style="color: #000000; font-weight: bold;">`</span>
     <span style="color: #007800;">nbr</span>=<span style="color: #000000; font-weight: bold;">`</span><span style="color: #c20cb9; font-weight: bold;">cat</span> <span style="color: #007800;">$k</span> <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">cut</span> <span style="color: #660033;">-f2</span> <span style="color: #660033;">-d</span><span style="color: #ff0000;">' '</span> <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">sort</span> <span style="color: #660033;">-n</span> <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">uniq</span> <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">wc</span> -l<span style="color: #000000; font-weight: bold;">`</span>
     <span style="color: #007800;">len</span>=<span style="color: #000000; font-weight: bold;">`</span><span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #007800;">$domaine</span> <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">wc</span> -c<span style="color: #000000; font-weight: bold;">`</span>
     <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #007800;">$len</span> <span style="color: #660033;">-gt</span> <span style="color: #000000;">16</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span>; <span style="color: #000000; font-weight: bold;">then</span>
         <span style="color: #007800;">tab</span>=<span style="color: #ff0000;">&quot;<span style="color: #000099; font-weight: bold;">\t</span><span style="color: #000099; font-weight: bold;">\t</span>&quot;</span>
     <span style="color: #000000; font-weight: bold;">else</span>
         <span style="color: #007800;">tab</span>=<span style="color: #ff0000;">&quot;<span style="color: #000099; font-weight: bold;">\t</span><span style="color: #000099; font-weight: bold;">\t</span><span style="color: #000099; font-weight: bold;">\t</span>&quot;</span>
     <span style="color: #000000; font-weight: bold;">fi</span>
     <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;<span style="color: #007800;">$domaine</span><span style="color: #007800;">$tab</span><span style="color: #007800;">$nbr</span>&quot;</span>
<span style="color: #000000; font-weight: bold;">done</span></pre></div></div>

<p>Ce qui donne :</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">domaine.net			<span style="color: #000000;">347</span>
domaine.com			<span style="color: #000000;">283</span>
blog.domaine.net		<span style="color: #000000;">1301</span>
www.domaine.fr			<span style="color: #000000;">705</span></pre></div></div>

<p></p>
<h3>LogFormat personnalisé &#038; variable PHP</h3>
<p>Si vous aimez analyser vos logs Apache pour faire des statistiques personnelles, il est toujours intéressant de pouvoir customiser le format de ses logs. Personnellement, je spécifie des format de logs dans <strong>&laquo;&nbsp;/etc/apache/conf.d/logformat&nbsp;&raquo;</strong> avec la syntaxe suivante :</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">LogFormat <span style="color: #ff0000;">&quot;%V %h %{forum_username}C %{PHPSESSID}C %l %u %t <span style="color: #000099; font-weight: bold;">\&quot;</span>%r<span style="color: #000099; font-weight: bold;">\&quot;</span> %&gt;s %b <span style="color: #000099; font-weight: bold;">\&quot;</span>%{Referer}i<span style="color: #000099; font-weight: bold;">\&quot;</span> <span style="color: #000099; font-weight: bold;">\&quot;</span>%{User-Agent}i<span style="color: #000099; font-weight: bold;">\&quot;</span>&quot;</span> forum
LogFormat <span style="color: #ff0000;">&quot;%V %h %{user_id}n %{PHPSESSID}C %l %u %t <span style="color: #000099; font-weight: bold;">\&quot;</span>%r<span style="color: #000099; font-weight: bold;">\&quot;</span> %&gt;s %b <span style="color: #000099; font-weight: bold;">\&quot;</span>%{Referer}i<span style="color: #000099; font-weight: bold;">\&quot;</span> <span style="color: #000099; font-weight: bold;">\&quot;</span>%{User-Agent}i<span style="color: #000099; font-weight: bold;">\&quot;</span>&quot;</span> site</pre></div></div>

<p>Dans le premier cas, je précise 2 variables venant des cookies, on le remarque avec le <strong>&laquo;&nbsp;C&nbsp;&raquo;</strong> placé après la variable.</p>
<p>Dans le second exemple, j&#8217;utilise une variable provenant d&#8217;un cookie mais aussi de mon environnement avec <strong>&laquo;&nbsp;%{user_id}n&nbsp;&raquo;</strong>. Le petit <strong>&laquo;&nbsp;n&nbsp;&raquo;</strong> placé à la fin signifie que la valeur provient de l&#8217;utilisation d&#8217;un <a  href="http://php.net/manual/fr/function.apache-note.php">apache_note()</a></p>
<p>Dans mon code, je définis simplement :</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #990000;">apache_note</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'user_id'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$monUserId</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>J&#8217;utilise beaucoup cette technique afin d&#8217;obtenir des stats très précise sur mes visiteurs mais aussi d&#8217;effectuer des analyses afin d&#8217;enrechir un CRM par exemple.</p>
<p>Il suffit par la suite dans son fichier de configuration Apache de placer la directive suivante :</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">CustomLog <span style="color: #000000; font-weight: bold;">/</span>var<span style="color: #000000; font-weight: bold;">/</span>www<span style="color: #000000; font-weight: bold;">/</span>forum.domaine.net.log forum</pre></div></div>

<p></p>
<p>Voilà, en espérant que ces petites astuces vous servent par la suite ;)</p>
]]></content:encoded>
			<wfw:commentRss>http://www.amicalement-web.net/tips-configuration-de-serveurs-2/2010/12/21/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Développement Web: les bons plans de la semaine #21</title>
		<link>http://www.amicalement-web.net/developpement-web-les-bons-plans-de-la-semaine-21/2009/11/02/</link>
		<comments>http://www.amicalement-web.net/developpement-web-les-bons-plans-de-la-semaine-21/2009/11/02/#comments</comments>
		<pubDate>Mon, 02 Nov 2009 20:26:19 +0000</pubDate>
		<dc:creator>Tim</dc:creator>
				<category><![CDATA[Bons plans]]></category>
		<category><![CDATA[CSS]]></category>
		<category><![CDATA[icone]]></category>
		<category><![CDATA[ie]]></category>
		<category><![CDATA[IE6]]></category>
		<category><![CDATA[ie8]]></category>
		<category><![CDATA[jquery]]></category>
		<category><![CDATA[mysql]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[service]]></category>

		<guid isPermaLink="false">http://www.amicalement-web.net/?p=912</guid>
		<description><![CDATA[Petit week end bien rempli et forcément petit report des bons plans de la semaine. Mais ce n&#8217;était qu&#8217;une question d&#8217;heure! Les voici les bons plans tant attendus avec un petit bonus à la fin Des icones pour google map (via pborreli) Oui parce que les icones par défaut, elles sont biens mais pas tops! [...]]]></description>
			<content:encoded><![CDATA[<p><img src="http://www.amicalement-web.net/wp-content/uploads/read-or-listen.png" alt="read-or-listen" title="read-or-listen" width="614" height="100" class="alignnone size-full wp-image-920" /> Petit week end bien rempli et forcément petit report des bons plans de la semaine. Mais ce n&#8217;était qu&#8217;une question d&#8217;heure! Les voici les bons plans tant attendus avec un petit bonus à la fin<br />
<span id="more-912"></span></p>
<ul>
<li><a  href="http://code.google.com/p/google-maps-icons/">Des icones pour google map</a> (via <a  href="http://twitter.com/pborreli">pborreli</a>)<br />
Oui parce que les icones par défaut, elles sont biens mais pas tops!</li>
<li><a  href="http://lloogg.com/">Un service de suivi de vos visiteurs en temps réel</a> (via <a  href="http://twitter.com/old_sound">old_sound</a>)<br />
Oui bon en gros, ca reste un tail -f access.log mais avec le décodeur en plus!</li>
<li><a  href="http://icones.pro/">Des jolies icones</a><br />
Un nouveau moteur de recherche avec quasi que des icones de qualité pour vos projets.</li>
<li><a  href="http://www.tripwiremagazine.com/tutorials/tutorials/jquery-and-general-javascript-tips-to-improve-your-code.html">Encore des astuces et optimisations pour jquery</a><br />
Et oui plus on l&#8217;utilise, plus on essaye de l&#8217;optimiser ce petit jquery.</li>
<li><a  href="http://www.thecssninja.com/css/real-text-rotation-with-css">Tourner vos textes en CSS&#8230; sous IE8</a><br />
Et oui voilà l&#8217;exception qui vient confirmer la règle, voici la propriété css3 que IE8 est le seul à interpréter! Ca devait être de ça dont parlait Microsoft quand ils nous ont vanté toute l&#8217;innovation de leur navigateur!</li>
<li><a  href="http://demo.tutorialzine.com/2009/10/google-wave-history-slider-jquery/demo.php">Reproduire le slider de l&#8217;historique google wave</a><br />
A peine sorti et voilà qu&#8217;on essaye déjà d&#8217;exporter en jquery ces bonnes idées. Ici le slider pour filtrer l&#8217;historique d&#8217;une liste de commentaire.</li>
<li><a  href="http://www.thegarvin.com/2009/10/28/clearer-code-with-jquery-micro-plugins.html">Codez en micro plugin jquery</a><br />
Une idée très intéressante qui consiste à implémenter vos lignes jquery de votre document.ready en micro plugin pour gagner en lisibilité et en maintenabilité</li>
<li><a  href="http://www.onderhond.com/tools/ie6fixer">IE6 fixer</a><br />
Voilà de quoi faciliter un peu votre debug IE6, grâce à cette génération de CSS qui permet de corriger les bugs les plus connus de ce dinosaure</li>
<li><a  href="http://www.phpclasses.org/browse/package/1136.html">Une classe PHP pour interfacer les thread PHP</a><br />
Tout est dans le titre ;)</li>
<li><a  href="http://till.klampaeckel.de/blog/archives/74-PHP-So-youd-like-to-migrate-from-MySQL-to-CouchDB-Part-I.html">Migrer de Mysql à COuchDB en PHP</a><br />
<a  href="http://twitter.com/jlecour">Jérémy</a> en parle beaucoup en ce moment. Ca méritait d&#8217;y jeter un oeil. Voici un tutorial pour appréhender la chose et comprendre ce qu&#8217;est CouchDB au passage.</li>
</ul>
<p>Et le bonus de la semaine chez l&#8217;ami Methylbro :<br />
<a  href="http://methylbro.titaxium.org/post/2009/11/01/concours-gagnez-le-livre-php5-avance">Gagner le livre php5 avance</a></p>
<p><small>Crédit photo: http://www.flickr.com/photos/chitrasudar/2721323275/</small></p>
]]></content:encoded>
			<wfw:commentRss>http://www.amicalement-web.net/developpement-web-les-bons-plans-de-la-semaine-21/2009/11/02/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Symfony: Relation many-to-many sur la meme table avec Propel</title>
		<link>http://www.amicalement-web.net/symfony-relation-many-to-many-sur-la-meme-table-avec-propel/2009/06/24/</link>
		<comments>http://www.amicalement-web.net/symfony-relation-many-to-many-sur-la-meme-table-avec-propel/2009/06/24/#comments</comments>
		<pubDate>Wed, 24 Jun 2009 10:36:51 +0000</pubDate>
		<dc:creator>Tim</dc:creator>
				<category><![CDATA[Astuces]]></category>
		<category><![CDATA[form]]></category>
		<category><![CDATA[mysql]]></category>
		<category><![CDATA[propel]]></category>
		<category><![CDATA[Symfony]]></category>

		<guid isPermaLink="false">http://www.amicalement-web.net/?p=375</guid>
		<description><![CDATA[Beaucoup de boulot pour cette courte semaine (je suis en week end, ce soir :D) du coup, je n&#8217;ai pas eu le temps de faire le billet Symfony du mardi! Ca tombait bien en même temps, car je n&#8217;avais pas énormément d&#8217;idées. Finalement, j&#8217;ai trouvé un petit quelque chose pour subvenir à vos besoins de [...]]]></description>
			<content:encoded><![CDATA[<p><img src="http://www.amicalement-web.net/wp-content/uploads/system-search.png" alt="system-search" title="system-search" width="48" height="48" class="alignleft size-full wp-image-39" /> Beaucoup de boulot pour cette courte semaine (je suis en week end, ce soir :D) du coup, je n&#8217;ai pas eu le temps de faire le billet Symfony du mardi! Ca tombait bien en même temps, car je n&#8217;avais pas énormément d&#8217;idées.<br />
Finalement, j&#8217;ai trouvé un petit quelque chose pour subvenir à vos besoins de nouveautés permanents. On va parler d&#8217;une petite faiblesse dans Propel, que je n&#8217;ai pas encore pris le temps d&#8217;analyser dans toute sa profondeur mais qui reste assez gênant: La gestion des relations many-to-many sur une même table.<br />
<span id="more-375"></span><br />
En gros, une table d&#8217;association dont le couple serait deux clé étrangères sur la même table du genre:</p>

<div class="wp_syntax"><div class="code"><pre class="yaml" style="font-family:monospace;">  cross_selling:
    _attributes: { phpName: CrossSelling }
    pid: { type: SMALLINT, size: '5', primaryKey: true, required: true, foreignTable: produits, foreignReference: pid, onDelete: CASCADE, onUpdate: CASCADE }
    csid: { type: SMALLINT, size: '5', primaryKey: true, required: true, foreignTable: produits, foreignReference: pid, onDelete: CASCADE, onUpdate: CASCADE }</pre></div></div>

<p>Autant dans la partie model, aucun souci, on est capable de récupérer chacun des éléments par la table &laquo;&nbsp;Produits&nbsp;&raquo; (la table où pointe les 2 clés étrangères pour ceux qui se sont perdus en route), autant dans la partie form, aucune trace du widget pour matérialiser cette relation, rien, nada. Alors qu&#8217;au départ, si on définie notre relation, c&#8217;est qu&#8217;on en a un peu besoin en fait.</p>
<p>On va donc tâcher de combler ce vide, en rajoutant ce dont a besoin notre formulaire pour reproduire notre relation dans le fichier <code>lib/form/ProduitsForm.class.php</code></p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">class</span> ProduitsForm <span style="color: #000000; font-weight: bold;">extends</span> BaseProduitsForm
<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>
    parent<span style="color: #339933;">::</span><span style="color: #004000;">configure</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;">// Ajout Cross Selling</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;">'cross_selling_list'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> sfWidgetFormPropelChoiceMany<span style="color: #009900;">&#40;</span><span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
    	<span style="color: #0000ff;">'model'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'Produits'</span>
    <span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">validatorSchema</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'cross_selling_list'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> sfValidatorPropelChoiceMany<span style="color: #009900;">&#40;</span><span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'model'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'Produits'</span><span style="color: #339933;">,</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: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span></pre></div></div>

<p>On crée ensuite la méthode qui va permettre d&#8217;enregistrer le résultat de notre choix dans notre widget de liste (en fait, il suffit d&#8217;adapter le code généré pour une relation many-to-many qui fonctionne):</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> saveCrossSellingList<span style="color: #009900;">&#40;</span><span style="color: #000088;">$con</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span>
  <span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">isValid</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
      <span style="color: #b1b100;">throw</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getErrorSchema</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #990000;">isset</span><span style="color: #009900;">&#40;</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;">'cross_selling_list'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
      <span style="color: #666666; font-style: italic;">// somebody has unset this widget</span>
      <span style="color: #b1b100;">return</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">is_null</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$con</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
      <span style="color: #000088;">$con</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getConnection</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000088;">$c</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Criteria<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$c</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">add</span><span style="color: #009900;">&#40;</span>CrossSellingPeer<span style="color: #339933;">::</span><span style="color: #004000;">PID</span><span style="color: #339933;">,</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">object</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getPrimaryKey</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    CrossSellingPeer<span style="color: #339933;">::</span><span style="color: #004000;">doDelete</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$c</span><span style="color: #339933;">,</span> <span style="color: #000088;">$con</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000088;">$values</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getValue</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'cross_selling_list'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">is_array</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$values</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
      <span style="color: #b1b100;">foreach</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$values</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$value</span><span style="color: #009900;">&#41;</span>
      <span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$obj</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> CrossSelling<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$obj</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setPid</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">object</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getPrimaryKey</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$obj</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setCsid</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$value</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$obj</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">save</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span>
  <span style="color: #009900;">&#125;</span></pre></div></div>

<p>Puis on surcharge le doSave pour ajouter cette fonction:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">protected</span> <span style="color: #000000; font-weight: bold;">function</span> doSave<span style="color: #009900;">&#40;</span><span style="color: #000088;">$con</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span>
  <span style="color: #009900;">&#123;</span>
    parent<span style="color: #339933;">::</span><span style="color: #004000;">doSave</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$con</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;">saveCrossSellingList</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$con</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span></pre></div></div>

<p>et pour finir on surcharge la méthode  updateDefaultsFromObject pour charger le résultat une fois sauvegardé:</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> updateDefaultsFromObject<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
  <span style="color: #009900;">&#123;</span>
    parent<span style="color: #339933;">::</span><span style="color: #004000;">updateDefaultsFromObject</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">isset</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">widgetSchema</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'cross_selling_list'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
      <span style="color: #000088;">$values</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #b1b100;">foreach</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">object</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getCrossSellingsRelatedByPid</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$obj</span><span style="color: #009900;">&#41;</span>
      <span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$values</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$obj</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getCsid</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #009900;">&#125;</span>
&nbsp;
      <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setDefault</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'cross_selling_list'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$values</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>Et voilà, Propel n&#8217;a qu&#8217;a bien se tenir, on peut se passer de lui! Je pense qu&#8217;avec un peu plus de temps, je me plongerai bien dans le détail pour voir où ça coince, à moins que j&#8217;ai loupé encore une fois un épisode.</p>
<p>Moralité, Doctrine vaincra?</p>
]]></content:encoded>
			<wfw:commentRss>http://www.amicalement-web.net/symfony-relation-many-to-many-sur-la-meme-table-avec-propel/2009/06/24/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Mysql, Propel et l&#8217;UTF8 sont dans un bateau</title>
		<link>http://www.amicalement-web.net/mysql-propel-utf8/2009/05/21/</link>
		<comments>http://www.amicalement-web.net/mysql-propel-utf8/2009/05/21/#comments</comments>
		<pubDate>Thu, 21 May 2009 13:35:37 +0000</pubDate>
		<dc:creator>Tim</dc:creator>
				<category><![CDATA[Astuces]]></category>
		<category><![CDATA[mysql]]></category>
		<category><![CDATA[propel]]></category>
		<category><![CDATA[Symfony]]></category>
		<category><![CDATA[utf8]]></category>

		<guid isPermaLink="false">http://www.amicalement-web.net/?p=254</guid>
		<description><![CDATA[Un gros souci que j&#8217;ai toujours eu lors de mes projets symfony c&#8217;est la capacité de dire à Propel que je veux ma base en UTF8, j&#8217;ai beau mettre UTF8 un peu partout, database.yml, schema.yml, propel.ini rien n&#8217;y fait, mon build-all créé mes tables en ISO. D&#8217;ailleurs un peu déçu que Jobeet chapitre 3 n&#8217;aborde [...]]]></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" /> Un gros souci que j&#8217;ai toujours eu lors de mes projets symfony c&#8217;est la capacité de dire à Propel que je veux ma base en UTF8, j&#8217;ai beau mettre UTF8 un peu partout, database.yml, schema.yml, propel.ini rien n&#8217;y fait, mon build-all créé mes tables en ISO. D&#8217;ailleurs un peu déçu que <a  href="http://www.symfony-project.org/jobeet/1_2/Propel/en/03" class="extern">Jobeet chapitre 3</a> n&#8217;aborde pas le sujet.</p>
<p>J&#8217;ai donc tenté de voir du côté de Mysql, pour mettre ce dernier par défaut en utf8, mais là aussi, on a beau remplir le my.cnf de utf8 dans toutes les options, rien de concret ne se produit.</p>
<p>J&#8217;en été donc resté à devoir rajouter à la main, dans mon .sql généré par symfony, le fameux &laquo;&nbsp;DEFAUT CHARSET=UTF8&#8243;. Mais devoir le rajouter à chaque modification de mon schema, cela peut devenir pénible dans des projets de tests.</p>
<p>Et puis par hasard au détour d&#8217;une conversation sur mysql dans une mailing-list (mea culpa j&#8217;ai pas noté l&#8217;adresse) je suis enfin tombé sur quelque chose qui fonctionne! A la création de la table, écrire:</p>

<div class="wp_syntax"><div class="code"><pre class="mysql" style="font-family:monospace;"><span style="color: #990099; font-weight: bold;">CREATE</span> <span style="color: #990099; font-weight: bold;">DATABASE</span> mydb CHARACTER <span style="color: #990099; font-weight: bold;">SET</span> utf8 <span style="color: #CC0099; font-weight: bold;">COLLATE</span> utf8_general_ci</pre></div></div>

<p>Voilà, toutes les tables créés maintenant dans cette base seront en UTF8 par défaut.</p>
<p>Il y a sans doute mieux, mais pour l&#8217;instant je me contente de ça. Mais peut-être que quelqu&#8217;un a une meilleur solution? Comment gérez-vous vos tables en UTF8?</p>
]]></content:encoded>
			<wfw:commentRss>http://www.amicalement-web.net/mysql-propel-utf8/2009/05/21/feed/</wfw:commentRss>
		<slash:comments>7</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! -->
