<?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>Symfony-IT &#187; mysql</title>
	<atom:link href="http://www.symfony.it/categoria/mysql/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.symfony.it</link>
	<description>Just another Symfony Framework weblog</description>
	<lastBuildDate>Mon, 30 Jan 2012 13:53:22 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
	<div id='fb-root'></div>
					<script type='text/javascript'>
						window.fbAsyncInit = function()
						{
							FB.init({appId: null, status: true, cookie: true, xfbml: true});
						};
						(function()
						{
							var e = document.createElement('script'); e.async = true;
							e.src = document.location.protocol + '//connect.facebook.net/it_IT/all.js';
							document.getElementById('fb-root').appendChild(e);
						}());
					</script>	
						<item>
		<title>Confrontare due campi della stessa tabella con Propel</title>
		<link>http://www.symfony.it/articoli/8/confrontare-due-campi-della-stessa-tabella-con-propel/</link>
		<comments>http://www.symfony.it/articoli/8/confrontare-due-campi-della-stessa-tabella-con-propel/#comments</comments>
		<pubDate>Thu, 19 Jul 2007 13:57:21 +0000</pubDate>
		<dc:creator>kea</dc:creator>
				<category><![CDATA[hacks]]></category>
		<category><![CDATA[mysql]]></category>
		<category><![CDATA[propel]]></category>
		<category><![CDATA[symfony]]></category>
		<category><![CDATA[tips]]></category>

		<guid isPermaLink="false">http://www.symfony.it/articoli/8/confrontare-due-campi-della-stessa-tabella-con-propel/</guid>
		<description><![CDATA[Con il criteria di Propel non è possibile ancora confrontare due campi della stessa tabella, ma solo il campo con un valore. Questa feature sarà probabilmente implementata nella versione 2.0. Nel frattempo vi propongo questo trick per confrontare tra loro i campi Campo1 e Campo2 della tabella Tabella 1 2 3 $c = new Criteria&#40;&#41;; [...]]]></description>
			<content:encoded><![CDATA[<p>Con il criteria di Propel non è possibile ancora confrontare due campi della stessa tabella, ma solo il campo con un valore.  Questa feature sarà probabilmente implementata nella versione 2.0.</p>
<p>Nel frattempo vi propongo questo trick per confrontare tra loro i campi Campo1 e Campo2 della tabella Tabella</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><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>
&nbsp;
<span style="color: #000088;">$c</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">add</span><span style="color: #009900;">&#40;</span>Tabella<span style="color: #339933;">.</span>Campo1<span style="color: #339933;">,</span> Tabella<span style="color: #339933;">.</span>Campo1<span style="color: #339933;">.</span><span style="color: #0000ff;">' = '</span><span style="color: #339933;">.</span>Tabella<span style="color: #339933;">.</span>Campo2<span style="color: #339933;">,</span> Criteria<span style="color: #339933;">:</span>CUSTOM<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>Naturalmente al posto di &#8220;.&#8217; = &#8216; .&#8221; potere inserire l&#8217;operatore che vi serve (=, &lt;&gt;, !=, &lt;, &gt;, etc.)</p>
<div class='wpfblike' style='height: 40px;'><fb:like href='http://www.symfony.it/articoli/8/confrontare-due-campi-della-stessa-tabella-con-propel/' layout='default' show_faces='true' width='400' action='like' colorscheme='light' send='false' /></div>]]></content:encoded>
			<wfw:commentRss>http://www.symfony.it/articoli/8/confrontare-due-campi-della-stessa-tabella-con-propel/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Importare un file CSV all&#8217;interno del database</title>
		<link>http://www.symfony.it/articoli/6/importare-un-file-csv-allinterno-del-database/</link>
		<comments>http://www.symfony.it/articoli/6/importare-un-file-csv-allinterno-del-database/#comments</comments>
		<pubDate>Wed, 04 Jul 2007 20:46:45 +0000</pubDate>
		<dc:creator>fullo</dc:creator>
				<category><![CDATA[mysql]]></category>
		<category><![CDATA[tips]]></category>

		<guid isPermaLink="false">http://www.symfony.it/articoli/6/importare-un-file-csv-allinterno-del-database/</guid>
		<description><![CDATA[Spesso risulta necessario importare grosse moli di dati direttamente nel database passando da una form. Nel caso di grosse mole di dati, l&#8217;utilizzo di oggetti rischia di sovraccaricare il sistema pertanto l&#8217;utilizzo delle funzioni native del DBMS (nel caso di MySQL) spesso si traduce in una ottima soluzione. Supponiamo quindi di aver caricato tramite form [...]]]></description>
			<content:encoded><![CDATA[<p>Spesso risulta necessario importare grosse moli di dati direttamente nel database passando da una form. </p>
<p>Nel caso di grosse mole di dati, l&#8217;utilizzo di oggetti rischia di sovraccaricare il sistema pertanto l&#8217;utilizzo delle funzioni native del DBMS (nel <a href="http://dev.mysql.com/doc/refman/5.0/en/load-data.html">caso di MySQL</a>) spesso si traduce in una ottima soluzione.</p>
<p><span id="more-6"></span></p>
<p>Supponiamo quindi di aver caricato tramite form un file CVS nel sistema ecco come dovrà essere il metodo da inserire nella <code>actions.class.php</code> per gestire l&#8217;importazione.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
</pre></td><td 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> executeImport<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
  <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getRequest</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">hasFiles</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;">$filename</span> <span style="color: #339933;">=</span> <span style="color: #990000;">date</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Ymd'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">'-'</span><span style="color: #339933;">.</span><span style="color: #000088;">$tipo</span><span style="color: #339933;">.</span><span style="color: #0000ff;">'-filename.csv'</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getRequest</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">moveFile</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'file_import'</span><span style="color: #339933;">,</span> sfConfig<span style="color: #339933;">::</span><span style="color: #004000;">get</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'sf_upload_dir'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">'/'</span><span style="color: #339933;">.</span><span style="color: #000088;">$filename</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">// elimino tutti i vecchi dati</span>
    TabellaPeer<span style="color: #339933;">::</span><span style="color: #004000;">doDeleteAll</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>	
&nbsp;
    <span style="color: #666666; font-style: italic;">// inizializzo la gestione del db			</span>
    <span style="color: #000088;">$databaseManager</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> sfDatabaseManager<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$databaseManager</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">initialize</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>		
    <span style="color: #000088;">$connection</span> <span style="color: #339933;">=</span> Propel<span style="color: #339933;">::</span><span style="color: #004000;">getConnection</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">// creo la query di import del file usando la sintassi di LOAD FILE di MySQL</span>
    <span style="color: #000088;">$query</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'LOAD DATA INFILE &quot;'</span><span style="color: #339933;">.</span>sfConfig<span style="color: #339933;">::</span><span style="color: #004000;">get</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'sf_upload_dir'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">'/'</span><span style="color: #339933;">.</span><span style="color: #000088;">$filename</span><span style="color: #339933;">.</span><span style="color: #0000ff;">'&quot; '</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$query</span><span style="color: #339933;">.=</span> <span style="color: #0000ff;">' INTO TABLE '</span><span style="color: #339933;">.</span>TabellaPeer<span style="color: #339933;">::</span><span style="color: #004000;">TABLE_NAME</span> <span style="color: #339933;">.</span><span style="color: #0000ff;">' FIELDS TERMINATED BY &quot;|&quot; ENCLOSED BY &quot;&quot; LINES TERMINATED BY &quot;\n&quot; '</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$query</span><span style="color: #339933;">.=</span> <span style="color: #0000ff;">'(campo1,campo2,campo3) SET campo4 = &quot;costante&quot;'</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000088;">$statement</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$connection</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">prepareStatement</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$query</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$resultset</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$statement</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">executeQuery</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>Analizziamo passo passo cosa facciamo nel metodo</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;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getRequest</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">hasFiles</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span></pre></div></div>

<p>Innanzitutto verifichiamo che il file sia stato caricato correttamente, in realtà potremmo inserire ulteriori controlli come ad esempio che i META-TYPE corrispondano a quelli di un file CSV, ma per ora soprassediamo.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$filename</span> <span style="color: #339933;">=</span> <span style="color: #990000;">date</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Ymd'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">'-'</span><span style="color: #339933;">.</span><span style="color: #000088;">$tipo</span><span style="color: #339933;">.</span><span style="color: #0000ff;">'-filename.csv'</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getRequest</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">moveFile</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'file_import'</span><span style="color: #339933;">,</span> sfConfig<span style="color: #339933;">::</span><span style="color: #004000;">get</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'sf_upload_dir'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">'/'</span><span style="color: #339933;">.</span><span style="color: #000088;">$filename</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Generiamo il nome del file e lo spostiamo nella directory di upload impostata nel file di configurazione della nostra applicazione</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">// elimino tutti i vecchi dati</span>
TabellaPeer<span style="color: #339933;">::</span><span style="color: #004000;">doDeleteAll</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Questo passaggio è opzionale, facendo così vengono cancellati tutti i vecchi dati relativi alla tabella su cui vogliamo lavorare, utile nel caso di aggiornamenti massicci (ad esempio da gestionali), meno utile se importiamo sempre nuovi dati.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$databaseManager</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> sfDatabaseManager<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$databaseManager</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">initialize</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>		
<span style="color: #000088;">$connection</span> <span style="color: #339933;">=</span> Propel<span style="color: #339933;">::</span><span style="color: #004000;">getConnection</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Inizializzamo la connessione al database usando Propel e prepariamo la query.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">// creo la query di import del file usando la sintassi di LOAD FILE di MySQL</span>
<span style="color: #000088;">$query</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'LOAD DATA INFILE &quot;'</span><span style="color: #339933;">.</span>sfConfig<span style="color: #339933;">::</span><span style="color: #004000;">get</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'sf_upload_dir'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">'/'</span><span style="color: #339933;">.</span><span style="color: #000088;">$filename</span><span style="color: #339933;">.</span><span style="color: #0000ff;">'&quot; '</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$query</span><span style="color: #339933;">.=</span> <span style="color: #0000ff;">' INTO TABLE '</span><span style="color: #339933;">.</span>TabellaPeer<span style="color: #339933;">::</span><span style="color: #004000;">TABLE_NAME</span> <span style="color: #339933;">.</span><span style="color: #0000ff;">' FIELDS TERMINATED BY &quot;|&quot; ENCLOSED BY &quot;&quot; LINES TERMINATED BY &quot;\n&quot; '</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$query</span><span style="color: #339933;">.=</span> <span style="color: #0000ff;">'(campo1,campo2,campo3) SET campo4 = &quot;costante&quot;'</span><span style="color: #339933;">;</span></pre></div></div>

<p>Qui generiamo la query che verrà eseguita su MySQL. Utilizzando il metodo <code>LOAD</code> carichiamo il file creato poco prima successivamente andiamo ad indicare il nome della tabella su cui vogliamo lavorare, il separatore tra un campo e l&#8217;altro (ad esempio | ), il carattere all&#8217;interno del quale il campo è racchiuso (alcuni file CSV ad esempio usano <code>'</code> o <code>"</code>), ed il carattere di fine linea (nel nostro caso l&#8217;andata a capo).</p>
<p>Infine nell&#8217;ultima riga della query andiamo ad indicare a cosa corrispondono i campi del CSV rispetto a quelli della tabella. E&#8217; interessante notare che è possibile anche <em>forzare</em> una variabile nel caso che quest&#8217;ultima non sia presente nel file.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$statement</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$connection</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">prepareStatement</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$query</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$resultset</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$statement</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">executeQuery</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Infine andiamo ad inserire la query nel metodo <code>prepareStatement()</code> che si occupa di tradurla per CREOLE e di eseguirla successivamente.</p>
<div class='wpfblike' style='height: 40px;'><fb:like href='http://www.symfony.it/articoli/6/importare-un-file-csv-allinterno-del-database/' layout='default' show_faces='true' width='400' action='like' colorscheme='light' send='false' /></div>]]></content:encoded>
			<wfw:commentRss>http://www.symfony.it/articoli/6/importare-un-file-csv-allinterno-del-database/feed/</wfw:commentRss>
		<slash:comments>2</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! -->
