<?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>Cakephp For All</title>
	<atom:link href="http://www.blog.cakephp4all.com/?feed=rss2" rel="self" type="application/rss+xml" />
	<link>http://www.blog.cakephp4all.com</link>
	<description>Just another Cakephp weblog</description>
	<lastBuildDate>Mon, 26 Apr 2010 15:28:09 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Using Transactions in Cakephp</title>
		<link>http://www.blog.cakephp4all.com/?p=7</link>
		<comments>http://www.blog.cakephp4all.com/?p=7#comments</comments>
		<pubDate>Mon, 01 Mar 2010 05:10:51 +0000</pubDate>
		<dc:creator>Guillermo Mansilla</dc:creator>
				<category><![CDATA[Articles]]></category>
		<category><![CDATA[Tutorial]]></category>
		<category><![CDATA[Transactions cakephp mysql mssql oracle]]></category>

		<guid isPermaLink="false">http://www.blog.cakephp4all.com/?p=7</guid>
		<description><![CDATA[What? Yes, transactions&#8230; just like &#8220;begin, commit or rollback&#8221;
If you don&#8217;t know what a transaction is I will explain that to you very short: It is a way to prepare a bunch of queries in your data base and execute ALL of them when you are sure that every thing is ok, imagine that you [...]]]></description>
			<content:encoded><![CDATA[<p>What? Yes, transactions&#8230; just like &#8220;begin, commit or rollback&#8221;</p>
<p>If you don&#8217;t know what a transaction is I will explain that to you very short: It is a way to prepare a bunch of queries in your data base and execute ALL of them when you are sure that every thing is ok, imagine that you are the administrator of a Bank, you have received the order to give every customer a 500$ prize, now, imagine that your database has 1 million customers, what if you are executing the query and suddenly while the query  is updating  the record 800.001 there is an employee that accidentally disconnected the database server? FAIL! Well, almost all RDBMS have a feature called Transactions, with them you can make sure you wont have the problem I said before, imagine that all queries are saved in somewhere, after checking everything is ok and you write a commit sentence you will have all the queries executed.</p>
<p>With that said let&#8217;s move to Cakephp, We know Cake has validation rules and by default when you perform a Model::save() there is a call to Model::validates() and if validation fails cake will not tell database engine to save anything, We also know there is a Model::saveAll() method that help us to save 2 models related data and if one model does not pass validation rules there is nothing written to the database.</p>
<p>But what if you need to save data from 2 Models that are not related? if you write something like<br />
if ($this-&gt;Model1-&gt;save() &amp;&amp; $this-&gt;Model2-&gt;save()) {  &#8230;. }</p>
<p>You could have this problem: Lets suppose model2 does not pass the validation, then Model1 will write queries to the database and we don&#8217;t want that, we want to make sure both models save data only if both of them pass validation rules</p>
<p>I found this article in the bakery http://bakery.cakephp.org/articles/view/transaction-behavior but I noticed in the comments that this only works on Mysql databases because every database engine has its own syntax, that is why cakephp provides many database drivers, I didn&#8217;t test it however, you can try it if you want.</p>
<p>Fortunately Cakephp provides a tool to handle this no matter what database engine you are using.</p>
<p>this is how you use it in your controller (you could and should have this code in your Model but I am not going to talk about that now):</p>
<pre>
$this-&gt;User-&gt;transactional = true; //enable transactions in this model
$this-&gt;User-&gt;begin(); //begin the transaction
if ($this-&gt;User-&gt;save($this-&gt;data['User']) &amp;&amp; $this-&gt;Tec-&gt;save($this-&gt;data['Tec']) ) {
        //here the "User" data will not be written to database until I call Model::commit()
        $this-&gt;User-&gt;commit(); //Here I tell cake to execute the queries related to User model
        $this-&gt;flash(__('Data have been saved', true), array('action'=&gt;'index'));
} else { //if User model or Tec model fails then User model data will be rolled back
        $this-&gt;User-&gt;rollback();
}
</pre>
<div>Note that I use transaction in just 1 model (I could&#8217;ve used it in both models ) I did it in that way because when 1 model fails there are not going to be any changes to the database</div>
]]></content:encoded>
			<wfw:commentRss>http://www.blog.cakephp4all.com/?feed=rss2&amp;p=7</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Cakephp Core Components (even Helpers) or 3rd parties components</title>
		<link>http://www.blog.cakephp4all.com/?p=4</link>
		<comments>http://www.blog.cakephp4all.com/?p=4#comments</comments>
		<pubDate>Tue, 16 Feb 2010 01:55:00 +0000</pubDate>
		<dc:creator>Guillermo Mansilla</dc:creator>
				<category><![CDATA[Vendors Cake Core Components Helpers]]></category>

		<guid isPermaLink="false">http://www.blog.cakephp4all.com/?p=4</guid>
		<description><![CDATA[I am sure this question has been in your head more than once, well, in my experience I can tell you that you Should stick to Cakephp Core components. For example, 2 years ago when we were working with cakephp 1.1 I really I decided to use OthAuth Component/Helper, It works perferct only if you [...]]]></description>
			<content:encoded><![CDATA[<p>I am sure this question has been in your head more than once, well, in my experience I can tell you that you Should stick to Cakephp Core components. For example, 2 years ago when we were working with cakephp 1.1 I really I decided to use OthAuth Component/Helper, It works perferct only if you are using cake 1.1, I met the author in the irc channel and chatted with him a little, he is a very good programmer by the way, but when I got cake 1.2 I found a lot of gotchas that made my life impossible, I guess the author was to busy to upgrade his code, then I started to see tons of blogs and comments looking for possible solutions, at that point I decided to learn how to use Auth and Acl Cake Components, it was a little hard at first but once you get the idea you cant go back <img src='http://www.blog.cakephp4all.com/wp-includes/images/smilies/icon_biggrin.gif' alt=':D' class='wp-smiley' /> </p>
<p>So, think twice before using 3rd parties components/helpers in you production apps</p>
<p>When you use Cakephp components you can feel safer because they should update all the code and make sure every thing works ok.</p>
<p>You can avoid use Core Components or Helpers in this situations:</p>
<ul>
<li>You are feeling creative and want to write your own helper or component (You will need to check its functionality every time cakephp releases a new version, you may find your code does not work properly due to compatibility reasons, new syntax,  etc.)</li>
<li>There are not Core component/helper able to do what you want. (There you can write your own code or get someone else&#8217;s code, again, be sure you get updated code)</li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://www.blog.cakephp4all.com/?feed=rss2&amp;p=4</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
