GitHub

CakePHP Custom Find Types

Posted by Matt on Fri, Oct 24 2008


The latest version of this code can be found at http://github.com/mcurry/find

A couple months ago Nate Abele wrote an article for C7Y which talked about the new find syntax in CakePHP 1.2.

CakePHP 1.2 beta introduced a new custom find syntax, where instead of calling methods like findAll(), you call methods like find(“all”). This allows for a great deal more flexibility and code reuse.

Nate further shows how you can make your own find types such as "find('popular')." Ever since I read the article I've been using this practice extensively. I would often times find myself copy and pasting the same find code in each model:

public function find($type, $options = array()) {
  switch ($type) {
    case "custom":
      return $this->__findCustom($options);
    default:
      return parent::find($type, $options);
  }
}

I was able to move this block to my AppModel and make it work for all my models:

httpc://github.com/mcurry/cakephp/raw/8f4cd9833824b80573e0de00cd69db37c87e1464/snippets/app_model_find/app_model.php

How It Works

Let's say want a custom find type which returns the latest 10 comments in your blog. Normally you might do something like this in your controller:

$comments = $this->Comment->find('all', array('conditions' => array('spam' => false),
                                              'order' => array('created' => 'desc'),
                                              'limit' => 10));

If you make a custom find type, your controller code becomes:

$comments = $this->Comment->find('latest);

Then in your Comment Model:

function __findLatest($options) {
  return  $this->find('all', array('conditions' => array('spam' => false),
                                   'order' => array('created' => 'desc'),
                                   'limit' => 10));
}

The new find method in AppModel automatically looks for a method "__findLatest" in your model. If the method exists it is called and the results returned, otherwise the parent find is called, which handles all the core find types (all, first, count...).

As always this code is available in GitHub.

Posted in GitHub | 5 Comments

Getting on the GitHub Bandwagon

Posted by Matt on Mon, Oct 20 2008

I put all the code I've previously posted on the CakePHP Bakery onto my GitHub account. A lot of the code has been updated so that it works on RC3.

Code

Asset Helper
HTML Cache
JavaScript Validation
Layout Switcher Component
Simplepie Component

Working example can be seen at sandbox2.pseudocoder.com
There is some other code, but I haven't checked it yet to see if it'll work with RC3. Use at your own risk.

Posted in GitHub | Leave a comment

Accessing User Sessions From Models (or Anywhere) In CakePHP REVEALED!

Posted by Matt on Mon, Oct 06 2008

The latest version of this code can be found at http://github.com/mcurry/cakephp_static_user/

"How do I access the logged in user in a model?" I've seen this question a bunch of times. I've tried numerous solutions, but never really liking any of them. The guys over at debuggable.com have hinted at a User::get('id') method twice, but never revealed the details.

Early Attempts

This question has shown up on the Google Group. Nate (one of the Cake developers) responded:

There are two right ways of doing this. One is to make the session a formal part of your domain model, i.e. model it. This allows it to interact with other models at the model level.

I think I get what Nate is saying here. Make a Session model without a table, set relationships to this model in any other models that may use it (or do I just call it statically?) and (somehow) set the data from your session to the model in app_controller. Assuming I'm understanding this correctly (there's a good chance I'm not), I wasn't crazy about this approach. How do you get the session data to the model? How do you set up the relationships when one of the models is tableless?

Nate's other approach was to set the session/user as a variable in the model with this code in the app_controller:

  function beforeFilter() {
        $this->{$this->modelClass}->setUserState($this->Auth->user());
   }

I used this approach for awhile, but had issues trying to access the user in cases where the model wasn't the "primary" one. The code above only sets the user state for the one model class, not any related models. You could loop through all relationships recursively to make sure the data is set down the line, but ehhh...

What I'm Using Now

Here's what I have. It's not perfect, but it's the best solution I've come up with so far. I liked the "User::get()" syntax used in the debuggable.com posts, so I worked from there. In your User model:

httpc://github.com/mcurry/cakephp/raw/2b78e2882718cc8a94b4e092897d3fec90ff5510/snippets/static_user/user.php

In AppController, beforeFilter method (assuming you are using the Auth component):

App::import('Model', 'User');
User::store($this->Auth->user());

How It Works

To get the currently logged in user's id:

User::get('id');

To get the currently logged in user's username (assuming you have a field "username" in your users table):

User::get('username');

Any other fields in your user table can be retrieved in the same manner. Also if you store related model data in your user session it can be retrieved:

User::get('Model.fieldname');

Issues

To get the "User::get()" syntax to work this block has to be placed in the User model, but it feels slightly misplaced there.

Uses

This has code has come in really handy. I use it a behavior with a beforeSave callback to set the created by user id and modified user id. Likewise you can have a behavior that automatically filters rows for index pages to only show records appropriate for the logged in user. It also works in anywhere in your Cake app. In views you can use this instead of $session->read('Auth.User.id').

Update

I missed that Cake already has a Model::set method. To prevent the conflict the method to set the user has been changed to "store". I also added an App::import in the AppController->beforeFilter to make sure the user model is already included.

Posted in GitHub | 10 Comments

CakePHP HTML Cache Helper

Posted by Matt on Wed, Sep 03 2008

The latest version of this code can be found here: http://github.com/mcurry/html_cache

Cake's core cache helper is great, but the files it outputs are PHP files, so it will never be as fast as straight HTML files. This HTML Cache Helper writes out pure HTML, meaning the web server doesn't have to touch PHP when a request is made. Yea, I know there are some huge limitations with this. First of all you can't have any user/session specific code on the page. Also there is no way to automatically check if the cache is expired and needs to be rebuilt.

Uses

I use this helper on RSStalker.com. It handles the custom RSS feeds (currently around 13k), which is perfect since there is nothing user specific in the XML. Each feed gets hit multiple times a day, by multiple aggregators. This really adds up to a ton of requests.

The Code

You can download it here. Or just copy and paste this into /app/views/helpers/html_cache.php:

httpc://github.com/mcurry/cakephp/raw/f76839a885da27a7c95efe77bc4ad42197bd128f/helpers/html_cache/html_cache.php

There really isn't much to it. Just add it to any controller that you want to cache the output of.

In addition you need to add two line to your webroot/.htaccess, so that the rewrite section looks like this:

httpc://github.com/mcurry/cakephp/raw/f76839a885da27a7c95efe77bc4ad42197bd128f/helpers/html_cache/webroot.htaccess

Issues

To expire the cache I use a cron job which deletes old files from the directory.

find /full/path/to/app/webroot/cache -mmin +360 | xargs rm -f

The cached files are getting written right to your webroot. The default Cake .htaccess checks to see if a file actually exists, this is what allows images, js, css, and other files to be handled directly by the web server.

This won't work with the root file of your controller. So for example www.rsstalker.com/feeds won't work, but www.rsstalker.com/feeds/amazon does.

Posted in GitHub | 7 Comments
1 | 2 | 3