1312060912
s:52056:"a:1:{s:3:"Rss";a:2:{s:7:"version";s:3:"2.0";s:7:"Channel";a:9:{s:5:"title";s:29:"Coding My Thoughts » CakePHP";s:4:"Link";a:2:{i:0;a:3:{s:4:"href";s:52:"http://marianoiglesias.com.ar/category/cakephp/feed/";s:3:"rel";s:4:"self";s:4:"type";s:19:"application/rss+xml";}i:1;s:29:"http://marianoiglesias.com.ar";}s:11:"description";s:36:"A glimpse at a coder's troubled mind";s:13:"lastBuildDate";s:31:"Mon, 30 May 2011 22:45:27 +0000";s:8:"language";s:2:"en";s:12:"updatePeriod";s:6:"hourly";s:15:"updateFrequency";s:1:"1";s:9:"generator";s:29:"http://wordpress.org/?v=3.2.1";s:4:"Item";a:10:{i:0;a:10:{s:5:"title";s:58:"Book release: CakePHP 1.3 Application Development Cookbook";s:4:"link";s:92:"http://marianoiglesias.com.ar/cakephp/book-release-cakephp-application-development-cookbook/";s:7:"pubDate";s:31:"Wed, 30 Mar 2011 22:49:58 +0000";s:7:"creator";s:7:"mariano";s:8:"Category";a:2:{i:0;s:7:"CakePHP";i:1;s:3:"PHP";}s:4:"guid";a:2:{s:5:"value";s:36:"http://marianoiglesias.com.ar/?p=221";s:11:"isPermaLink";s:5:"false";}s:11:"description";s:702:"Just a few days ago, I was happy to see my first book published. Entitled CakePHP 1.3 Application Development Cookbook, it&#8217;s a book released in the form of a cookbook, with a series of solutions to common problems one faces when developing CakePHP applications. While working on it, I tried to aim for developers at [...]Related posts:<ol><li><a
href='http://marianoiglesias.com.ar/ubuntu/book-meme/' rel='bookmark' title='Permanent Link: Book meme'>Book meme</a></li><li><a
href='http://marianoiglesias.com.ar/cakephp/cakefest-3-cakephp-in-berlin-july-9-12/' rel='bookmark' title='Permanent Link: CakeFest #3: CakePHP in Berlin, July 9-12'>CakeFest #3: CakePHP in Berlin, July 9-12</a></li></ol>";s:7:"encoded";s:3664:"<p>Just a few days ago, I was happy to see my first book published. Entitled <a
href="http://www.amazon.com/dp/1849511926/?tag=packtpubli-20" rel="nofollow" >CakePHP 1.3 Application Development Cookbook</a>, it&#8217;s a book released in the form of a cookbook, with a series of solutions to common problems one faces when developing <a
href="http://cakephp.org" rel="nofollow" >CakePHP</a> applications.</p><p><a
href="http://www.amazon.com/dp/1849511926/?tag=packtpubli-20" rel="nofollow" ><img
class="alignleft size-medium wp-image-222" style="margin-right: 10px;" title="CakePHP 1.3 Application Development Cookbook cover" src="http://marianoiglesias.com.ar/wp-content/uploads/2011/03/book-medium-243x300.png" alt="" width="194" height="240" /></a>While working on it, I tried to aim for developers at different levels of knowledge, yet a disclaimer has to be made: this is <strong>not a beginners book</strong>. It will not teach you how to install CakePHP, or how to get its friendly URLs working on Microsoft platforms (dodged that bullet.) It is written for CakePHP developers that are looking to solve different problems, and leverage their own applications. So no &#8220;building a blog&#8221; chapter in this book.</p><p>There are some recipes that deal with more complex topics, while others deal with what I consider interesting solutions to simple problems. Each recipe starts by proposing a problem, showing the solution, and giving an explanation of how the solution works. Most of the recipes include alternatives and extend the topic at hand beyond the scope of the problem they are solving, and some of them are based on open source packages that CakePHP community members (myself included) released.</p><p>This book also benefited from an unbelievably, super-cool, top of the world team of technical reviewers (the <a
href="http://mark-story.com" rel="nofollow" >CakePHP 1.3 lead developer</a> happens to be amongst them) that made its code shine (I am known for being humble.) They improved each recipe and proposed awesome alternatives to my original ideas. Because of that, this blog post is being written. I&#8217;m not sure sure I would&#8217;ve been as proud of the original version of the book <img
src='http://marianoiglesias.com.ar/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /></p><p>You should also know that the publishing company behind the publication of this book, <a
href="https://www.packtpub.com" rel="nofollow" >Packt Publishing</a>, is donating an important portion of the book earnings to the <a
href="http://cakefoundation.org" rel="nofollow" >Cake Software Foundation</a>, which is like <em>The Force</em> behind CakePHP. So I might not get rich, but at least the foundation will get some beers out of each sale. And trust me, nothing says thank you like a beer.</p><p>If you bought the book, I welcome any feedback you may have (you could also <a
href="http://www.amazon.com/dp/1849511926/?tag=packtpubli-20" rel="nofollow" >leave a review</a> and tell others how super cool the book is.) If you are more of a bytes person and less of a paper person, and look forward to reading the <strong>digital version</strong>, you can also get <a
href="http://link.packtpub.com/6ihp7k" rel="nofollow" >the ebook</a>.</p><p>Related posts:<ol><li><a
href='http://marianoiglesias.com.ar/ubuntu/book-meme/' rel='bookmark' title='Permanent Link: Book meme'>Book meme</a></li><li><a
href='http://marianoiglesias.com.ar/cakephp/cakefest-3-cakephp-in-berlin-july-9-12/' rel='bookmark' title='Permanent Link: CakeFest #3: CakePHP in Berlin, July 9-12'>CakeFest #3: CakePHP in Berlin, July 9-12</a></li></ol></p>";s:10:"commentRss";s:97:"http://marianoiglesias.com.ar/cakephp/book-release-cakephp-application-development-cookbook/feed/";s:8:"Comments";a:2:{i:0;s:101:"http://marianoiglesias.com.ar/cakephp/book-release-cakephp-application-development-cookbook/#comments";i:1;s:2:"12";}}i:1;a:10:{s:5:"title";s:44:"Pagination with custom find types in CakePHP";s:4:"link";s:83:"http://marianoiglesias.com.ar/cakephp/pagination-with-custom-find-types-in-cakephp/";s:7:"pubDate";s:31:"Tue, 28 Apr 2009 15:11:00 +0000";s:7:"creator";s:7:"mariano";s:8:"category";s:7:"CakePHP";s:4:"guid";a:2:{s:5:"value";s:77:"http://marianoiglesias.com.ar/1/pagination-with-custom-find-types-in-cakephp/";s:11:"isPermaLink";s:5:"false";}s:11:"description";s:1176:"With the release of CakePHP 1.2 a whole set of new features were made available to us bakers. One of those features is custom find types, which is one of the coolest things that ever happened since I realized I was cooler than Maverick. I&#8217;m not gonna go through custom find types, you can find [...]Related posts:<ol><li><a
href='http://marianoiglesias.com.ar/cakephp/modelizing-habtm-join-tables-in-cakephp-1-2-with-and-auto-with-models/' rel='bookmark' title='Permanent Link: Modelizing HABTM join tables in CakePHP 1.2: with and auto-with models'>Modelizing HABTM join tables in CakePHP 1.2: with and auto-with models</a></li><li><a
href='http://marianoiglesias.com.ar/business/the-danger-of-mba-types-running-software-development-firms/' rel='bookmark' title='Permanent Link: The danger of MBA types running software development firms'>The danger of MBA types running software development firms</a></li><li><a
href='http://marianoiglesias.com.ar/cakephp/cakephp-tip-of-the-day-pay-attention-to-conventions/' rel='bookmark' title='Permanent Link: CakePHP tip of the day: pay attention to conventions'>CakePHP tip of the day: pay attention to conventions</a></li></ol>";s:7:"encoded";s:6126:"<p>With the release of <a
href="http://cakephp.org" rel="nofollow" >CakePHP 1.2</a> a whole set of new features were made available to us bakers. One of those features is custom find types, which is one of the coolest things that ever happened since <a
href="http://video.google.com/videoplay?docid=-5883772879840922003" rel="nofollow" >I realized I was cooler than Maverick</a>.</p><p>I&#8217;m not gonna go through custom find types, you can find more info about them <a
href="http://www.pseudocoder.com/archives/2008/10/24/cakephp-custom-find-types/" rel="nofollow" >at Matt&#8217;s blog</a>, or at <a
href="http://c7y.phparch.com/c/entry/1/art,mvc_and_cake" rel="nofollow" >this article</a> written by someone whose name I think I&#8217;ve heard somewhere. What I&#8217;m going to talk about is how to mix your custom find types with pagination, without having to use <a
href="http://book.cakephp.org/view/249/Custom-Query-Pagination" rel="nofollow" >paginate and paginateCount</a> in your models.</p><p>So let&#8217;s first start by building yet another posts table, and inserting some records:</p><pre class="brush:sql">CREATE TABLE `posts`(
	`id` INT NOT NULL AUTO_INCREMENT,
	`title` VARCHAR(255) NOT NULL,
	`body` TEXT NOT NULL,
	`published` TINYINT(1) NOT NULL default 0,
	`created` DATETIME,
	`modified` DATETIME,
	PRIMARY KEY(`id`)
);

INSERT INTO `posts`(`title`, `body`, `published`, `created`, `modified`) VALUES
	('Post 1', 'Body for Post 1', 1, NOW(), NOW()),
	('Post 2', 'Body for Post 2', 0, NOW(), NOW()),
	('Post 3', 'Body for Post 3', 0, NOW(), NOW()),
	('Post 4', 'Body for Post 4', 1, NOW(), NOW()),
	('Post 5', 'Body for Post 5', 1, NOW(), NOW()),
	('Post 6', 'Body for Post 6', 0, NOW(), NOW()),
	('Post 7', 'Body for Post 7', 1, NOW(), NOW()),
	('Post 8', 'Body for Post 8', 1, NOW(), NOW()),
	('Post 9', 'Body for Post 9', 1, NOW(), NOW());</pre><p>Now let&#8217;s assume we want a find type called <code>published</code> to fetch only the published posts, and that we also want to be able to paginate using this find type. We will be approaching this through a generic approach, something that can be used throughout all our models. With this in mind, let&#8217;s first introduce a model based member variable called <code>$_types</code>, where we define the specific needs of each custom find type. Therefore, that variable will hold what we need as conditions, order, etc. for each custom find type. So let&#8217;s build our <code>Post</code> model:</p><pre class="brush:php">&lt;?php
class Post extends AppModel {
	public $name = 'Post';
	protected $_types = array(
		'published' =&gt; array(
			'conditions' =&gt; array('Post.published' =&gt; 1),
			'order' =&gt; array('Post.created' =&gt; 'desc')
		)
	);
}
?&gt;</pre><p>As you can see, we define options for each find type as if we would be calling <code>find()</code> directly. So with the above, instead of doing:</p><pre class="brush:php">$posts = $this-&gt;Post-&gt;find('all', array(
	'conditions' =&gt; array('Post.published' =&gt; 1),
	'order' =&gt; array('Post.created' =&gt; 'desc')
));</pre><p>We can now do:</p><pre class="brush:php">$posts = $this-&gt;Post-&gt;find('published');</pre><p>Now, what if we wanted to paginate with the above custom find type? Just as we set pagination parameters through the controller member variable <code>$paginate</code>, we can specify which find type pagination we&#8217;ll use. We do so by specifying the find type in the index 0 of the pagination settings. Like so:</p><pre class="brush:php">$this-&gt;paginate['Post'] = array(
	'published',
	'limit' =&gt; 10
);

$posts = $this-&gt;paginate('Post');</pre><p>Easy, huh? When this is specified, paginate() does the following:</p><ol><li>It issues a <code>find('count')</code> on the Post model, specifying the custom find type (<code>published</code>) in the <code>$options</code> array, through an option named <code>type</code>. Therefore, we can use <code>$options['type']</code> when our model is about to do the count to use the given options for our custom find type.</li><li>It fetches the records by calling <code>find()</code> with the custom find type, <code>find('published')</code> in our example.</li></ol><p>So where&#8217;s that sexy code? Add the following in your <code>AppModel</code>, making the above available for all our models.</p><pre class="brush:php">&lt;?php
class AppModel extends Model {
	public function find($type, $options = array()) {
		if (!empty($this-&gt;_types)) {
			$types = array_keys($this-&gt;_types);
			$type = (is_string($type) ? $type : null);
			if (!empty($type)) {
				if (($type == 'count' &amp;&amp; !empty($options['type']) &amp;&amp; in_array($options['type'], $types)) || in_array($type, $types)) {
					$options = Set::merge(
						$this-&gt;_types[($type == 'count' ? $options['type'] : $type)],
						array_diff_key($options, array('type'=&gt;true))
					);
				}
				if (in_array($type, $types)) {
					$type = (!empty($this-&gt;_types[$type]['type']) ? $this-&gt;_types[$type]['type'] : 'all');
				}
			}
		}
		return parent::find($type, $options);
	}
}
?&gt;</pre><p>Now ain&#8217;t CakePHP great? Don&#8217;t tell me, <a
href="http://cakefest.org" rel="nofollow" >tell everyone at CakeFest #3</a>.</p><p>Related posts:<ol><li><a
href='http://marianoiglesias.com.ar/cakephp/modelizing-habtm-join-tables-in-cakephp-1-2-with-and-auto-with-models/' rel='bookmark' title='Permanent Link: Modelizing HABTM join tables in CakePHP 1.2: with and auto-with models'>Modelizing HABTM join tables in CakePHP 1.2: with and auto-with models</a></li><li><a
href='http://marianoiglesias.com.ar/business/the-danger-of-mba-types-running-software-development-firms/' rel='bookmark' title='Permanent Link: The danger of MBA types running software development firms'>The danger of MBA types running software development firms</a></li><li><a
href='http://marianoiglesias.com.ar/cakephp/cakephp-tip-of-the-day-pay-attention-to-conventions/' rel='bookmark' title='Permanent Link: CakePHP tip of the day: pay attention to conventions'>CakePHP tip of the day: pay attention to conventions</a></li></ol></p>";s:10:"commentRss";s:88:"http://marianoiglesias.com.ar/cakephp/pagination-with-custom-find-types-in-cakephp/feed/";s:8:"Comments";a:2:{i:0;s:92:"http://marianoiglesias.com.ar/cakephp/pagination-with-custom-find-types-in-cakephp/#comments";i:1;s:2:"11";}}i:2;a:10:{s:5:"title";s:41:"CakeFest #3: CakePHP in Berlin, July 9-12";s:4:"link";s:77:"http://marianoiglesias.com.ar/cakephp/cakefest-3-cakephp-in-berlin-july-9-12/";s:7:"pubDate";s:31:"Mon, 23 Mar 2009 16:53:00 +0000";s:7:"creator";s:7:"mariano";s:8:"category";s:7:"CakePHP";s:4:"guid";a:2:{s:5:"value";s:71:"http://marianoiglesias.com.ar/1/cakefest-3-cakephp-in-berlin-july-9-12/";s:11:"isPermaLink";s:5:"false";}s:11:"description";s:968:"What better place to talk CakePHP than the world&#8217;s beer nation? That was the question that made the CakePHP Team decide CakeFest third edition should be located in Berlin, Germany. Just as Buenos Aires was the meat fest, I hereby predict that this will be the Beer Fest. Let&#8217;s face it, your code gets better [...]Related posts:<ol><li><a
href='http://marianoiglesias.com.ar/cakephp/cakephp-1-2-beta-released-and-cakefest-site-launched/' rel='bookmark' title='Permanent Link: CakePHP 1.2 beta released and CakeFest site launched'>CakePHP 1.2 beta released and CakeFest site launched</a></li><li><a
href='http://marianoiglesias.com.ar/cakephp/my-talk-in-cakefest-behaviors/' rel='bookmark' title='Permanent Link: My talk in CakeFest: behaviors'>My talk in CakeFest: behaviors</a></li><li><a
href='http://marianoiglesias.com.ar/cakephp/cakefest-was-a-success/' rel='bookmark' title='Permanent Link: CakeFest was a success'>CakeFest was a success</a></li></ol>";s:7:"encoded";s:1867:"<div><p>What better place to talk CakePHP than the world&#8217;s beer nation? That was the question that made the CakePHP Team decide CakeFest third edition should be located in Berlin, Germany. Just as Buenos Aires was the meat fest, I hereby predict that this will be the Beer Fest.</p><p>Let&#8217;s face it, your code gets better when you use CakePHP. So attending the official CakePHP gathering where all Core Developers and prominent community members go to share their knowledge is an offer you should not ignore. Add awesome beer and guaranteed fun to that recipe, and you would be insane not to join us.</p><p>So what are you waiting for? Go and <a
href="http://cakefest.org/registrations/add" rel="nofollow" >get your CakeFest ticket</a> as soon as possible, you don&#8217;t want to be left behind! If you have a company, or you are a regular Baker just wanting to show your love back to the project, do not hesitate to <a
href="http://cakefest.org/pages/sponsors" rel="nofollow" >sign up for the sponsorship packages</a>. Also help us spread the word by <a
href="http://cakefest.org/pages/badges" rel="nofollow" >placing these CakeFest badges</a> in your site, and you may even get a free ticket!</p></div><p>Related posts:<ol><li><a
href='http://marianoiglesias.com.ar/cakephp/cakephp-1-2-beta-released-and-cakefest-site-launched/' rel='bookmark' title='Permanent Link: CakePHP 1.2 beta released and CakeFest site launched'>CakePHP 1.2 beta released and CakeFest site launched</a></li><li><a
href='http://marianoiglesias.com.ar/cakephp/my-talk-in-cakefest-behaviors/' rel='bookmark' title='Permanent Link: My talk in CakeFest: behaviors'>My talk in CakeFest: behaviors</a></li><li><a
href='http://marianoiglesias.com.ar/cakephp/cakefest-was-a-success/' rel='bookmark' title='Permanent Link: CakeFest was a success'>CakeFest was a success</a></li></ol></p>";s:10:"commentRss";s:82:"http://marianoiglesias.com.ar/cakephp/cakefest-3-cakephp-in-berlin-july-9-12/feed/";s:8:"Comments";a:2:{i:0;s:86:"http://marianoiglesias.com.ar/cakephp/cakefest-3-cakephp-in-berlin-july-9-12/#comments";i:1;s:1:"0";}}i:3;a:10:{s:5:"title";s:51:"SVN rename all your CakePHP views from thtml to ctp";s:4:"link";s:90:"http://marianoiglesias.com.ar/cakephp/svn-rename-all-your-cakephp-views-from-thtml-to-ctp/";s:7:"pubDate";s:31:"Wed, 28 May 2008 16:53:00 +0000";s:7:"creator";s:7:"mariano";s:8:"category";s:7:"CakePHP";s:4:"guid";a:2:{s:5:"value";s:84:"http://marianoiglesias.com.ar/1/svn-rename-all-your-cakephp-views-from-thtml-to-ctp/";s:11:"isPermaLink";s:5:"false";}s:11:"description";s:717:"As I recently moved to Ubuntu 8.04 (plus CompizFusion + AWN which is rocking my world), I am now back to the good old days where bash was there to save my life. So basically I needed to SVN rename (also known as move) all my thtml views to ctp, recursively. Since this is a [...]Related posts:<ol><li><a
href='http://marianoiglesias.com.ar/ubuntu/simple-backup-script-for-ubuntu/' rel='bookmark' title='Permanent Link: Simple backup script for Ubuntu'>Simple backup script for Ubuntu</a></li><li><a
href='http://marianoiglesias.com.ar/cakephp/using-configure-in-cakephp-applications/' rel='bookmark' title='Permanent Link: Using Configure in CakePHP applications'>Using Configure in CakePHP applications</a></li></ol>";s:7:"encoded";s:997:"<p>As I recently moved to Ubuntu 8.04 (plus CompizFusion + AWN which is rocking my world), I am now back to the good old days where bash was there to save my life. So basically I needed to SVN rename (also known as move) all my thtml views to ctp, recursively. Since this is a short tip, I&#8217;ll get down to the details. Just cd to your /views path in CakePHP, and issue:</p><pre class="brush:bash">for file in `find . -name "*.thtml"` ; do svn mv $file `echo $file | sed s/\\\\.thtml/\\\\.ctp/` ; done</pre><p>After that you can safely commit. Enjoy!</p><p>Related posts:<ol><li><a
href='http://marianoiglesias.com.ar/ubuntu/simple-backup-script-for-ubuntu/' rel='bookmark' title='Permanent Link: Simple backup script for Ubuntu'>Simple backup script for Ubuntu</a></li><li><a
href='http://marianoiglesias.com.ar/cakephp/using-configure-in-cakephp-applications/' rel='bookmark' title='Permanent Link: Using Configure in CakePHP applications'>Using Configure in CakePHP applications</a></li></ol></p>";s:10:"commentRss";s:95:"http://marianoiglesias.com.ar/cakephp/svn-rename-all-your-cakephp-views-from-thtml-to-ctp/feed/";s:8:"Comments";a:2:{i:0;s:99:"http://marianoiglesias.com.ar/cakephp/svn-rename-all-your-cakephp-views-from-thtml-to-ctp/#comments";i:1;s:1:"5";}}i:4;a:10:{s:5:"title";s:22:"CakeFest was a success";s:4:"link";s:61:"http://marianoiglesias.com.ar/cakephp/cakefest-was-a-success/";s:7:"pubDate";s:31:"Thu, 14 Feb 2008 18:19:00 +0000";s:7:"creator";s:7:"mariano";s:8:"category";s:7:"CakePHP";s:4:"guid";a:2:{s:5:"value";s:55:"http://marianoiglesias.com.ar/1/cakefest-was-a-success/";s:11:"isPermaLink";s:5:"false";}s:11:"description";s:983:"I came back to Buenos Aires the day before yesterday, after a wonderful experience in Orlando. CakeFest was not only a great opportunity to be a part of the first official CakePHP gathering, but most importantly to meet a lot of smart people. As I said to someone while in Orlando, eventhough all the talks [...]Related posts:<ol><li><a
href='http://marianoiglesias.com.ar/cakephp/cakefest-3-cakephp-in-berlin-july-9-12/' rel='bookmark' title='Permanent Link: CakeFest #3: CakePHP in Berlin, July 9-12'>CakeFest #3: CakePHP in Berlin, July 9-12</a></li><li><a
href='http://marianoiglesias.com.ar/cakephp/cakephp-1-2-beta-released-and-cakefest-site-launched/' rel='bookmark' title='Permanent Link: CakePHP 1.2 beta released and CakeFest site launched'>CakePHP 1.2 beta released and CakeFest site launched</a></li><li><a
href='http://marianoiglesias.com.ar/business/cakefest-2-coming-up/' rel='bookmark' title='Permanent Link: CakeFest #2 coming up'>CakeFest #2 coming up</a></li></ol>";s:7:"encoded";s:4582:"<div><p>I came back to Buenos Aires the day before yesterday, after a wonderful experience in Orlando. <a
href="http://www.cakefest.org" rel="nofollow" >CakeFest</a> was not only a great opportunity to be a part of the first official CakePHP gathering, but most importantly to meet a lot of smart people. As I said to someone while in Orlando, eventhough all the talks were thorough and hands-down interesting, I found that the after hours were the moments where I got most out of the conference. Just hanging out with fellow bakers, drinking beer (lots of beer) and talking code to each other was an incredible opportunity.</p><p>I enjoyed my time there so much that I came back completely decided to host a CakeFest in Buenos Aires, Argentina during the first week of December, 2008. I am already taking care of the organization and will open up inscriptions very soon (so now you can&#8217;t complain, you have almost 10 months of notice.) Some of the talks will be english and some in spanish, so everyone is invited to join. As a corolary, I&#8217;ll leave you with some pictures taken in Orlando (wish I would&#8217;ve taken more). Click on the pictures to get their respective bigger size photos.</p><p><strong>DOWNLOAD</strong>: In case you are interested, you can download the slides of my presentation entitled <a
href="http://cricava.com/blogs/media/el_eternauta/posts/behaviors_making_your_models_behave.pdf" rel="nofollow" >Behaviors: Making your Models Behave</a>.</p><table
border="0" align="center"><tr><td><a
href="http://cricava.com/blogs/media/el_eternauta/posts/cakefest-lunch.jpg" rel="nofollow"  target="_blank"><img
src="http://cricava.com/blogs/media/el_eternauta/posts/cakefest-lunch-thumb.jpg" border="0" alt="" width="200" height="150" /></a><br
/>Lunch</td><td><a
href="http://cricava.com/blogs/media/el_eternauta/posts/cakefest-lunch_2.jpg" rel="nofollow"  target="_blank"><img
src="http://cricava.com/blogs/media/el_eternauta/posts/cakefest-lunch_2-thumb.jpg" border="0" alt="" width="200" height="150" /></a><br
/>Chit chat at salad bar</td></tr><tr><td><a
href="http://cricava.com/blogs/media/el_eternauta/posts/cakefest-gwoo_mac.jpg" rel="nofollow"  target="_blank"><img
src="http://cricava.com/blogs/media/el_eternauta/posts/cakefest-gwoo_mac-thumb.jpg" border="0" alt="" width="200" height="150" /></a><br
/>Gwoo and his Mac</td><td><a
href="http://cricava.com/blogs/media/el_eternauta/posts/cakefest-gwoo_keynote.jpg" rel="nofollow"  target="_blank"><img
src="http://cricava.com/blogs/media/el_eternauta/posts/cakefest-gwoo_keynote-thumb.jpg" border="0" alt="" width="200" height="150" /></a><br
/>Gwoo&#8217;s keynote</td></tr><tr><td><a
href="http://cricava.com/blogs/media/el_eternauta/posts/cakefest-felix_tim_and_me.jpg" rel="nofollow"  target="_blank"><img
src="http://cricava.com/blogs/media/el_eternauta/posts/cakefest-felix_tim_and_me-thumb.jpg" border="0" alt="" width="200" height="116" /></a><br
/>Felix (the_undefined), Tim and me</td><td><a
href="http://cricava.com/blogs/media/el_eternauta/posts/cakefest-phishy_and_me.jpg" rel="nofollow"  target="_blank"><img
src="http://cricava.com/blogs/media/el_eternauta/posts/cakefest-phishy_and_me-thumb.jpg" border="0" alt="" width="200" height="114" /></a><br
/>Jeff (phishy) and me. Marc in the background.</td></tr><tr><td><a
href="http://cricava.com/blogs/media/el_eternauta/posts/cakefest-dennis_and_me.jpg" rel="nofollow"  target="_blank"><img
src="http://cricava.com/blogs/media/el_eternauta/posts/cakefest-dennis_and_me-thumb.jpg" border="0" alt="" width="200" height="146" /></a><br
/>Dennis and me</td><td><a
href="http://cricava.com/blogs/media/el_eternauta/posts/cakefest-team.jpg" rel="nofollow"  target="_blank"><img
src="http://cricava.com/blogs/media/el_eternauta/posts/cakefest-team-thumb.jpg" border="0" alt="" width="200" height="124" /></a><br
/>Some of CakeFest team</td></tr></table></div><p>Related posts:<ol><li><a
href='http://marianoiglesias.com.ar/cakephp/cakefest-3-cakephp-in-berlin-july-9-12/' rel='bookmark' title='Permanent Link: CakeFest #3: CakePHP in Berlin, July 9-12'>CakeFest #3: CakePHP in Berlin, July 9-12</a></li><li><a
href='http://marianoiglesias.com.ar/cakephp/cakephp-1-2-beta-released-and-cakefest-site-launched/' rel='bookmark' title='Permanent Link: CakePHP 1.2 beta released and CakeFest site launched'>CakePHP 1.2 beta released and CakeFest site launched</a></li><li><a
href='http://marianoiglesias.com.ar/business/cakefest-2-coming-up/' rel='bookmark' title='Permanent Link: CakeFest #2 coming up'>CakeFest #2 coming up</a></li></ol></p>";s:10:"commentRss";s:66:"http://marianoiglesias.com.ar/cakephp/cakefest-was-a-success/feed/";s:8:"Comments";a:2:{i:0;s:70:"http://marianoiglesias.com.ar/cakephp/cakefest-was-a-success/#comments";i:1;s:1:"9";}}i:5;a:10:{s:5:"title";s:26:"CakeFest starting tomorrow";s:4:"link";s:65:"http://marianoiglesias.com.ar/cakephp/cakefest-starting-tomorrow/";s:7:"pubDate";s:31:"Tue, 05 Feb 2008 18:05:00 +0000";s:7:"creator";s:7:"mariano";s:8:"category";s:7:"CakePHP";s:4:"guid";a:2:{s:5:"value";s:59:"http://marianoiglesias.com.ar/1/cakefest-starting-tomorrow/";s:11:"isPermaLink";s:5:"false";}s:11:"description";s:762:"With just hours left to launch the first CakeFest conference, people are starting to show up and speakers are working &#8217;round the clock with their presentations. However, something has been catching my attention since I came to the hotel: I am different. That&#8217;s right, I am unique. I stand out as an oasis in the [...]Related posts:<ol><li><a
href='http://marianoiglesias.com.ar/cakephp/cakefest-was-a-success/' rel='bookmark' title='Permanent Link: CakeFest was a success'>CakeFest was a success</a></li><li><a
href='http://marianoiglesias.com.ar/general/seven-things-you-dont-know-about-me-meme/' rel='bookmark' title='Permanent Link: Seven Things You Don&#8217;t Know About Me (MEME)'>Seven Things You Don&#8217;t Know About Me (MEME)</a></li></ol>";s:7:"encoded";s:1291:"<div><p>With just hours left to launch the first <a
href="http://www.cakefest.org" rel="nofollow" >CakeFest</a> conference, people are starting to show up and speakers are working &#8217;round the clock with their presentations. However, something has been catching my attention since I came to the hotel: I am different. That&#8217;s right, I am unique. I stand out as an oasis in the middle of the desert. Don&#8217;t take my word for it, just look at my dear old Thinkpad standing in a pool of Macs. And in case you are wondering, people there from left to right are: Jeff Loiselle (aka phishy), Felix Geisend&#246;rfer (aka the_undefined), Tim Koschuetzki, and giving his back is Dennis Hennen.</p><p
align="center"><img
src="http://cricava.com/blogs/media/el_eternauta/posts/mac-ownage.jpg" border="0" alt="" width="512" height="384" /></p></div><p>Related posts:<ol><li><a
href='http://marianoiglesias.com.ar/cakephp/cakefest-was-a-success/' rel='bookmark' title='Permanent Link: CakeFest was a success'>CakeFest was a success</a></li><li><a
href='http://marianoiglesias.com.ar/general/seven-things-you-dont-know-about-me-meme/' rel='bookmark' title='Permanent Link: Seven Things You Don&#8217;t Know About Me (MEME)'>Seven Things You Don&#8217;t Know About Me (MEME)</a></li></ol></p>";s:10:"commentRss";s:70:"http://marianoiglesias.com.ar/cakephp/cakefest-starting-tomorrow/feed/";s:8:"Comments";a:2:{i:0;s:74:"http://marianoiglesias.com.ar/cakephp/cakefest-starting-tomorrow/#comments";i:1;s:1:"1";}}i:6;a:10:{s:5:"title";s:30:"My talk in CakeFest: behaviors";s:4:"link";s:68:"http://marianoiglesias.com.ar/cakephp/my-talk-in-cakefest-behaviors/";s:7:"pubDate";s:31:"Fri, 25 Jan 2008 17:51:00 +0000";s:7:"creator";s:7:"mariano";s:8:"category";s:7:"CakePHP";s:4:"guid";a:2:{s:5:"value";s:62:"http://marianoiglesias.com.ar/1/my-talk-in-cakefest-behaviors/";s:11:"isPermaLink";s:5:"false";}s:11:"description";s:998:"With the recent release of the confirmed schedule for the CakeFest conference, you can see that my talk entitled Making your Models Behave has been allocated for Friday, February 8 at 11.15 am. Right after Nate&#8217;s keynote, and before lunch, what an honor For those of you who are attending my talk will be focused [...]Related posts:<ol><li><a
href='http://marianoiglesias.com.ar/cakephp/cakefest-3-cakephp-in-berlin-july-9-12/' rel='bookmark' title='Permanent Link: CakeFest #3: CakePHP in Berlin, July 9-12'>CakeFest #3: CakePHP in Berlin, July 9-12</a></li><li><a
href='http://marianoiglesias.com.ar/cakephp/cakefest-was-a-success/' rel='bookmark' title='Permanent Link: CakeFest was a success'>CakeFest was a success</a></li><li><a
href='http://marianoiglesias.com.ar/cakephp/cakephp-1-2-beta-released-and-cakefest-site-launched/' rel='bookmark' title='Permanent Link: CakePHP 1.2 beta released and CakeFest site launched'>CakePHP 1.2 beta released and CakeFest site launched</a></li></ol>";s:7:"encoded";s:1728:"<div><p>With the recent release of the <a
href="http://www.cakefest.org/pages/schedule" rel="nofollow" >confirmed schedule</a> for the <a
href="http://www.cakefest.org" rel="nofollow" >CakeFest conference</a>, you can see that my talk entitled <strong>Making your Models Behave</strong> has been allocated for Friday, February 8 at 11.15 am. Right after Nate&#8217;s keynote, and before lunch, what an honor <img
src='http://marianoiglesias.com.ar/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /></p><p>For those of you who are attending my talk will be focused in understanding and exploiting the potential of what I consider to be one of the coolest features CakePHP has brought us: model behaviors. And for those of you who are not going, what are you waiting for? There are still tickets available. Reserve your flight and your stay, and head on to the conference.</p><p>If for some reason or another you are not going, we will be releasing the presentation slides on the CakeFest site sometime in the feature.</p></div><p>Related posts:<ol><li><a
href='http://marianoiglesias.com.ar/cakephp/cakefest-3-cakephp-in-berlin-july-9-12/' rel='bookmark' title='Permanent Link: CakeFest #3: CakePHP in Berlin, July 9-12'>CakeFest #3: CakePHP in Berlin, July 9-12</a></li><li><a
href='http://marianoiglesias.com.ar/cakephp/cakefest-was-a-success/' rel='bookmark' title='Permanent Link: CakeFest was a success'>CakeFest was a success</a></li><li><a
href='http://marianoiglesias.com.ar/cakephp/cakephp-1-2-beta-released-and-cakefest-site-launched/' rel='bookmark' title='Permanent Link: CakePHP 1.2 beta released and CakeFest site launched'>CakePHP 1.2 beta released and CakeFest site launched</a></li></ol></p>";s:10:"commentRss";s:73:"http://marianoiglesias.com.ar/cakephp/my-talk-in-cakefest-behaviors/feed/";s:8:"Comments";a:2:{i:0;s:77:"http://marianoiglesias.com.ar/cakephp/my-talk-in-cakefest-behaviors/#comments";i:1;s:1:"4";}}i:7;a:10:{s:5:"title";s:52:"CakePHP 1.2 beta released and CakeFest site launched";s:4:"link";s:91:"http://marianoiglesias.com.ar/cakephp/cakephp-1-2-beta-released-and-cakefest-site-launched/";s:7:"pubDate";s:31:"Wed, 02 Jan 2008 20:44:00 +0000";s:7:"creator";s:7:"mariano";s:8:"category";s:7:"CakePHP";s:4:"guid";a:2:{s:5:"value";s:85:"http://marianoiglesias.com.ar/1/cakephp-1-2-beta-released-and-cakefest-site-launched/";s:11:"isPermaLink";s:5:"false";}s:11:"description";s:961:"A lot has happened in the last few weeks, but it all came together today: CakePHP 1.2 has reached its beta status and the CakeFest website has launched officialy. CakeFest is the first official gathering related to CakePHP, and will be celebrated in Orlando, Florida, on February 6, 7, and 8, 2008. You&#8217;ll get a [...]Related posts:<ol><li><a
href='http://marianoiglesias.com.ar/cakephp/cakefest-3-cakephp-in-berlin-july-9-12/' rel='bookmark' title='Permanent Link: CakeFest #3: CakePHP in Berlin, July 9-12'>CakeFest #3: CakePHP in Berlin, July 9-12</a></li><li><a
href='http://marianoiglesias.com.ar/cakephp/cakefest-was-a-success/' rel='bookmark' title='Permanent Link: CakeFest was a success'>CakeFest was a success</a></li><li><a
href='http://marianoiglesias.com.ar/cakephp/response-to-dho-leaving-the-cakephp-team/' rel='bookmark' title='Permanent Link: Response to dho leaving the CakePHP team'>Response to dho leaving the CakePHP team</a></li></ol>";s:7:"encoded";s:1620:"<div><p> A lot has happened in the last few weeks, but it all came together today: <a
href="http://bakery.cakephp.org/articles/view/new-year-new-beta" rel="nofollow" >CakePHP 1.2</a> has reached its beta status and the <a
href="http://www.cakefest.org" rel="nofollow" >CakeFest website</a> has launched officialy. CakeFest is the first official gathering related to CakePHP, and will be celebrated in Orlando, Florida, on February 6, 7, and 8, 2008. You&#8217;ll get a chance to see  experienced CakePHP developers (that&#8217;s right, the core team will be there) sharing their knowledge in talks and different events. I will be there as well, regardless if that&#8217;s good or bad news to you all. So don&#8217;t wait any longer, go and find out about the <a
href="http://www.cakefest.org" rel="nofollow" >CakePHP festivities</a> and save your ticket <img
src='http://marianoiglesias.com.ar/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /></p></div><p>Related posts:<ol><li><a
href='http://marianoiglesias.com.ar/cakephp/cakefest-3-cakephp-in-berlin-july-9-12/' rel='bookmark' title='Permanent Link: CakeFest #3: CakePHP in Berlin, July 9-12'>CakeFest #3: CakePHP in Berlin, July 9-12</a></li><li><a
href='http://marianoiglesias.com.ar/cakephp/cakefest-was-a-success/' rel='bookmark' title='Permanent Link: CakeFest was a success'>CakeFest was a success</a></li><li><a
href='http://marianoiglesias.com.ar/cakephp/response-to-dho-leaving-the-cakephp-team/' rel='bookmark' title='Permanent Link: Response to dho leaving the CakePHP team'>Response to dho leaving the CakePHP team</a></li></ol></p>";s:10:"commentRss";s:96:"http://marianoiglesias.com.ar/cakephp/cakephp-1-2-beta-released-and-cakefest-site-launched/feed/";s:8:"Comments";a:2:{i:0;s:100:"http://marianoiglesias.com.ar/cakephp/cakephp-1-2-beta-released-and-cakefest-site-launched/#comments";i:1;s:1:"1";}}i:8;a:10:{s:5:"title";s:47:"ExpectsBehavior, model unbinding in CakePHP 1.2";s:4:"link";s:85:"http://marianoiglesias.com.ar/cakephp/expectsbehavior-model-unbinding-in-cakephp-1-2/";s:7:"pubDate";s:31:"Mon, 29 Oct 2007 23:21:00 +0000";s:7:"creator";s:7:"mariano";s:8:"category";s:7:"CakePHP";s:4:"guid";a:2:{s:5:"value";s:79:"http://marianoiglesias.com.ar/1/expectsbehavior-model-unbinding-in-cakephp-1-2/";s:11:"isPermaLink";s:5:"false";}s:11:"description";s:1160:"UPDATE: the first beta version has been released, and renamed to Bindable Behavior. Check it out at its bakery tutorial Bindable Behavior: control your model bindings. It&#8217;s been some time since I&#8217;ve done any work on expects, and while enjoying the incredible set of cool features CakePHP 1.2 pre-beta is giving us all, I decided [...]Related posts:<ol><li><a
href='http://marianoiglesias.com.ar/cakephp/limiting-the-associated-models-returned-with-cakephp/' rel='bookmark' title='Permanent Link: Limiting the associated models returned with CakePHP'>Limiting the associated models returned with CakePHP</a></li><li><a
href='http://marianoiglesias.com.ar/cakephp/modelizing-habtm-join-tables-in-cakephp-1-2-with-and-auto-with-models/' rel='bookmark' title='Permanent Link: Modelizing HABTM join tables in CakePHP 1.2: with and auto-with models'>Modelizing HABTM join tables in CakePHP 1.2: with and auto-with models</a></li><li><a
href='http://marianoiglesias.com.ar/cakephp/using-configure-in-cakephp-applications/' rel='bookmark' title='Permanent Link: Using Configure in CakePHP applications'>Using Configure in CakePHP applications</a></li></ol>";s:7:"encoded";s:7149:"<p> <strong>UPDATE</strong>: the first beta version has been released, and renamed to Bindable Behavior. Check it out at its bakery tutorial <a
href="http://bakery.cakephp.org/articles/view/bindable-behavior-control-your-model-bindings" rel="nofollow" >Bindable Behavior: control your model bindings</a>.</p><p> It&#8217;s been some time since I&#8217;ve done any work on <a
href="http://bakery.cakephp.org/articles/view/an-improvement-to-unbindmodel-on-model-side" rel="nofollow" >expects</a>, and while enjoying the incredible set of cool features CakePHP 1.2 pre-beta is giving us all, I decided it was time to let expects meet 1.2. My new baby is called <strong>ExpectsBehavior</strong>. As its 1.1 predecessor, its main goal is to provide a wrapper for CakePHP&#8217;s unbindModel function, to let the developer specify what bindings (model relationships) he&#8217;s expecting right before a find. At the same time, I needed some new features to be added, some of them courtesy of the previous brain work Felix did on his very cool <a
href="http://www.thinkingphp.org/2007/06/14/containable-20-beta/" rel="nofollow" >Containable behavior</a>.</p><p><span
id="more-27"></span></p><p> So what are some of these cool new features I&#8217;m talking about?</p><ul><li><strong>Parameter notation flexibility</strong>: you can choose different ways on how to specify the models you need. Whatever is sexier for you!</li><li><strong>Calling methods</strong>: you can use the old way (calling Article-&gt;expects() right before your find), or use the newly CakePHP find notation with embedded parameters for the behavior, specifying your expects setting right on the Model::find() call.</li><li><strong>Override binding settings</strong>: tired of having to do a bindModel() to override some binding settings (such as limit, order, offset) right before your expects call? Want to only get certain fields of a related model, instead of all of them? Don&#8217;t stress! ExpectsBehavior allows you to override these settings easily.</li><li><strong>Auto recursivity</strong>: by default, ExpectsBehavior will determine the necessary recursivity needed to fetch the models you specified, and will set the main model recursivity accordingly. This feature can be disabled via a behavior setting.</li><li><strong>Warnings</strong>: if you enable a behavior setting, ExpectsBehavior will issue notices when specified bindings do not really exist. By default this setting is disabled.</li><li><strong>Thorough testing</strong>: I&#8217;m working very hard in adding tests for almost every conceivable aspect of the ExpectsBehavior. I&#8217;m already at 433 asserts, and counting! All succeeding, naturally <img
src='http://marianoiglesias.com.ar/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /></li></ul><p> Let&#8217;s start with the basics: <strong>parameter notation</strong>. ExpectsBehavior allows you to use two types of alternative notations when defining the bindings you are expecting: dot notation, and depth notation. Dot notation comes from expects 1.1, while depth notation is an alternative way where you specify the models using associative arrays. Let&#8217;s start with some samples (both calls will produce the same result):</p><pre class="brush:php">// Dot notation

$this-&gt;Article-&gt;expects('Category', 'Comment.User', 'Tag');

// Depth notation

$this-&gt;Article-&gt;expects(array('Category', 'Comment' =&gt; 'User', 'Tag'));</pre><p> Ok, let&#8217;s get a little deeper:</p><pre class="brush:php">// Dot notation

$this-&gt;Article-&gt;expects('Category.Section', 'Comment.User.Profile', 'Tag');

// Depth notation

$this-&gt;Article-&gt;expects(array('Category' =&gt; 'Section', 'Comment' =&gt; array('User' =&gt; 'Profile'), 'Tag'));</pre><p> What about mixing them up?</p><pre class="brush:php">$this-&gt;Article-&gt;expects(array('Category' =&gt; 'Section', 'Comment' =&gt; 'User.Profile', 'Tag'));</pre><p> Cool, huh? Now, with <strong>calling methods</strong> you can still call expects the way we&#8217;ve just seen (the old way), or use the new, cool way: embedded parameters. Instead of doing:</p><pre class="brush:php">$this-&gt;Article-&gt;expects(array('Category', 'Comment' =&gt; 'User', 'Tag'));
$result = $this-&gt;Article-&gt;findAll();</pre><p> Why not just do:</p><pre class="brush:php">$result = $this-&gt;Article-&gt;find('all', array('expects' =&gt; array(
	'Category', 'Comment' =&gt; 'User', 'Tag'
)));</pre><p> Wait, does that really work? Yeah it does! Really? Of course! But don&#8217;t go anywhere, there&#8217;s more. Amongst the features I mentioned you can now <strong>override binding settings</strong>. Let&#8217;s see some code! Assume we just want the fields &#8216;username&#8217;, and &#8216;email&#8217; for the User binding on our previous call, so we simply change it to:</p><pre class="brush:php">$result = $this-&gt;Article-&gt;find('all', array('expects' =&gt; array(
	'Category',
	'Comment' =&gt; array('User' =&gt; array('username', 'email')),
	'Tag'
)));</pre><p> Or if you don&#8217;t like seeing so many arrays, use the embedded field notation:</p><pre class="brush:php">$result = $this-&gt;Article-&gt;find('all', array('expects' =&gt; array(
	'Category',
	'Comment' =&gt; 'User(username, email)',
	'Tag'
)));</pre><p> What about mixing fields and inner models? Let&#8217;s bring up the Profile attached to a User, and the Friend the user&#8217;s got:</p><pre class="brush:php">$result = $this-&gt;Article-&gt;find('all', array('expects' =&gt; array(
	'Category',
	'Comment' =&gt; array('User' =&gt; array('username', 'email', 'Profile', 'Friend')),
	'Tag'
)));</pre><p> What about other settings? Like limit, and order? Sure, no problem. Say we want to get only the first 5 tags related to the articles, and order them by their field tag. Then let&#8217;s change the above to:</p><pre class="brush:php">$result = $this-&gt;Article-&gt;find('all', array('expects' =&gt; array(
	'Category',
	'Comment' =&gt; array('User' =&gt; array('username', 'email', 'Profile', 'Friend')),
	'Tag' =&gt; array('limit' =&gt; 5, 'order' =&gt; 'tag ASC')
)));</pre><p> I&#8217;m not yet releasing it since I want to add further testing, but I will be posting the sourcecode on a live server very soon. So stay tuned, and until then I welcome your feedback.</p><p>Related posts:<ol><li><a
href='http://marianoiglesias.com.ar/cakephp/limiting-the-associated-models-returned-with-cakephp/' rel='bookmark' title='Permanent Link: Limiting the associated models returned with CakePHP'>Limiting the associated models returned with CakePHP</a></li><li><a
href='http://marianoiglesias.com.ar/cakephp/modelizing-habtm-join-tables-in-cakephp-1-2-with-and-auto-with-models/' rel='bookmark' title='Permanent Link: Modelizing HABTM join tables in CakePHP 1.2: with and auto-with models'>Modelizing HABTM join tables in CakePHP 1.2: with and auto-with models</a></li><li><a
href='http://marianoiglesias.com.ar/cakephp/using-configure-in-cakephp-applications/' rel='bookmark' title='Permanent Link: Using Configure in CakePHP applications'>Using Configure in CakePHP applications</a></li></ol></p>";s:10:"commentRss";s:90:"http://marianoiglesias.com.ar/cakephp/expectsbehavior-model-unbinding-in-cakephp-1-2/feed/";s:8:"Comments";a:2:{i:0;s:94:"http://marianoiglesias.com.ar/cakephp/expectsbehavior-model-unbinding-in-cakephp-1-2/#comments";i:1;s:2:"13";}}i:9;a:10:{s:5:"title";s:52:"CakePHP tip of the day: pay attention to conventions";s:4:"link";s:90:"http://marianoiglesias.com.ar/cakephp/cakephp-tip-of-the-day-pay-attention-to-conventions/";s:7:"pubDate";s:31:"Tue, 23 Oct 2007 20:31:00 +0000";s:7:"creator";s:7:"mariano";s:8:"category";s:7:"CakePHP";s:4:"guid";a:2:{s:5:"value";s:84:"http://marianoiglesias.com.ar/1/cakephp-tip-of-the-day-pay-attention-to-conventions/";s:11:"isPermaLink";s:5:"false";}s:11:"description";s:1146:"CakePHP 1.2 sexyness is mainly a result of its convention over configuration methodology. Theoretical discussions aside, conventions save us (developers) valuable time, and allow us to concentrate on where we should put our effort: business logic. Some conventions may go under the radar until you find what you think is a weird bug. Let&#8217;s see [...]Related posts:<ol><li><a
href='http://marianoiglesias.com.ar/cakephp/cakephp-1-2-tip-of-the-day-be-mindful-of-modelset/' rel='bookmark' title='Permanent Link: CakePHP 1.2 tip of the day: be mindful of Model::set'>CakePHP 1.2 tip of the day: be mindful of Model::set</a></li><li><a
href='http://marianoiglesias.com.ar/cakephp/something-to-consider-when-saving-habtm-records-in-cakephp/' rel='bookmark' title='Permanent Link: Something to consider when saving HABTM records in CakePHP'>Something to consider when saving HABTM records in CakePHP</a></li><li><a
href='http://marianoiglesias.com.ar/cakephp/pagination-with-custom-find-types-in-cakephp/' rel='bookmark' title='Permanent Link: Pagination with custom find types in CakePHP'>Pagination with custom find types in CakePHP</a></li></ol>";s:7:"encoded";s:5662:"<p> CakePHP 1.2 sexyness is mainly a result of its convention over configuration methodology. Theoretical discussions aside, conventions save us (developers) valuable time, and allow us to concentrate on where we should put our effort: business logic. Some conventions may go under the radar until you find what <strong>you think</strong> is a weird bug. Let&#8217;s see an example by building a simple action in a controller:</p><p><span
id="more-28"></span></p><pre class="brush:php">class PostsController extends AppController {
	function edit($slug) {
		$post = $this-&gt;Post-&gt;findBySlug($slug);

		if (!empty($this-&gt;data)) {
			// We have the post already, so don't let user submit and ID
			// in POSTed information, just in case he gets funky ideas

			if (isset($this-&gt;data['Post']['id'])) {
				unset($this-&gt;data['Post']['id']);
			}

			$this-&gt;Post-&gt;set($this-&gt;data);
			if ($this-&gt;Post-&gt;validates()) {
				// Force correct ID
				$this-&gt;data['Post']['id'] = $post['Post']['id'];

				// Do something else we need to do before saving
				// ...

				// and now save
				$this-&gt;Post-&gt;save($this-&gt;data);
			}
		}
	}
}</pre><p> To our surprise, you&#8217;ll see that the above code always generates a new Post, instead of editing the post which slug is $slug. Why is that? I&#8217;ll give you a hint: print out $this->Post->id just before you execute the validates(), and you&#8217;ll see that it is set to whatever the slug is. What? So ID is set to a slug? Now, why the heck is that? Let&#8217;s dive into CakePHP core code, and pay special attention to the first few lines of Controller::constructClasses():</p><pre class="brush:php">if (empty($this-&gt;passedArgs) || !isset($this-&gt;passedArgs['0'])) {
	$id = false;
} else {
	$id = $this-&gt;passedArgs['0'];
}

if ($this-&gt;uses === false) {
	$this-&gt;loadModel($this-&gt;modelClass, $id);
} elseif ($this-&gt;uses) {
	$uses = is_array($this-&gt;uses) ? $this-&gt;uses : array($this-&gt;uses);
	$this-&gt;modelClass = $uses[0];
	foreach ($uses as $modelClass) {
		$this-&gt;loadModel($modelClass);
	}
}</pre><p> As you can see CakePHP, when only the auto-model linked to our controller is used (i.e: we&#8217;re not using Controller::$uses), will assume that the first parameter sent to the current action (if there&#8217;s indeed one) corresponds to the model ID. Therefore on our case it is set to whatever the $slug passed was. Now you may be asking, why should that still be a problem if we&#8217;re forcing the ID right before the save? Once again, let&#8217;s dive into the core, and see that when Model::invalidFields() (called by Model::validates()) is figuring out what rules to execute during validation, it does the following so it knows if the current record being validated is a new record, or an existing one:</p><pre class="brush:php">$exists = $this-&gt;exists();</pre><p> For performance reasons, Model::exists() will save internally a variable so the next time someone calls Model::exists() it doesn&#8217;t have to execute another COUNT, unless you force it (by passing true as a parameter to exists). Model::save() naturally needs to know if the record exists to figure out if it&#8217;s an INSERT or an UPDATE. How does it find out? You&#8217;ve guessed it: by calling exists:</p><pre class="brush:php">$exists = $this-&gt;exists();</pre><p> It is not forcing exists() to re-run the query, which means on our case Model::exists() will return the value found the last time it was executed, which was back when the model was being validated. At that time Model::$id was set to $slug, so naturally Model::exists() said this record doesn&#8217;t exist. Hence, Model::save() thinks now the record doesn&#8217;t exist, and the save gets executed as an INSERT.</p><p> Now don&#8217;t get all hasty and think this is a bug, or complain saying that Model::save() should force Model::exists() to refresh. Instead think that we may be missing the point: there&#8217;s another convention which says Model should know the real Model ID before a validation is performed. It is a convention for our own benefit: so we can specify certain validation rules to run only on update, create, or both. So we need to change our code and <strong>make sure that the right model ID</strong> is set before executing Model::validates():</p><pre class="brush:php">class PostsController extends AppController {
	function edit($slug) {
		$post = $this-&gt;Post-&gt;findBySlug($slug);

		if (!empty($this-&gt;data)) {
			// Let's force the ID, just in case user gets funky ideas
			$this-&gt;data['Post']['id'] = $post['Post']['id'];

			$this-&gt;Post-&gt;set($this-&gt;data);
			if ($this-&gt;Post-&gt;validates()) {
				// Do something else we need to do before saving
				// ...

				// and now save
				$this-&gt;Post-&gt;save($this-&gt;data);
			}
		}
	}
}</pre><p>Related posts:<ol><li><a
href='http://marianoiglesias.com.ar/cakephp/cakephp-1-2-tip-of-the-day-be-mindful-of-modelset/' rel='bookmark' title='Permanent Link: CakePHP 1.2 tip of the day: be mindful of Model::set'>CakePHP 1.2 tip of the day: be mindful of Model::set</a></li><li><a
href='http://marianoiglesias.com.ar/cakephp/something-to-consider-when-saving-habtm-records-in-cakephp/' rel='bookmark' title='Permanent Link: Something to consider when saving HABTM records in CakePHP'>Something to consider when saving HABTM records in CakePHP</a></li><li><a
href='http://marianoiglesias.com.ar/cakephp/pagination-with-custom-find-types-in-cakephp/' rel='bookmark' title='Permanent Link: Pagination with custom find types in CakePHP'>Pagination with custom find types in CakePHP</a></li></ol></p>";s:10:"commentRss";s:95:"http://marianoiglesias.com.ar/cakephp/cakephp-tip-of-the-day-pay-attention-to-conventions/feed/";s:8:"Comments";a:2:{i:0;s:99:"http://marianoiglesias.com.ar/cakephp/cakephp-tip-of-the-day-pay-attention-to-conventions/#comments";i:1;s:1:"4";}}}}}}";
