<?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 Hints</title>
	<atom:link href="http://cakealot.com/category/cakephp-general/hints/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>Force download a custom view using MediaView</title>
		<link>http://cakealot.com/2009/10/force-download-a-custom-view-using-mediaview/</link>
		<comments>http://cakealot.com/2009/10/force-download-a-custom-view-using-mediaview/#comments</comments>
		<pubDate>Thu, 01 Oct 2009 15:17:50 +0000</pubDate>
		<dc:creator>Kjell</dc:creator>
				<category><![CDATA[CakePHP Hints]]></category>
		<category><![CDATA[controller]]></category>
		<category><![CDATA[exporting]]></category>
		<category><![CDATA[files]]></category>
		<category><![CDATA[force-download]]></category>
		<category><![CDATA[mediaview]]></category>
		<category><![CDATA[mime]]></category>
		<category><![CDATA[render]]></category>
		<category><![CDATA[tmp]]></category>

		<guid isPermaLink="false">http://cakealot.com/?p=468</guid>
		<description><![CDATA[Generally you would need to force-download the file, probably using MediaView. Problem is that it isn't actually a real file for MediaView to work with. Here's a workaround to that.]]></description>
			<content:encoded><![CDATA[<p><strong>Just some quick hint:</strong> Let&#8217;s assume you generate a non-html file using a view, layout and custom extension (sql, xml, json, etc..) and you want to offer the displayed result as a download. &#8211; Generally you would need to force-download the file, probably using MediaView. Problem is that it isn&#8217;t actually a real file for MediaView to work with. </p>
<p>Here&#8217;s a quick workaround to that, using two calls to render() in a single action (comments welcome):</p>
<pre class="sh_php">
public function export() {
  $data = $this->{$this->modelClass}->find('all');
  $this->set(compact('data'));
  $rendered = $this->render();
  $fileext = 'xml'; // optional: $this->params['url']['ext'];
  $filepath = TMP.'rendered'.DS;
  $filename = md5(String::uuid()).'.'.$fileext;
  file_put_contents($filepath.$filename, $rendered); // php5 func
  $this->view = 'Media';
  $params = array(
    'id' => $filename,
    'path' => $filepath,
    'extension' => $fileext,
    'download' => true, // force download
    'name' => low($this->name).'-export' // fancy name
  );
  $this->set($params);
  $this->render(); // init download
}
</pre>
<p>That&#8217;s it. First you render the .ctp using all the fancy helpers, temporarly save the result on your (ram)disk, change the view to Media, reuse almost same variables for the params array and simply call render again. Since we change $this->view in between renders, we can use both view renderer in a row.</p>
<p>DRY: For the extension you could grab the current extension from <code>$this->params['url']['ext']</code> if you used <code>array('ext' => '...')</code> in the <code>HtmlHelper::link()</code> url param. This makes the action even more generic.</p>
]]></content:encoded>
			<wfw:commentRss>http://cakealot.com/2009/10/force-download-a-custom-view-using-mediaview/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>QLColorCode and CakePHP Templates</title>
		<link>http://cakealot.com/2009/07/qlcolorcode-and-cakephp-templates/</link>
		<comments>http://cakealot.com/2009/07/qlcolorcode-and-cakephp-templates/#comments</comments>
		<pubDate>Sat, 18 Jul 2009 07:37:19 +0000</pubDate>
		<dc:creator>Kjell</dc:creator>
				<category><![CDATA[CakePHP Hints]]></category>
		<category><![CDATA[highlight]]></category>
		<category><![CDATA[leopard]]></category>
		<category><![CDATA[mac]]></category>
		<category><![CDATA[osx]]></category>
		<category><![CDATA[quicklook]]></category>
		<category><![CDATA[views]]></category>

		<guid isPermaLink="false">http://cakealot.com/?p=410</guid>
		<description><![CDATA[Mac OSX "Leopard" HowTo: Add CakePHP View extensions to the QLColorCode QuickLook Plugin.]]></description>
			<content:encoded><![CDATA[<p><strong>Mac OSX Hint</strong>: If you use the <a href="http://code.google.com/p/qlcolorcode/" target="_blank">QLColorCode</a> plugin for <strong>QuickLook</strong> to get fancy highlight when viewing a file, you might want to have the same when viewing CakePHP <code>*.ctp</code> and <code>*.thtml</code> files.</p>
<p>It involves a bit of hacking, but not much.<br />
<span id="more-410"></span></p>
<h3>Edit Info.plist</h3>
<p><img src="http://static.cakealot.com/wp-content/uploads/2009/07/filestochange-300x188.png" alt="filestochange" title="filestochange" width="300" height="188" class="alignleft size-medium wp-image-417" />First you need to tell the Plugin about the custom file-types. For that you need to view the package contents of <code>QLColorCode.qlgenerator</code> file.</p>
<p>It should be in <code>/Library/QuickLook</code> or in your home directory <code>~/Library/QuickLook</code>.</p>
<p>Open the &#8220;Contents&#8221; folder and open the <code>Info.plist</code> file. Any decent texteditor will do. Now scroll down to line 270 or so. There you will find the <code>UTTypeConformsTo</code> sections. </p>
<p>I just copied the &#8220;css&#8221; block (from &lt;dict&gt; to &lt;/dict&gt;) and placed it right under it.</p>
<p>- Change the <code>UTTypeDescription</code> to &#8220;PHP File&#8221;.<br />
- Change the <code>UTTypeIdentifier</code> to &#8220;public.php&#8221;. </p>
<p>Then theres a dict-subsection with <code>public.filename-extension</code> as key.<br />
If you followed my steps it should contain &#8220;css&#8221; (if so, replace &#8220;css&#8221; with &#8220;php&#8221;).</p>
<p>- Duplicate the <code>&lt;string&gt;</code> part a couple times and add &#8220;ctp&#8221; and &#8220;thtml&#8221; to the array. </p>
<p>The complete section should then look like this:</p>
<pre class="sh_html"><code>
&lt;dict>
	&lt;key>UTTypeConformsTo&lt;/key>
	&lt;array>
		&lt;string>public.source-code&lt;/string>
	&lt;/array>
	&lt;key>UTTypeDescription&lt;/key>
	&lt;string>PHP File&lt;/string>
	&lt;key>UTTypeIdentifier&lt;/key>
	&lt;string>public.php&lt;/string>
	&lt;key>UTTypeTagSpecification&lt;/key>
	&lt;dict>
		&lt;key>public.filename-extension&lt;/key>
		&lt;array>
			&lt;string>php&lt;/string>
			&lt;string>ctp&lt;/string>
			&lt;string>thtml&lt;/string>
		&lt;/array>
	&lt;/dict>
&lt;/dict>
</code></pre>
<p>Save and close the file. Your QuickLook plugin now recognizes ctp and thtml files. That&#8217;s great, but you need to do one more thing to actually get PHP syntax coloring.</p>
<h3>Edit colorize.sh</h3>
<p>Back in your Finder with the &#8220;package contents&#8221; navigate into the &#8220;Resources&#8221; folder and open the contained &#8220;colorize.sh&#8221;. That&#8217;s a shellscript which does a bit of evaluation before it actually calls the highlight program. </p>
<p>Around line 36 you&#8217;ll find something like a switch-block. Here it&#8217;s called &#8220;case&#8221;&#8230; Anyway. You might recognize the schema here and see that each &#8220;case&#8221; corresponds to a file extension. This is where we jump in and add our <code>.ctp</code> and <code>.thtml</code> sections. </p>
<p>Note that there is no &#8220;break&#8221; statement like in PHP. Each case ends with a double semicolon instead. </p>
<pre class="sh_php"><code>    *.ctp )
        lang=php
        ;;
    *.thtml )
        lang=php
        ;;
</code></pre>
<p>You can put these somewhere in the middle. As said, watch out for the semicolons. Save and close the file. Since you&#8217;ve modified the Info.plist in the first step you might have to let your system know about the changes. Execute the following two commands on your Terminal/shell to refresh the QuickLook service.</p>
<pre class="sh_php">qlmanage -r
/System/Library/Frameworks/CoreServices.framework/Versions/A/Frameworks/LaunchServices.framework/Versions/A/Support/lsregister -f -v /Library/QuickLook/QLColorCode.qlmanager</pre>
<p>Make sure it&#8217;s pointing to the correct plugin path (at the end of the long command..).<br />
After refreshing QL it should work right away. Go to your &#8220;views&#8221; folder and try it out.</p>
<p><a href="http://static.cakealot.com/wp-content/uploads/2009/07/quicklook-for-ctp.png" class="highslide-image" onclick="return hs.expand(this);" target="_blank" title="opens a new window"><img src="http://static.cakealot.com/wp-content/uploads/2009/07/quicklook-for-ctp-300x235.png" alt="quicklook-for-ctp" title="quicklook-for-ctp" width="300" height="235" class="size-medium wp-image-431" /></a></p>
<p>Enjoy!</p>
]]></content:encoded>
			<wfw:commentRss>http://cakealot.com/2009/07/qlcolorcode-and-cakephp-templates/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Quickie: SecurityComponent, Ajax and input.disabled</title>
		<link>http://cakealot.com/2009/04/quickie-securitycomponent-ajax-and-inputdisabled/</link>
		<comments>http://cakealot.com/2009/04/quickie-securitycomponent-ajax-and-inputdisabled/#comments</comments>
		<pubDate>Fri, 24 Apr 2009 12:59:05 +0000</pubDate>
		<dc:creator>Kjell</dc:creator>
				<category><![CDATA[CakePHP Hints]]></category>
		<category><![CDATA[ajax]]></category>
		<category><![CDATA[blackhole]]></category>
		<category><![CDATA[form]]></category>
		<category><![CDATA[input]]></category>
		<category><![CDATA[Javascript]]></category>
		<category><![CDATA[security]]></category>

		<guid isPermaLink="false">http://cakealot.com/?p=306</guid>
		<description><![CDATA[Watch out for input.disabled = true when using SecurityComponent. A short story about what went wrong and why, plus a sidenote about SecurityComponent and Ajax.]]></description>
			<content:encoded><![CDATA[<p><strong>Don&#8217;t mess wit teh formz.</strong></p>
<p>I thought it&#8217;s cool to disable a field simply by using the <code>disabled</code> Element property. Being distracted by other parts in the app i just applied it and left it alone &#8211; &#8220;How can this be bad, right?&#8221;. After some more time i did test the form again and found myself being blackholed all of a sudden &#8211; whaaa? &#8211; Took me some time to find out what the problem was (grrr).</p>
<h3>Here&#8217;s the reason</h3>
<blockquote><p>If you disable a field using Javascript, the field name and its value never reaches the server. This of course invalidates the Security Token and thus leads to a blackhole.</p></blockquote>
<p>I must admit that this was kinda stupid but that&#8217;s how things can go after some hours in CakePHP-land. :-)</p>
<p>Some &#8220;validation scripts&#8221; also apply disable. So look out for that too. Just don&#8217;t mess around with the fields (..much), and avoid prefilled values using <code>array('value' => 'bla')</code> if plan to change the value with Javascript. A good workaround to that is to set the &#8220;initial value&#8221; on page load using Javascript. SecurityComponent and Ajax then shouldn&#8217;t be a problem at all.</p>
]]></content:encoded>
			<wfw:commentRss>http://cakealot.com/2009/04/quickie-securitycomponent-ajax-and-inputdisabled/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Did you know? image() can has url!</title>
		<link>http://cakealot.com/2009/02/did-you-know_image-can-has-url/</link>
		<comments>http://cakealot.com/2009/02/did-you-know_image-can-has-url/#comments</comments>
		<pubDate>Wed, 04 Feb 2009 06:50:44 +0000</pubDate>
		<dc:creator>Kjell</dc:creator>
				<category><![CDATA[CakePHP Hints]]></category>
		<category><![CDATA[HtmlHelper]]></category>
		<category><![CDATA[image]]></category>
		<category><![CDATA[link]]></category>
		<category><![CDATA[options]]></category>
		<category><![CDATA[url]]></category>

		<guid isPermaLink="false">http://cakealot.com/?p=200</guid>
		<description><![CDATA[Here i show a sort of hidden 'url' option that exists within the HtmlHelper::image() method options array. ]]></description>
			<content:encoded><![CDATA[<p>Now this is a quick one here. Most of you know the following code from the default.ctp after you &#8220;just baked&#8221; the app.</p>
<pre class="sh_php">
echo $html->link(
	$html->image('cake.power.gif', array('alt'=> __("CakePHP...", true), 'border'=>"0")),
	'http://www.cakephp.org/',
	array('target'=>'_blank'), null, false
);
</pre>
<p>This displays the small &#8220;CakePHP Power&#8221; badge with a link to their website. Much code for just a image surrounded with a link, eh? </p>
<p>The $html->image() method has a option called &#8216;url&#8217; that automatically does this for you. Only limitation is that you can&#8217;t pass attributes to the link tag. But who really needs &#8220;target=_blank&#8221; in XHTML world anyway?</p>
<p>So here&#8217;s the same thing, only without the clutter:</p>
<pre class="sh_php">
echo $html->image('cake.power.gif', array('url' => 'http://www.cakephp.org', 'alt' => __('CakePHP...', true), 'border' => 0));
</pre>
<p>Tada! There you have it. Of course &#8216;url&#8217; accepts arrays and everything else since it passes the value straight into Helper::url(); </p>
<p>Enjoy</p>
]]></content:encoded>
			<wfw:commentRss>http://cakealot.com/2009/02/did-you-know_image-can-has-url/feed/</wfw:commentRss>
		<slash:comments>2</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>CakePHP Migrations: INTEGER gotchas</title>
		<link>http://cakealot.com/2008/12/cakephp-migrations-integer-gotchas/</link>
		<comments>http://cakealot.com/2008/12/cakephp-migrations-integer-gotchas/#comments</comments>
		<pubDate>Thu, 25 Dec 2008 14:47:46 +0000</pubDate>
		<dc:creator>Kjell</dc:creator>
				<category><![CDATA[CakePHP]]></category>
		<category><![CDATA[CakePHP Hints]]></category>
		<category><![CDATA[database]]></category>
		<category><![CDATA[migrations]]></category>
		<category><![CDATA[shell]]></category>
		<category><![CDATA[yaml]]></category>

		<guid isPermaLink="false">http://cakealot.wordpress.com/?p=9</guid>
		<description><![CDATA[You can basicly set any integer type using YAML, but you need to know what length will be translated to what]]></description>
			<content:encoded><![CDATA[<p>Okay.. today i played around with the wonderful <a href="http://github.com/joelmoss/cakephp-db-migrations">migrations implementation from Joel Moss</a> and found some stuff in regard of typing in YAML.</p>
<p>First of all: It&#8217;s a pain in the ass &#8211; Secondly: It&#8217;s a breeze when it&#8217;s done.</p>
<p>Some beginners, just like me, may have wondered about why your generated schema does not reflect the exact layout after doing &#8220;migrate up&#8221; with the exact same file. For now i will get at the integers, because i think they are quiet important to know and i at least couldn&#8217;t find anything about it.</p>
<p>You can basicly set any <strong>integer</strong> type using YAML, but you need to know what <strong>length</strong> will be translated to what. Here is a small chart explaining just that.</p>
<ul>
<li>length: 1 =&gt; tinyint(4)</li>
<li>length: 2 =&gt; smallint(6)</li>
<li>length: 3 =&gt; mediumint(9)</li>
<li>length: 4 =&gt; integer(11)</li>
</ul>
<p>Everything greater than 4 will become bigint(20). If we look at length as a setting for the <em>sub</em>-fieldtype rather than being the real length of the field, this all makes sense. Dont bother trying to set length to 11 .. it wont help.</p>
]]></content:encoded>
			<wfw:commentRss>http://cakealot.com/2008/12/cakephp-migrations-integer-gotchas/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>
