<?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>zeroasterisk &#187; beforeSave</title>
	<atom:link href="http://zeroasterisk.com/tag/beforesave/feed/" rel="self" type="application/rss+xml" />
	<link>http://zeroasterisk.com</link>
	<description>zeroasterisk blog - alan blount &#38; friends</description>
	<lastBuildDate>Tue, 20 Dec 2011 17:20:16 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.6</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Cakephp Beforesave() Gotcha: Need To Set $this-&gt;__exists To True If Setting A Primary Key</title>
		<link>http://zeroasterisk.com/2010/02/18/cakephp-beforesave-gotcha-this-__exists/</link>
		<comments>http://zeroasterisk.com/2010/02/18/cakephp-beforesave-gotcha-this-__exists/#comments</comments>
		<pubDate>Fri, 19 Feb 2010 02:59:45 +0000</pubDate>
		<dc:creator>alan</dc:creator>
				<category><![CDATA[cakephp]]></category>
		<category><![CDATA[development]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[beforeSave]]></category>
		<category><![CDATA[cake]]></category>

		<guid isPermaLink="false">http://zeroasterisk.com/?p=380</guid>
		<description><![CDATA[if you add a primary key which would turn an "insert" into an "update" within beforeSave() you'll need to set $this->__exists = true;... the call to $this->exists(); happens in model.php before the callback to beforeSave().]]></description>
			<content:encoded><![CDATA[<p>I added an update to <a href="http://book.cakephp.org/view/76/Callback-Methods">CakePHP Book: beforeSave()</a></p>
<blockquote><p>Also, if you add a primary key which would turn an &#8220;insert&#8221; into an &#8220;update&#8221; within beforeSave() you&#8217;ll need to set <code>$this-&gt;__exists = true;</code>&#8230; the call to <a href="http://api12.cakephp.org/view_source/model/#line-1159">$this-&gt;exists();</a> happens in model.php before the callback to <a href="http://api12.cakephp.org/view_source/model/#line-1191">beforeSave()</a>.</p>
<pre>function beforeSave() {
	if (!isset($this-&gt;data[$this-&gt;name['id') &amp;&amp; isset($this-&gt;data[$this-&gt;name['unique_field')) {
		$found = $this-&gt;find("first",array(
				"recursive" =&gt; -1,
				"fields" =&gt; array("id"),
				"conditions" =&gt; array("unique_field" =&gt; $this-&gt;data[$this-&gt;name['unique_field')));
		if (!empty($found) &amp;&amp; isset($found[$this-&gt;name['id')) {
			$this-&gt;id = $this-&gt;data[$this-&gt;name['id' = $found[$this-&gt;name['id';
			<strong>$this-&gt;__exists = true;</strong>
		}
	}
	return parent::beforeSave();
}</pre>
</blockquote>
<p>That&#8217;s been confusing me a bit recently &#8211; setting the ID of a row within beforesave() should change the save to an update, but it was trying to insert with a specified ID and thus, failing&#8230; glad to have a simple solution that makes sense&#8230; hope that helps someone else&#8230;</p>
<p><map name='google_ad_map_380_0676ea2b0e7dda10'>
<area shape='rect' href='http://imageads.googleadservices.com/pagead/imgclick/380?pos=0' coords='1,2,367,28' />
<area shape='rect' href='http://services.google.com/feedback/abg' coords='384,10,453,23'/></map>
<img usemap='#google_ad_map_380_0676ea2b0e7dda10' border='0' src='http://imageads.googleadservices.com/pagead/ads?format=468x30_aff_img&amp;client=&amp;channel=&amp;output=png&amp;cuid=380&amp;url= http%3A%2F%2Fzeroasterisk.com%2F2010%2F02%2F18%2Fcakephp-beforesave-gotcha-this-__exists%2F' /></p>]]></content:encoded>
			<wfw:commentRss>http://zeroasterisk.com/2010/02/18/cakephp-beforesave-gotcha-this-__exists/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

