<?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>cakealot &#187; CakePHP Snippets</title>
	<atom:link href="http://cakealot.com/category/cakephp-general/snippets/feed/" rel="self" type="application/rss+xml" />
	<link>http://cakealot.com</link>
	<description>cakephp and stuff</description>
	<lastBuildDate>Sat, 13 Mar 2010 23:20:57 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.5</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Adding &#8220;empty&#8221; to list-find, for the lazy</title>
		<link>http://cakealot.com/2009/04/adding-empty-to-list-find/</link>
		<comments>http://cakealot.com/2009/04/adding-empty-to-list-find/#comments</comments>
		<pubDate>Mon, 13 Apr 2009 17:32:04 +0000</pubDate>
		<dc:creator>Kjell</dc:creator>
				<category><![CDATA[CakePHP Snippets]]></category>
		<category><![CDATA[AppModel]]></category>
		<category><![CDATA[empty]]></category>
		<category><![CDATA[find]]></category>
		<category><![CDATA[form]]></category>
		<category><![CDATA[list]]></category>
		<category><![CDATA[list-find]]></category>
		<category><![CDATA[Model]]></category>
		<category><![CDATA[select]]></category>

		<guid isPermaLink="false">http://cakealot.com/?p=317</guid>
		<description><![CDATA[Currently i use the scaffold views alot to bake a simple admin interface. What annoyed me was that 'empty' is not set on select inputs. I could of course go through all add/edit views and add it, but i thought i rather modify the result to include a empty entry.]]></description>
			<content:encoded><![CDATA[<p>Currently i use the scaffold views alot to bake a simple admin interface. What annoyed me was that &#8216;empty&#8217; is not set on select inputs. I could of course go through all add/edit views and add it, but i thought i rather modify the result to include a empty entry.</p>
<h3>Always add a &#8220;empty&#8221; entry to the result</h3>
<pre class="sh_php">
function find($conditions = null, $fields = array(), $order = null, $recursive = null) {
	if (is_array($conditions)) {
		return parent::find($conditions, $fields, $order, $recursive); // old interface
	} else {
		switch ($conditions) {
			case 'list':
				$result = parent::find($conditions, $fields, $order, $recursive);
				$return = $result; // default

				if (!empty($result)) {
					$return = aa('', __("-- please select --", true));
					foreach ($result as $id => $name)
						$return[$id] = $name;
				}

				return $return;
			break;

			default:
				return parent::find($conditions, $fields, $order, $recursive);
			break;
		}
	}
}
</pre>
<p>Okay, that works and i don&#8217;t have to add &#8216;empty&#8217; everywhere. I modified the method a little further (not actually using it, but maybe someone likes it..) for you to grab. It will still require that you modify all files, but this way you have more control.</p>
<h3>Adding &#8220;empty&#8221; to the find() options array</h3>
<pre class="sh_php">
function find($conditions = null, $fields = array(), $order = null, $recursive = null) {

	if (is_array($conditions)) {
		return parent::find($conditions, $fields, $order, $recursive); // old interface
	} else {
		switch ($conditions) {
			case 'list':
				$result = parent::find($conditions, $fields, $order, $recursive);
				$return = $result; // default

				if (!empty($result) &#038;&#038; isset($fields['empty']) &#038;&#038; $fields['empty'] != false) {
					$return = aa('', $fields['empty']); // begin with whatever is in 'empty'
					foreach ($result as $id => $name)
						$return[$id] = $name;
				}

				return $return;
			break;

			default:
				return parent::find($conditions, $fields, $order, $recursive);
			break;
		}
	}
}
</pre>
<p>Everything above goes to <code>app_model.php</code> of course. With the last snippet you are able to move the &#8220;empty&#8221; option to the controller and go like this:</p>
<pre class="sh_php">
$categories = $this->Category->find('list', array('empty' => __("-- please select --", true)));
</pre>
<p>Enjoy &#038; Happy Easter!</p>
]]></content:encoded>
			<wfw:commentRss>http://cakealot.com/2009/04/adding-empty-to-list-find/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Probably watching too many RoR demos lately</title>
		<link>http://cakealot.com/2009/03/probably-watching-too-many-ror-demos-lately/</link>
		<comments>http://cakealot.com/2009/03/probably-watching-too-many-ror-demos-lately/#comments</comments>
		<pubDate>Tue, 17 Mar 2009 12:12:24 +0000</pubDate>
		<dc:creator>Kjell</dc:creator>
				<category><![CDATA[CakePHP]]></category>
		<category><![CDATA[CakePHP Snippets]]></category>
		<category><![CDATA[ActiveRecord]]></category>
		<category><![CDATA[Behaviors]]></category>
		<category><![CDATA[Experiments]]></category>
		<category><![CDATA[funstuff]]></category>
		<category><![CDATA[Model]]></category>

		<guid isPermaLink="false">http://cakealot.com/?p=310</guid>
		<description><![CDATA[Somewhat ActivRecord'ish for CakePHP.. ]]></description>
			<content:encoded><![CDATA[<p>Not sure why, but this ActiveRecord thing appealed to me and so i wrote a short behavior (nothing to release, really..) that kinda emulates RoR a little more&#8230; However, i think this is something for the bin. Or does anyone see a need or benefit of something like this:</p>
<pre class="sh_php">
$this->User->find('first');
pr ($this->User->record); // has the full record

$this->User->record->username = 'BlaBlubb';
$this->User->record->email = 'bla@blubb.com';
$this->User->save(); // saves full record 

$this->User->find('first');
pr ($this->User->record); // jup, that worked.
</pre>
<p>Useful? hmm.. maybe. It was fun to explore though. If you are interested in the Behavior, i&#8217;ve <a href="http://gist.github.com/80504">put it into a gist</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://cakealot.com/2009/03/probably-watching-too-many-ror-demos-lately/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Model::save blacklist &#8211; in reply to teknoid</title>
		<link>http://cakealot.com/2009/03/modelsave-blacklist-in-reply-to-teknoid/</link>
		<comments>http://cakealot.com/2009/03/modelsave-blacklist-in-reply-to-teknoid/#comments</comments>
		<pubDate>Fri, 13 Mar 2009 23:11:31 +0000</pubDate>
		<dc:creator>Kjell</dc:creator>
				<category><![CDATA[CakePHP Snippets]]></category>
		<category><![CDATA[AppModel]]></category>
		<category><![CDATA[blackList]]></category>
		<category><![CDATA[save]]></category>
		<category><![CDATA[whiteList]]></category>

		<guid isPermaLink="false">http://cakealot.com/?p=292</guid>
		<description><![CDATA[Blacklist your model fields for save() - but within AppModel.]]></description>
			<content:encoded><![CDATA[<p>It&#8217;s not that i did not know what else to post, but here&#8217;s a more general approach for those who like using blackList over whiteList when tighten up save() calls.</p>
<p>This post is ment as follow up to <a href="http://teknoid.wordpress.com/2009/03/11/blacklist-your-model-fields-for-save/">the one teknoid posted</a>. Instead of doing the array_diff stuff in your controller, you could do it in the model (respectively, all models). So here it goes. I made the $blackList part optional, but feel free to change that. </p>
<pre class="sh_php">
class AppModel extends Model {

	function save($data = null, $validate = true, $fieldList = array(), $blackList = false) {
		if (!empty($fieldList) &#038;&#038; $blackList) {
			$fieldList = array_diff(array_keys($this->schema()), $fieldList);
		}
		return parent::save($data, $validate, $fieldList);
	}
}
</pre>
<p>In your controller you can then use blacklisting like so:</p>
<pre class="sh_php">
function some_action() {
	if ($this->Whatever->save($this->data, true, a('user_id'), true)) {
		//...
}
</pre>
<p>I also had ideas to allow both methods by using blacklist/whitelist as array keys of fieldList, but then again.. lets not overdo this. :)</p>
]]></content:encoded>
			<wfw:commentRss>http://cakealot.com/2009/03/modelsave-blacklist-in-reply-to-teknoid/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Fancy URLs with Inflector::slug</title>
		<link>http://cakealot.com/2009/01/fancy-urls-with-inflectorslug/</link>
		<comments>http://cakealot.com/2009/01/fancy-urls-with-inflectorslug/#comments</comments>
		<pubDate>Sun, 25 Jan 2009 21:01:49 +0000</pubDate>
		<dc:creator>Kjell</dc:creator>
				<category><![CDATA[CakePHP Hints]]></category>
		<category><![CDATA[CakePHP Snippets]]></category>
		<category><![CDATA[AppModel]]></category>
		<category><![CDATA[beforeSave]]></category>
		<category><![CDATA[Callbacks]]></category>
		<category><![CDATA[Inflector]]></category>
		<category><![CDATA[Slugs]]></category>
		<category><![CDATA[URLs]]></category>

		<guid isPermaLink="false">http://cakealot.com/?p=113</guid>
		<description><![CDATA[How to use core functions to create slugs and auto-update them on Model::save ]]></description>
			<content:encoded><![CDATA[<p>Motivated by my recent tweet i thought i share my method of replacing the old SluggableBehavior with core functions.</p>
<p>Sometimes we have more than one model that uses slugs. For example: Tags, Posts, Categories. That&#8217;s why i add the following to <code>AppModel</code> which allows me to call it on any model.</p>
<pre class="sh_php">function slugify($text) {
	return low(Inflector::slug($text, '-'));
}</pre>
<p>But because i am lazy i just use one of the model callbacks to automate the &#8220;slugification(tm)&#8221;. </p>
<pre class="sh_php">
function beforeSave() {
	if ($this->hasField('slug') &#038;&#038; isset($this->data[$this->alias][$this->displayField])) {
		$this->data[$this->alias]['slug'] = $this->slugify($this->data[$this->alias][$this->displayField]);
	}
}</pre>
<p>So on any model that has a &#8220;slug&#8221; field in the associated table we always slugify the displayField (usually &#8220;name&#8221;) and keep it fresh. Dead simple.</p>
]]></content:encoded>
			<wfw:commentRss>http://cakealot.com/2009/01/fancy-urls-with-inflectorslug/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Did you know? String::insert is nice for translation!</title>
		<link>http://cakealot.com/2009/01/did-you-know-stringinsert-is-nice-for-translation/</link>
		<comments>http://cakealot.com/2009/01/did-you-know-stringinsert-is-nice-for-translation/#comments</comments>
		<pubDate>Thu, 08 Jan 2009 03:37:03 +0000</pubDate>
		<dc:creator>Kjell</dc:creator>
				<category><![CDATA[CakePHP Snippets]]></category>
		<category><![CDATA[i18n]]></category>
		<category><![CDATA[l10n]]></category>
		<category><![CDATA[sprintf]]></category>
		<category><![CDATA[string]]></category>

		<guid isPermaLink="false">http://cakealot.com/?p=106</guid>
		<description><![CDATA[Why to use String::insert over sprintf, and how this helps in multilingual apps.]]></description>
			<content:encoded><![CDATA[<p>You probably replace your language strings like so:</p>
<pre class="sh_php">
e(sprintf( __("Please %s or %s to write a comment", true),
    $html->link(__("login", true), array('controller' => 'users', 'action' => 'login')),
    $html->link(__("register", true), array('controller' => 'users', 'action' => 'register'))
));
</pre>
<p>There are two problems with it:</p>
<ol>
<li>You may have to explain alot when someone translates the app. %s is meaningless.</li>
<li>You probably have to reorder the sprintf arguments some day (login,register<>register,login).</li>
</ol>
<h3>String::insert to the rescue!</h3>
<p>Let&#8217;s rewrite the above:</p>
<pre class="sh_php">
e(String::insert( __("Please :login or :register to write a comment", true),
array(
    'login' => $html->link(__("login", true), array('controller' => 'users', 'action' => 'login')),
    'register' => $html->link(__("register", true), array('controller' => 'users', 'action' => 'register'))
)));
</pre>
<p>Now this looks alot nicer, don&#8217;t you agree? Anyone translating your app will know what is inserted where, and it  even allows to reorder the link replacements within the translation msgstr if needed. And the best part of it: we don&#8217;t care!</p>
]]></content:encoded>
			<wfw:commentRss>http://cakealot.com/2009/01/did-you-know-stringinsert-is-nice-for-translation/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>How to use THIS in THAT and then some</title>
		<link>http://cakealot.com/2009/01/how-to-use-this-in-that-and-then-some/</link>
		<comments>http://cakealot.com/2009/01/how-to-use-this-in-that-and-then-some/#comments</comments>
		<pubDate>Fri, 02 Jan 2009 13:11:25 +0000</pubDate>
		<dc:creator>Kjell</dc:creator>
				<category><![CDATA[CakePHP Snippets]]></category>
		<category><![CDATA[ClassRegistry]]></category>
		<category><![CDATA[components]]></category>
		<category><![CDATA[controllers]]></category>
		<category><![CDATA[extend]]></category>
		<category><![CDATA[helpers]]></category>
		<category><![CDATA[import]]></category>

		<guid isPermaLink="false">http://cakealot.com/?p=59</guid>
		<description><![CDATA[How to use a model in a component, How to extend a core helper, How to extend a core component, How to add a foreign model to a controller, How to load data from a foreign model in any controller, How to use a model in a helper]]></description>
			<content:encoded><![CDATA[<p>Some common questions on IRC:</p>
<h3>How to use a model in a component</h3>
<pre class="sh_php">
$model = ClassRegistry::init('ModelName');
$result = $model->find('...');
</pre>
<h3>How to extend a core helper</h3>
<pre class="sh_php">
App::import('Helper', 'Text');
class AppTextHelper extends TextHelper {
  // ... your code
}</pre>
<h3>How to extend a core component</h3>
<pre class="sh_php">
App::import('Component', 'Auth');
class AppAuthComponent extends AuthComponent {
  // ... your code
}</pre>
<h3>How to get just the url from a link (href) in a view</h3>
<pre class="sh_php">
$html->url(array('controller' => 'users', 'action' => 'edit'));
</pre>
<h3>How to transform those array formatted urls into a string</h3>
<pre class="sh_php">
Router::url(array('controller' => 'users', 'action' => 'edit'));
</pre>
<h3>How to add a foreign model to a controller</h3>
<pre class="sh_php">
class UsersController extends AppController {
  var $uses = array('User', 'Page');
  function something() {
     $result = $this->Page->find(...);
  }
}</pre>
<h3>How to load data from a foreign model in any controller</h3>
<pre class="sh_php">
class UsersController extends AppController {
  function something() {
     $result = ClassRegistry::init('Page')->find(...);
  }
}</pre>
<h3>How to use a helper in a component (or controller)</h3>
<p>Don&#8217;t &#8230;</p>
<h3>How to use a model in a helper</h3>
<p>Don&#8217;t &#8230;</p>
]]></content:encoded>
			<wfw:commentRss>http://cakealot.com/2009/01/how-to-use-this-in-that-and-then-some/feed/</wfw:commentRss>
		<slash:comments>9</slash:comments>
		</item>
		<item>
		<title>Easy CakePHP deployment: Environment &amp; Database Config</title>
		<link>http://cakealot.com/2008/12/environment-and-database-config-easy-cakephp-deployment/</link>
		<comments>http://cakealot.com/2008/12/environment-and-database-config-easy-cakephp-deployment/#comments</comments>
		<pubDate>Sat, 27 Dec 2008 20:31:17 +0000</pubDate>
		<dc:creator>Kjell</dc:creator>
				<category><![CDATA[CakePHP]]></category>
		<category><![CDATA[CakePHP Snippets]]></category>
		<category><![CDATA[bootstrap]]></category>
		<category><![CDATA[config]]></category>
		<category><![CDATA[configure]]></category>
		<category><![CDATA[database]]></category>
		<category><![CDATA[environment]]></category>

		<guid isPermaLink="false">http://cakealot.com/?p=43</guid>
		<description><![CDATA[How to use the Environment class setup in DATABASE_CONFIG class to switch databases between environments]]></description>
			<content:encoded><![CDATA[<p>I just digged <a href="http://rafaelbandeira3.wordpress.com/2008/12/05/handling-multiple-enviroments-on-cakephp/">Rafael Bandeiras Environment class</a> which is supposed to help handling multiple enviroments. It really is a nice piece of code and i don&#8217;t find it overkill either. </p>
<p>After adding it to my app i thought how i could use my setups to change the database with ease. I then remembered reading an <a href="http://bakery.cakephp.org/articles/view/easy-peasy-database-config">bakery article from Joel Moss</a> on how one could use the object constructor in the DATABASE_CONFIG class. Well.. both techniques go well together!</p>
<p>Environment writes the currently loaded environment name to the config, so we just use that info to switch the database config too.</p>
<pre class="sh_php">
class DATABASE_CONFIG {
	public $default = array(
		'host' 		=> 'localhost',
		'driver' 	=> 'mysql',
		'persistent' 	=> false,
		'login' 	=> 'DEFAULT_USERNAME',
		'password' 	=> 'DEFAULT_PASSWORD',
		'database' 	=> 'DEFAULT_DATABASE',
	);

	public $development = array(
		'login' 	=> 'dev_username',
		'password' 	=> 'dev_password',
		'database' 	=> 'dev_db',
	);

	public $production = array(
		'login' 	=> 'live_username',
		'password' 	=> 'live_password',
		'database' 	=> 'live_db',
	);

    function __construct() { // the magic
    	$environment = Configure::read('Environment.name');
    	if (($environment) &#038;&#038; isset($this->{$environment})) {
    		$this->default = array_merge($this->default, $this->{$environment});
    	}
    }
}
</pre>
<p>Okay.. the above assumes we have setup two environments called &#8220;production&#8221; and &#8220;development&#8221;. What an environment does for you is best explained on the <a href="http://rafaelbandeira3.wordpress.com/2008/12/05/handling-multiple-enviroments-on-cakephp/">original post</a>. I will show you a example setup that&#8217;s in the bootstrap.php.</p>
<pre class="sh_php">
	Environment::configure(
	'development',
		array('server' => 'localhost'),
		array(
			'debug' => 2,
			'security' => 'low'
		)
	);
	Environment::configure(
	'production',
		true,
		array(
			'debug' => 0,
			'security' => 'high'
		)
	);
</pre>
<p>Now whenever the environment changes the custom database config array by the <strong>same name</strong> is merged with DATABASE_CONFIG::$default. </p>
]]></content:encoded>
			<wfw:commentRss>http://cakealot.com/2008/12/environment-and-database-config-easy-cakephp-deployment/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
	</channel>
</rss>
