Skip to content

Instantly share code, notes, and snippets.

@hiromi2424
Created September 1, 2011 21:25
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hiromi2424/1187325 to your computer and use it in GitHub Desktop.
Save hiromi2424/1187325 to your computer and use it in GitHub Desktop.
preferable example for getting data method for model
<?php
class Post extends AppModel {
/**
* Usage:
* $nextPost = $this->Post->getNext(1);
*/
public function getNext($id) {
$greater = $this->find('first', array(
'conditions' => array(
'Post.id >' => $id,
),
));
if ($greater) {
return $greater;
}
$littler = $this->find('first', array(
'conditions' => array(
'Post.id <' => $id,
),
));
return $littler;
}
}
<?php
class Post extends AppModel {
public $belongsTo = array(
'Category',
);
/**
* Usage:
* $nextPost = $this->Post->getNext(1, 'CakePHP');
*/
public function getNext($id, $category) {
$greater = $this->find('first', array(
'conditions' => array(
'Post.id >' => $id,
'Category.name' => $category,
),
));
if ($greater) {
return $greater;
}
$littler = $this->find('first', array(
'conditions' => array(
'Post.id <' => $id,
'Category.name' => $category,
),
));
return $littler;
}
}
<?php
class Post extends AppModel {
public $belongsTo = array(
'Category',
);
/**
* Usage:
* $nextPost = $this->Post->getNext(1);
* $nextPost = $this->Post->getNext(1, 'CakePHP');
*/
public function getNext($id, $category = null) {
$categoryScope = $category === null ? array() : array(
'Category.name' => $category,
);
$greater = $this->find('first', array(
'conditions' => array(
'Post.id >' => $id,
) + $categoryScope,
));
if ($greater) {
return $greater;
}
$littler = $this->find('first', array(
'conditions' => array(
'Post.id <' => $id,
) + $categoryScope,
));
return $littler;
}
}
<?php
class Post extends AppModel {
public $belongsTo = array(
'Category',
);
/**
* Usage:
* $nextPost = $this->Post->getNext(1);
* $nextPost = $this->Post->getNext(1, 'CakePHP');
* $nextPost = $this->Post->getNext(1, 'CakePHP', 'tips');
* $nextPost = $this->Post->getNext(1, null, 'tips');
*/
public function getNext($id, $category = null, $tag = null) {
$categoryScope = $category === null ? array() : array(
'Category.name' => $category,
);
$tagScope = $tagQuery = array();
if ($tag !== null) {
$tagScope = array(
'Tag.name' => $tag,
);
$tagQuery = array(
'joins' => array(
array(
'alias' => 'PostsTag',
'table' => 'posts_tags',
'conditions' => '`PostsTag`.`post_id` = `Post`.`id`',
'type' => 'RIGHT',
),
array(
'alias' => 'Tag',
'table' => 'tags',
'conditions' => '`PostsTag`.`tag_id` = `Tag`.`id`',
'type' => 'RIGHT',
),
),
'group' => 'Post.id',
);
}
$greater = $this->find('first', array(
'conditions' => array(
'Post.id >' => $id,
) + $categoryScope + $tagScope,
) + $tagQuery);
if ($greater) {
return $greater;
}
$littler = $this->find('first', array(
'conditions' => array(
'Post.id <' => $id,
) + $categoryScope + $tagScope,
) + $tagQuery);
return $littler;
}
}
<?php
class Post extends AppModel {
public $belongsTo = array(
'Category',
);
var $_findMethods = array(
'next' => true,
);
}
<?php
class Post extends AppModel {
public $belongsTo = array(
'Category',
);
var $_findMethods = array(
'next' => true,
);
/**
* Usage:
* $nextPost = $this->Post->find('next', array(
* 'id' => 1,
* ...
* ));
*/
protected function _findNext($state, $query, $results = array()) {
if ($state == 'before') {
$greaterQuery = Set::merge($query, array(
'conditions' => array(
'Post.id >' => $query['id'],
),
));
$greaterExists = $this->find('count', $greaterQuery);
if (!$greaterExists) {
$littlerQuery = Set::merge($query, array(
'conditions' => array(
'Post.id <' => $query['id'],
),
));
$query = $littlerQuery;
} else {
$query = $greaterQuery;
}
unset($query['id']);
$query['order'] = array(
'Post.id' => 'ASC',
);
$query['limit'] = 1;
return $query;
} elseif ($state == 'after') {
if (empty($results[0])) {
return false;
}
return $results[0];
}
}
}
<?php
class Post extends AppModel {
public $belongsTo = array(
'Category',
);
var $_findMethods = array(
'next' => true,
);
/**
* Usage:
* $nextPost = $this->Post->find('next', array(
* 'id' => 1,
* ...
* ));
*/
protected function _findNext($state, $query, $results = array()) {
if ($state == 'before') {
$greaterQuery = Set::merge($query, array(
'conditions' => array(
'Post.id >' => $query['id'],
),
));
$greaterExists = $this->find('count', $greaterQuery);
if (!$greaterExists) {
$littlerQuery = Set::merge($query, array(
'conditions' => array(
'Post.id <' => $query['id'],
),
));
$query = $littlerQuery;
} else {
$query = $greaterQuery;
}
unset($query['id']);
$query['order'] = array(
'Post.id' => 'ASC',
);
return $this->_findFirst($state, $query, $results);
} elseif ($state == 'after') {
return $this->_findFirst($state, $query, $results);
}
}
}
<?php
class Post extends AppModel {
public $belongsTo = array(
'Category',
);
var $_findMethods = array(
'next' => true,
);
/**
* Usage:
* $nextPost = $this->Post->find('next', array(
* 'id' => 1,
* ...
* ));
*/
protected function _findNext($state, $query, $results = array()) {
if ($state == 'before') {
$greaterQuery = Set::merge($query, array(
'conditions' => array(
'Post.id >' => $query['id'],
),
));
$greaterExists = $this->find('count', $greaterQuery);
if (!$greaterExists) {
$littlerQuery = Set::merge($query, array(
'conditions' => array(
'Post.id <' => $query['id'],
),
));
$query = $littlerQuery;
} else {
$query = $greaterQuery;
}
unset($query['id']);
$query['order'] = array(
'Post.id' => 'ASC',
);
return $this->_findFirst($state, $query, $results);
} elseif ($state == 'after') {
return $this->_findFirst($state, $query, $results);
}
}
/**
* Available find keys:
* 'category': adding creteria with name of category.
* 'tag': adding creteria and additional association with name of tag.
*/
public function beforeFind($query = array()) {
if (array_key_exists('category', $query)) {
$query = Set::merge($query, array(
'conditions' => array(
'Category.name' => $query['category'],
),
));
unset($query['category']);
}
if (array_key_exists('tag', $query)) {
$query = Set::merge($query, array(
'conditions' => array(
'Tag.name' => $query['tag'],
),
'joins' => array(
array(
'alias' => 'PostsTag',
'table' => 'posts_tags',
'conditions' => '`PostsTag`.`post_id` = `Post`.`id`',
'type' => 'RIGHT',
),
array(
'alias' => 'Tag',
'table' => 'tags',
'conditions' => '`PostsTag`.`tag_id` = `Tag`.`id`',
'type' => 'RIGHT',
),
),
'group' => 'Post.id',
));
unset($query['tag']);
}
return $query;
}
}
<?php
class Post extends AppModel {
public $belongsTo = array(
'Category',
);
public $actsAs = array(
'FindNext',
'Categorize',
'Taggable',
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment