<?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>Trent Richardson &#187; CakePHP</title> <atom:link href="http://trentrichardson.com/category/cakephp/feed/" rel="self" type="application/rss+xml" /><link>http://trentrichardson.com</link> <description>practical web design &#38; development</description> <lastBuildDate>Wed, 02 May 2012 17:05:19 +0000</lastBuildDate> <language>en</language> <sy:updatePeriod>hourly</sy:updatePeriod> <sy:updateFrequency>1</sy:updateFrequency> <generator>http://wordpress.org/?v=3.3.2</generator> <item><title>Using Subqueries as Fields in CakePHP</title><link>http://trentrichardson.com/2012/03/19/using-subqueries-as-fields-in-cakephp/</link> <comments>http://trentrichardson.com/2012/03/19/using-subqueries-as-fields-in-cakephp/#comments</comments> <pubDate>Mon, 19 Mar 2012 11:02:36 +0000</pubDate> <dc:creator>trent</dc:creator> <category><![CDATA[CakePHP]]></category> <category><![CDATA[Database]]></category> <category><![CDATA[PHP]]></category> <category><![CDATA[model]]></category> <category><![CDATA[query]]></category> <category><![CDATA[sql]]></category> <category><![CDATA[subquery]]></category><guid
isPermaLink="false">http://trentrichardson.com/?p=1150</guid> <description><![CDATA[From time to time when querying the database, you have to break the mold. CakePHP gives many options when it comes to complex conditions, but sometimes you need to push the boundaries. Recently I needed to return a subquery as a field name. It wasn&#8217;t a high demand query, so I wasn&#8217;t too concerned with [...]
Related posts:<ol><li><a
href='http://trentrichardson.com/2010/03/05/handling-timezones-in-cakephp/' rel='bookmark' title='Handling TimeZones in CakePHP'>Handling TimeZones in CakePHP</a></li><li><a
href='http://trentrichardson.com/2010/01/31/cakephp-postgresql-and-regex/' rel='bookmark' title='CakePHP, PostgreSQL, and Regex'>CakePHP, PostgreSQL, and Regex</a></li><li><a
href='http://trentrichardson.com/2010/02/03/adding-postgresql-regex-support-in-cakephp/' rel='bookmark' title='Adding PostgreSQL Regex Support in CakePHP'>Adding PostgreSQL Regex Support in CakePHP</a></li></ol>]]></description> <content:encoded><![CDATA[<p>From time to time when querying the database, you have to break the mold.  CakePHP gives many options when it comes to complex conditions, but sometimes you need to push the boundaries.  Recently I needed to return a subquery as a field name.  It wasn&#8217;t a high demand query, so I wasn&#8217;t too concerned with performance.  Here was my first approach to &#8220;hack&#8221; my way through Cake&#8217;s setup:</p><pre><code class="php">
$paging = array(
	'fields'=>array('User.id', 'User.email', '((select max(ul2.login) from "user_logins" as "ul2" where "ul2"."user_id"="User"."id") >= \''.$two_mo_ago.'\') as "User__alive"'),
	'order'=>'User.email asc',
	'contain'=>array(),
	'limit'=>20
);
</code></pre><p>I&#8217;ve changed my query a bit for simplicity, but the basic concept is to get a true/false if the user has logged in within the past two months.  This works for some databases, but I use sqlite for development, and it complained with &#8220;User__alive&#8221;.  To properly group the fields cake uses the Model__field naming, but oddly Sqlite didn&#8217;t like me using this.  So how can you get Cake to handle all this for you?  Simple, use virtualFields with your model.  The solution ended up being cleaner than the example above.  Here&#8217;s what I ended up with:</p><pre><code class="php">
$this->User->virtualFields['alive'] = '((select max(ul2.login) from "user_logins" "ul2" where "ul2"."user_id"="User"."id") >= \''.$two_mo_ago.'\')';

$paging = array(
	'fields'=>array('User.id', 'User.email', 'User.alive'),
	'order'=>'User.email asc',
	'contain'=>array(),
	'limit'=>20
);
</code></pre><p>Cake knows how to handle piecing the fields together across databases, so cross platform issue is solved, plus cake&#8217;s pagination knows what to do when you sort by this field.  Of course you will want to assign these virtualFields in your model so they&#8217;re available everywhere, but for simplicity I showed using/assigning it in the controller.<p>Adding another level of Cake goodness you may want to consider using the model&#8217;s <a
href="http://book.cakephp.org/2.0/en/models/retrieving-your-data.html#sub-queries" title="Cakephp BuildStatement for Subqueries" target="_blank">buildStatement to generate subquery strings</a>.  This will almost eliminate the need to write sql statements at all, thus let Cake do more cross database sql generation for you.  I may tackle this in another post.  Enjoy!</p><p>Related posts:<ol><li><a
href='http://trentrichardson.com/2010/03/05/handling-timezones-in-cakephp/' rel='bookmark' title='Handling TimeZones in CakePHP'>Handling TimeZones in CakePHP</a></li><li><a
href='http://trentrichardson.com/2010/01/31/cakephp-postgresql-and-regex/' rel='bookmark' title='CakePHP, PostgreSQL, and Regex'>CakePHP, PostgreSQL, and Regex</a></li><li><a
href='http://trentrichardson.com/2010/02/03/adding-postgresql-regex-support-in-cakephp/' rel='bookmark' title='Adding PostgreSQL Regex Support in CakePHP'>Adding PostgreSQL Regex Support in CakePHP</a></li></ol></p>]]></content:encoded> <wfw:commentRss>http://trentrichardson.com/2012/03/19/using-subqueries-as-fields-in-cakephp/feed/</wfw:commentRss> <slash:comments>0</slash:comments> </item> <item><title>Install PHP Pear Spreadsheet in Ubuntu and Mint</title><link>http://trentrichardson.com/2012/02/22/install-php-pear-spreadsheet-in-ubuntu-and-mint/</link> <comments>http://trentrichardson.com/2012/02/22/install-php-pear-spreadsheet-in-ubuntu-and-mint/#comments</comments> <pubDate>Wed, 22 Feb 2012 11:50:10 +0000</pubDate> <dc:creator>trent</dc:creator> <category><![CDATA[CakePHP]]></category> <category><![CDATA[Linux]]></category> <category><![CDATA[PHP]]></category> <category><![CDATA[Programming]]></category> <category><![CDATA[excel]]></category> <category><![CDATA[ods]]></category> <category><![CDATA[pear]]></category> <category><![CDATA[spreadsheet]]></category><guid
isPermaLink="false">http://trentrichardson.com/?p=1121</guid> <description><![CDATA[If you&#8217;ve ever had to work with spreadsheets on a linux system, you may have stumbled across the PEAR Excel Spreadsheet Writer. Its a quite useful too, but installing it on your system may be a little misleading. Here&#8217;s the quick rundown: # install php-pear sudo apt-get install php-pear # install PEAR OLE sudo pear [...]
Related posts:<ol><li><a
href='http://trentrichardson.com/2010/02/08/installing-memcached-on-ubuntu-for-php/' rel='bookmark' title='Installing Memcached on Ubuntu for PHP'>Installing Memcached on Ubuntu for PHP</a></li><li><a
href='http://trentrichardson.com/2008/01/08/install-safari-on-ubuntu/' rel='bookmark' title='Install Safari on Ubuntu'>Install Safari on Ubuntu</a></li><li><a
href='http://trentrichardson.com/2008/04/21/localhost-subdomains-on-ubuntu/' rel='bookmark' title='Localhost Subdomains on Ubuntu'>Localhost Subdomains on Ubuntu</a></li></ol>]]></description> <content:encoded><![CDATA[<p>If you&#8217;ve ever had to work with spreadsheets on a linux system, you may have stumbled across the <a
href="http://pear.php.net/Spreadsheet_Excel_Writer" title="PEAR Spreadsheet Writer" target="_blank">PEAR Excel Spreadsheet Writer</a>. Its a quite useful too, but installing it on your system may be a little misleading.  Here&#8217;s the quick rundown:</p><pre class="shell"><code>
# install php-pear
sudo apt-get install php-pear

# install PEAR OLE
sudo pear install OLE-1.0.0RC2

# install Spreadsheet_Excel_Writer
sudo pear install Spreadsheet_Excel_Writer-0.9.2
</code></pre><p>Not much to it, but just getting around the prereq of OLE. Now you should be able to create and edit Excel spreadsheets on linux.  Of course if you have complete flexibility in requirements for your app, you would generate ODS, but I&#8217;ll leave that for a future topic ;) Unfortunately the corporate world is quite attached to .xls and xlsx files. Anyway.. Enjoy!</p><p>Related posts:<ol><li><a
href='http://trentrichardson.com/2010/02/08/installing-memcached-on-ubuntu-for-php/' rel='bookmark' title='Installing Memcached on Ubuntu for PHP'>Installing Memcached on Ubuntu for PHP</a></li><li><a
href='http://trentrichardson.com/2008/01/08/install-safari-on-ubuntu/' rel='bookmark' title='Install Safari on Ubuntu'>Install Safari on Ubuntu</a></li><li><a
href='http://trentrichardson.com/2008/04/21/localhost-subdomains-on-ubuntu/' rel='bookmark' title='Localhost Subdomains on Ubuntu'>Localhost Subdomains on Ubuntu</a></li></ol></p>]]></content:encoded> <wfw:commentRss>http://trentrichardson.com/2012/02/22/install-php-pear-spreadsheet-in-ubuntu-and-mint/feed/</wfw:commentRss> <slash:comments>0</slash:comments> </item> <item><title>Handling TimeZones in CakePHP</title><link>http://trentrichardson.com/2010/03/05/handling-timezones-in-cakephp/</link> <comments>http://trentrichardson.com/2010/03/05/handling-timezones-in-cakephp/#comments</comments> <pubDate>Fri, 05 Mar 2010 13:15:02 +0000</pubDate> <dc:creator>trent</dc:creator> <category><![CDATA[CakePHP]]></category> <category><![CDATA[Database]]></category> <category><![CDATA[PHP]]></category> <category><![CDATA[Programming]]></category> <category><![CDATA[convert]]></category> <category><![CDATA[date]]></category> <category><![CDATA[datetime]]></category> <category><![CDATA[time]]></category> <category><![CDATA[timezone]]></category> <category><![CDATA[user]]></category><guid
isPermaLink="false">http://trentrichardson.com/?p=526</guid> <description><![CDATA[The web is a global resource for anything, anywhere, any timezone. But often times we are careless in consideration for people in other areas of the world. The different timezones offset people&#8217;s schedules from one area to the next. When handling user profiles (or anything for that matter) where date and time is involved we [...]
Related posts:<ol><li><a
href='http://trentrichardson.com/2012/03/19/using-subqueries-as-fields-in-cakephp/' rel='bookmark' title='Using Subqueries as Fields in CakePHP'>Using Subqueries as Fields in CakePHP</a></li><li><a
href='http://trentrichardson.com/2009/03/31/handling-php-form-arrays-with-jquery/' rel='bookmark' title='Handling PHP Form Arrays with jQuery'>Handling PHP Form Arrays with jQuery</a></li><li><a
href='http://trentrichardson.com/2011/12/02/discussion-how-asynchronous-should-we-get/' rel='bookmark' title='Discussion &#8211; How Asynchronous Should We Get'>Discussion &#8211; How Asynchronous Should We Get</a></li></ol>]]></description> <content:encoded><![CDATA[<p><img
src="http://trentrichardson.com/wp-content/uploads/2010/03/timezone.png" alt="Timezones with CakePHP" title="timezone" width="128" height="128" class="alignright size-full wp-image-530" /></p><p>The web is a global resource for anything, anywhere, any timezone.  But often times we are careless in consideration for people in other areas of the world.  The different timezones offset people&#8217;s schedules from one area to the next.  When handling user profiles (or anything for that matter) where date and time is involved we can do a little magic with our CakePHP app to make things a little more convenient for our users.  How do we handle this you ask?</p><p>First lets say when a user logs in to their profile they can click to edit the profile, selects his/her timezone, and clicks save. Relative to GMT we can calculate offsets of each timezone, where GMT would = 0.  So for instance Eastern Timezone for New York, US would be -5 since its 5 ours BEHIND GMT.  So lets start by generating a dropdown list with cake with all the different timezones, then save this list to the users profile.  If you are using Auth with CakePHP, you will likely have a users table, and we&#8217;ll add a decimal field called &#8220;timezone&#8221; to that table.  Fortunately there is a helper to assist us with this dropdown in the bakery, <a
href="http://bakery.cakephp.org/articles/view/updated-timezone-helper">Timezone Helper</a>.  By taking a look at the source you&#8217;ll notice all the labels associated with an offset &#8220;-5.0&#8243; or &#8220;+3.0&#8243;.  Also look a little closer and you might notice a few have actual decimal (Kabul is +4.5).  So first things first, go ahead and install that Helper as those instructions say.  Add it to your Edit view for users to edit their profile. Once included you just add the drop down to your form with:</p><pre><code class="php">
echo $timezone->select('timezone');
</code></pre><p>Now each user should have a timezone attached to their profile.  Now the trick to timezones is that no matter what the timezone of our server is, we just want to convert it to display to the user in that user&#8217;s preferred timezone.  On the reverse side, if a user enters a datetime, we convert it back to the timezone of the server.</p><p>Lets start our with the user viewing a date from the server, in our view we want to convert it to the user&#8217;s preferred timezone set to their profile (which should now be stored in their session, I&#8217;ll demonstrate with Auth)</p><pre><code class="php">
$time->format('F j, Y g:i:s a', $mydate, null, $session->read('Auth.User.timezone'));
</code></pre><p>Using the Time helper in CakePHP, the format function accepts a fourth parameter which indicates the timezone, which is extremely helpful.  So this is all you need to do to display a time from the server to a user.</p><p>Next lets say the user is about to edit a field from the server in a form.  First we need to select the existing data from the database, we&#8217;ll convert it to the user&#8217;s timezone, and place it in an input field to be edited.  Then once the user saves the data we&#8217;ll simply convert it back to the server&#8217;s timezone.  (There a bunch of &#8220;one liners&#8221; in doing all of these tasks).  So if you have your Time helper in your controller when you get the data from the database, go ahead and convert it to the new timezone just like we did before:</p><pre><code class="php">
$data['Model']['mydate'] = $time->format('Y-n-d H:i:sP', $data['Model']['mydate'], null, $this->Auth->user('timezone'));
</code></pre><p>Now in your view for the form there are a few different scenarios.  1) You use Cake&#8217;s default inputs for editing date times, with the 6 dropdowns, or 2) You use a plain input and use a date/time picker of some sort.  Lets first handle the first scenario, Cakes default 6 input datetime.  The data has been submitted, validated and we&#8217;re about to place it back in the database, we need to convert it back to server timezone.  But wait! the form field is broken down into an array of six elements: year, month, day, hour, minute, meridian.  Well we can create a nice little function that will convert this array into a datetime, and pass it our timezone:</p><pre><code class="php">
function dateTimeArrayToServerTZString($dt, $tz=0){
	$tz = (floatval($tz) >= 0)? '+'. number_format(floatval($tz),2,':','') : number_format(floatval($tz),2,':','');
	$dt['min'] = str_pad($dt['min'], 2, "0", STR_PAD_LEFT);

	$loc = $dt['year'] .'-'. $dt['month'] .'-'. $dt['day'] .' '. $dt['hour'] .':'. $dt['min'] .' '. $dt['meridian'] .' '. $tz;

	$datetime = new DateTime($loc);
	$datetime->setTimezone(new DateTimeZone(date('e', time())));

	return $datetime->format('Y-n-d H:i:sP');
}
</code></pre><p>Now right before I insert my data into the database I just need to call this function on that datetime array:</p><pre><code class="php">
$this->data['Model']['mydate'] = dateTimeArrayToServerTZString($this->data['Model']['mydate'], $this->Auth->user('timezone'));
</code></pre><p>Now $this->data['Model']['mydate'] will be a string, and cake still knows how to handle that just fine.  Now for the second scenerio, a user submitting a string.  This isn&#8217;t too hard at all, we just use our time helper and convert that puppy back to the server time.</p><pre><code class="php">
$this->data['Model']['mydate'] = $time->format('Y-n-d H:i:sP', $this->data['Model']['mydate'], null, -5);
</code></pre><p>Ok, so I&#8217;ve hardcoded in the timezone to -5 (Eastern Timezone), what if I want this to be dynamic to where ever my server is? Well the Time helper has a function which isn&#8217;t mentioned in the book called serverOffset(), which will return a number of the offset.  If you&#8217;re not using the helper and just want to get the timezone of the server its a simple one-liner:</p><pre><code class="php">
date('Z', time())
</code></pre><p>Thats all there is to handling timezones in your cakePHP site, or any site for that matter.  Just keep in mind as you go to convert any times for the user&#8217;s pleasure.  You have any tips or alternatives?</p><p>Related posts:<ol><li><a
href='http://trentrichardson.com/2012/03/19/using-subqueries-as-fields-in-cakephp/' rel='bookmark' title='Using Subqueries as Fields in CakePHP'>Using Subqueries as Fields in CakePHP</a></li><li><a
href='http://trentrichardson.com/2009/03/31/handling-php-form-arrays-with-jquery/' rel='bookmark' title='Handling PHP Form Arrays with jQuery'>Handling PHP Form Arrays with jQuery</a></li><li><a
href='http://trentrichardson.com/2011/12/02/discussion-how-asynchronous-should-we-get/' rel='bookmark' title='Discussion &#8211; How Asynchronous Should We Get'>Discussion &#8211; How Asynchronous Should We Get</a></li></ol></p>]]></content:encoded> <wfw:commentRss>http://trentrichardson.com/2010/03/05/handling-timezones-in-cakephp/feed/</wfw:commentRss> <slash:comments>1</slash:comments> </item> <item><title>Using Cakephp Components in Shells and Tasks</title><link>http://trentrichardson.com/2010/02/15/using-cakephp-components-in-shells-and-tasks/</link> <comments>http://trentrichardson.com/2010/02/15/using-cakephp-components-in-shells-and-tasks/#comments</comments> <pubDate>Mon, 15 Feb 2010 13:04:00 +0000</pubDate> <dc:creator>trent</dc:creator> <category><![CDATA[CakePHP]]></category><guid
isPermaLink="false">http://trentrichardson.com/?p=485</guid> <description><![CDATA[Not sure if cake has a prettier, more efficient way of grabbing components inside of Shells and Tasks with cakephp&#8217;s cli, but here is a way which worked for me. Inside your Task (or Shell) just use the following in a similar manner for your component: class FooTask extends Shell { App::import('Component', 'Bar'); $this->Bar =&#038; [...]
Related posts:<ol><li><a
href='http://trentrichardson.com/2010/02/03/adding-postgresql-regex-support-in-cakephp/' rel='bookmark' title='Adding PostgreSQL Regex Support in CakePHP'>Adding PostgreSQL Regex Support in CakePHP</a></li><li><a
href='http://trentrichardson.com/2012/03/19/using-subqueries-as-fields-in-cakephp/' rel='bookmark' title='Using Subqueries as Fields in CakePHP'>Using Subqueries as Fields in CakePHP</a></li><li><a
href='http://trentrichardson.com/2010/01/31/cakephp-postgresql-and-regex/' rel='bookmark' title='CakePHP, PostgreSQL, and Regex'>CakePHP, PostgreSQL, and Regex</a></li></ol>]]></description> <content:encoded><![CDATA[<p>Not sure if cake has a prettier, more efficient way of grabbing components inside of Shells and Tasks with cakephp&#8217;s cli, but here is a way which worked for me.  Inside your Task (or Shell) just use the following in a similar manner for your component:</p><pre><code class="php">
class FooTask extends Shell {
	App::import('Component', 'Bar');

	$this->Bar =&#038; new BarComponent();

	$resp = $this->Bar->crazy_things();
	$this->out($resp);
}
</code></pre><p>Of course Foo and Bar are just for example.  This works for me.  Anyone know of another way?</p><p>Related posts:<ol><li><a
href='http://trentrichardson.com/2010/02/03/adding-postgresql-regex-support-in-cakephp/' rel='bookmark' title='Adding PostgreSQL Regex Support in CakePHP'>Adding PostgreSQL Regex Support in CakePHP</a></li><li><a
href='http://trentrichardson.com/2012/03/19/using-subqueries-as-fields-in-cakephp/' rel='bookmark' title='Using Subqueries as Fields in CakePHP'>Using Subqueries as Fields in CakePHP</a></li><li><a
href='http://trentrichardson.com/2010/01/31/cakephp-postgresql-and-regex/' rel='bookmark' title='CakePHP, PostgreSQL, and Regex'>CakePHP, PostgreSQL, and Regex</a></li></ol></p>]]></content:encoded> <wfw:commentRss>http://trentrichardson.com/2010/02/15/using-cakephp-components-in-shells-and-tasks/feed/</wfw:commentRss> <slash:comments>1</slash:comments> </item> </channel> </rss>
<!-- Performance optimized by W3 Total Cache. Learn more: http://www.w3-edge.com/wordpress-plugins/

Minified using disk: basic
Page Caching using disk: enhanced
Database Caching 5/41 queries in 0.024 seconds using disk: basic
Object Caching 1219/1239 objects using disk: basic

Served from: trentrichardson.com @ 2012-05-21 05:30:31 -->
