Skip to content

Instantly share code, notes, and snippets.

@jaredhoyt
Created January 4, 2011 20:54
Show Gist options
  • Save jaredhoyt/765395 to your computer and use it in GitHub Desktop.
Save jaredhoyt/765395 to your computer and use it in GitHub Desktop.
Custom find methods
<?php
class Page extends AppModel {
var $_findMethods = array(
'index' => true,
);
function _findRevision($state, $query, $results = array()) {
if ($state == 'before') {
if (empty($query['slug'])) {
throw new InvalidArgumentException(__('Invalid page', true));
}
if (empty($query['rev_id'])) {
throw new InvalidArgumentException(__('Invalid revision', true));
}
$query['conditions'] = array("Page.slug" => $query['slug']);
$query['fields'] = array(
'Page.id', 'Page.title', 'Page.slug',
'Revision.id', 'Revision.version_id', 'Revision.edit_summary', 'Revision.content',
'Revision.version_created',
);
$query['joins'] = array(array(
'table' => 'pages_revs',
'alias' => 'Revision',
'type' => 'INNER',
'foreignKey' => false,
'conditions'=> array(
"Revision.version_id = {$query['revision']}",
'Revision.id = Page.id'
),
'limit' => '1',
));
$query['limit'] = 1;
unset($query['slug']);
unset($query['revision']);
return $query;
} elseif ($state == 'after') {
if (empty($results[0])) {
throw new OutOfBoundsException(__('Invalid page', true));
}
return $results[0];
}
}
}
class PagesController extends AppController {
function revision($rev_id = null, $title = null) {
try {
$page = $this->Page->find('revision', array(
'slug' => $title,
'revision' => $rev_id,
));
} catch (Exception $e) {
$this->redirect(array('action' => 'index'));
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment