I am mikek's Blog

The personal blog of Mike Karthauser, who you may know from Brightstorm Limited or the internet.

CakePHP virtual Fields

Something pretty useful in the CakePHP right now is VirtualFields.

A virtual field is a way to combine several fields to make a new ‘virtual’ one that exists when you read from the model. So with ‘firstname’ and ‘surname’ you can make a ‘full_name’ field which will allow you to use this within a find(‘list’) – a notoriously tricky task otherwise.

To do this on the User Model for example, just add the following to your app/models/user.php.

var $virtualFields = array(
'full_name' => 'CONCAT(User.firstname, " ", User.surname)'
);

Simple and powerful.

Available in CakePHP 1.3.*

Filed under: code snippets, , ,

New CakePHP has landed – 1.3.0-beta released

Today i wake to find the fantastic news that CakePHP 1.3 is now in beta. You can read more about that via cakephp.org

The bit that particularly tickles me about this new is the inclusion of something i’ve wanted since i first heard about it, virtual fields.

Virtual fields are a new feature in the Model for CakePHP 1.3. Virtual fields allow you to create arbitrary SQL expressions and assign them as fields in a Model. These fields cannot be saved, but will be treated like other model fields for read operations. They will be indexed under the model’s key alongside other model fields.

For anyone who has ever had to stitch together $firstname.’ ‘.$lastname to make $name fields and wondered why you couldn’t get that in a $this->Model->find(‘list’), this is for you.

Once again, many thanks to the fine work from CakePHP developers and contributors, especially Mark Story.

Top new year news indeed and looking forward to the full 1.3. stable release.

Filed under: development, , ,

Bake path for cakephp

A super easy way to bake cakephp from anywhere in commandline is to add / append your path in .profile/ .bash_login etc.

Add /cake/cake_1.2.3.8166/cake/console or equiv location of your console directory.

ala:

export PATH=/cake/cake_1.2.2.8120/cake/console:/usr/local/bin:/opt/local/bin:/opt/local/apache2/bin:/opt/local/lib/postgresql82/bin:$PATH

Then run source .bash_login etc to enable it or log out and in again.

Filed under: Uncategorized, , ,

Named arguments vs cakephp sessions

Trying to capture named arguments and store them into a session in CakePHP was returning some unexpected results when I subsequently printed the session data.

My code simplified is as follows:

if( !empty($this->data)){
$params = $this->Session->read('Media.params');
pr($params);
} else {
$named = $this->params['named'];
$this->Session->write('Media.params',$named);
}

If i feed it a url as such http://testApp.com/controller/action/named1:value1/named:value2

I would expect the pr($params) to return an array as such:

array(
[named1]=>[value1]
[named2]=>[value2]
)

Except it was only returning:

array(
[named1]=>[value1]
)

Funny eh?

After a bit of fiddling, I managed to get it to work by adding an additional slash as such:
http://testApp.com/controller/action/named1:value1/named:value2/
which gave me the full named argument array.

For more about Sessions in CakePHP, visit http://book.cakephp.org/view/173/Sessions
and http://justkez.com/understanding-cakephp-sessions.

There are a few issues affecting this still, specifically if i add a namedArg to the end of a
$html->link() .

I have compensated for that as follows:
echo $html->link(__('Upload Media', true), array('controller'=> 'media', 'action'=>'upload','/article_id:'.$article['Article']['id']."/ "));
Note hacky ."/ "

When i establish whats up, I’ll post a reply here. If you know more than me, please leave a comment below.

Follow up:

The correct way to do this would be:

echo $html->link(__('Upload Media', true), array('controller'=> 'media', 'action'=>'upload','article_id:'=>$article['Article']['id']));

(thanks jon)

Filed under: development, , ,

Final cakephp 1.2 now here

The much anticipated cakephp 1.2 development framework is now stable.

The history of the CakePHP grows stronger. December 25, 2008 will be remembered as one of the most important points in this history. After exactly 2 yrs from the first development release, we can happily say we have the most stable and powerful web framework available. Please enjoy our big present to you, CakePHP 1.2 stable [1].

See the release notes here

Fantastic news.

Filed under: development, , , ,

Slashdot Architecture – How the Old Man of the Internet Learned to Scale

Slashdot effect: overwhelming unprepared sites with an avalanche of reader’s clicks after being mentioned on Slashdot. Sure, we now have the “Digg effect” and other hot new stars, but Slashdot was the original. And like many stars from generations past, Slashdot plays the elder statesman’s role with with class, dignity, and restraint. Yet with millions and millions of users Slashdot is still box office gold and more than keeps up with the young’ins. And with age comes the wisdom of learning how to handle all those users. Just how does Slashdot scale and what can you learn by going old school?

Read more at High Scaleability

Filed under: development, , ,

what's hot

Flickr Photos

Sponsor Mike run the Bath Half marathon