<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Blog of a LAMP Developer based near Guildford, Surrey &#187; MYSQL</title>
	<atom:link href="http://www.lampdeveloper.co.uk/category/mysql/feed" rel="self" type="application/rss+xml" />
	<link>http://www.lampdeveloper.co.uk</link>
	<description>A day in the life of a Lamp Developer</description>
	<lastBuildDate>Wed, 18 Aug 2010 14:44:13 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Archiving Mysql Data Tables using CakePHP or just SQL</title>
		<link>http://www.lampdeveloper.co.uk/mysql/handy-way-to-copy-data-between-mysql-databases.html</link>
		<comments>http://www.lampdeveloper.co.uk/mysql/handy-way-to-copy-data-between-mysql-databases.html#comments</comments>
		<pubDate>Fri, 05 Jun 2009 12:20:24 +0000</pubDate>
		<dc:creator>Jamie</dc:creator>
				<category><![CDATA[Cakephp]]></category>
		<category><![CDATA[MYSQL]]></category>
		<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://www.lampdeveloper.co.uk/?p=126</guid>
		<description><![CDATA[I used to go down the road of doing a for each on my source table, which I always hated and thought was bad.
This way I can use table locking for MyISAM or transactions, for InnoDB, to make sure the data gets copied across.
I mainly use this method for archiving tables and keep the primary [...]]]></description>
			<content:encoded><![CDATA[<p>I used to go down the road of doing a for each on my source table, which I always hated and thought was bad.</p>
<p>This way I can use table locking for MyISAM or transactions, for InnoDB, to make sure the data gets copied across.</p>
<p>I mainly use this method for archiving tables and keep the primary keys the same.</p>
<p>I have 2 tables created called ads and adsarchives.</p>
<p>What you do is make a copy of your database structure called adsarchives,  and change the primary key so that it doesn&#8217;t auto increment. This mean that you can&#8217;t copy the same data twice. Also the second query won&#8217;t delete your data from the source on a fail.</p>
<p>( Oh by the way, my first example is using Cakephp on a MyISAM table) It would be much better to do transactions for this. But I wasn&#8217;t updating an InnoDB table.</p>
<pre name="code" class="php">

function archiveads($monthsold=false){

if(is_numeric($monthsold) &amp; $monthsold &gt; 1){

$range = date(&#039;Y-m-d H:i:s&#039;, strtotime(&quot;-{$monthsold} months&quot;));

$db =&amp; ConnectionManager::getDataSource($this-&gt;useDbConfig);

$this-&gt;query(&#039;LOCK TABLES ads READ, adsarchives WRITE&#039;);
$this-&gt;query(&quot;INSERT INTO adsarchives SELECT ads.* FROM ads WHERE ads.enddate &lt; &#039;{$range}&#039;&quot;);

$error = $db-&gt;lastError();

$this-&gt;query(&#039;UNLOCK TABLES&#039;);

if(!$error) { $this-&gt;query(&quot;DELETE FROM ads WHERE  ads.enddate &lt; &#039;{$range}&#039;&quot;) ; return true;}else{

$this-&gt;log(&#039;FATAL Error archiving the Ads |&#039;.$error);

}

}else{

return false;
}

}
</pre>
<p>If you want the raw SQl way of doing it try this. I&#8217;m archiving 3 month old data.</p>
<pre name="code" class="sql">

LOCK TABLES ads READ, adsarchives WRITE;
SET @age=(CURRENT_DATE()- INTERVAL 3 MONTH), @today=CURRENT_DATE();

INSERT INTO adsarchives
SELECT ads.*
FROM ads WHERE ads.enddate &lt; @age;

UNLOCK TABLES;
DELETE FROM ads WHERE  ads.enddate &lt; @age;
</pre>
<p class="addtoany_share_save_container">
    <a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save?sitename=Blog%20of%20a%20LAMP%20Developer%20based%20near%20Guildford%2C%20Surrey&amp;siteurl=http%3A%2F%2Fwww.lampdeveloper.co.uk%2F&amp;linkname=Archiving%20Mysql%20Data%20Tables%20using%20CakePHP%20or%20just%20SQL&amp;linkurl=http%3A%2F%2Fwww.lampdeveloper.co.uk%2Fmysql%2Fhandy-way-to-copy-data-between-mysql-databases.html"><img src="http://www.lampdeveloper.co.uk/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share/Save/Bookmark"/></a>

	</p>]]></content:encoded>
			<wfw:commentRss>http://www.lampdeveloper.co.uk/mysql/handy-way-to-copy-data-between-mysql-databases.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Using MYSQL Transactions in Cakephp</title>
		<link>http://www.lampdeveloper.co.uk/mysql/using-mysql-transactions-in-cakephp.html</link>
		<comments>http://www.lampdeveloper.co.uk/mysql/using-mysql-transactions-in-cakephp.html#comments</comments>
		<pubDate>Fri, 05 Jun 2009 12:07:02 +0000</pubDate>
		<dc:creator>Jamie</dc:creator>
				<category><![CDATA[Cakephp]]></category>
		<category><![CDATA[MYSQL]]></category>
		<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://www.lampdeveloper.co.uk/?p=112</guid>
		<description><![CDATA[After plenty of research and trials, it is possible to use mysql transactions for custom queries.
You still have to create the table with InnoDB for row based locking to work.


ALTER TABLE table_name type=InnoDB;

And set the transactions variable in your model


var $transactional = true;

Now in your method use the following:


class YourModel extends AppModel {

var $transactional = [...]]]></description>
			<content:encoded><![CDATA[<p>After plenty of research and trials, it is possible to use mysql transactions for custom queries.</p>
<p>You still have to create the table with InnoDB for row based locking to work.</p>
<pre name="code" class="sql">

ALTER TABLE table_name type=InnoDB;
</pre>
<p>And set the transactions variable in your model</p>
<pre name="code" class="php">

var $transactional = true;
</pre>
<p>Now in your method use the following:</p>
<pre name="code" class="php">

class YourModel extends AppModel {

var $transactional = true;

function mymethod()
{

  $db =&amp; ConnectionManager::getDataSource($this-&gt;useDbConfig);

 #start the transaction
  $db-&gt;begin($this);

 $success = $db-&gt;query(&#039;my sql&#039;);
  if ($success  === true) {
       return $db-&gt;commit($this);
  } else {
       $db-&gt;rollback($this);
       return false;
  }
}

}
</pre>
<p class="addtoany_share_save_container">
    <a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save?sitename=Blog%20of%20a%20LAMP%20Developer%20based%20near%20Guildford%2C%20Surrey&amp;siteurl=http%3A%2F%2Fwww.lampdeveloper.co.uk%2F&amp;linkname=Using%20MYSQL%20Transactions%20in%20Cakephp&amp;linkurl=http%3A%2F%2Fwww.lampdeveloper.co.uk%2Fmysql%2Fusing-mysql-transactions-in-cakephp.html"><img src="http://www.lampdeveloper.co.uk/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share/Save/Bookmark"/></a>

	</p>]]></content:encoded>
			<wfw:commentRss>http://www.lampdeveloper.co.uk/mysql/using-mysql-transactions-in-cakephp.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
