Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
preferable example for getting data method for model(behaviorized)
<?php
/**
* Available find keys:
* 'category': adding creteria with name of category.
*/
class CategorizeBehavior extends ModelBehavior {
public $defaultSettings = array(
'key' => 'category',
'categoryModel' => 'Category',
'field' => 'name',
);
public function setup($Model, $config = array()) {
$this->settings[$Model->alias] = Set::merge($this->defaultSettings, $config);
}
public function beforeFind($Model, $query = array()) {
if (array_key_exists($this->settings[$Model->alias]['key'], $query)) {
$query = Set::merge($query, array(
'conditions' => array(
$this->settings[$Model->alias]['categoryModel'] . '.' . $this->settings[$Model->alias]['field'] => $query[$this->settings[$Model->alias]['key']],
),
));
unset($query[$this->settings[$Model->alias]['key']]);
}
return $query;
}
}
<?php
/**
* Usage:
* $nextPost = $this->Post->find('next', array(
* 'id' => 1,
* ...
* ));
*/
class FindNextBehavior extends ModelBehavior {
public $defaultSettings = array(
'findType' => 'next',
);
public function setup($Model, $config = array()) {
$this->settings[$Model->alias] = Set::merge($this->defaultSettings, $config);
$this->mapMethods = array(
sprintf('/^_find%s$/', ucfirst($this->settings[$Model->alias]['findType'])) => '__findNext',
);
$Model->_findMethods[$this->settings[$Model->alias]['findType']] = true;
}
public function __findNext($Model, $method, $state, $query, $results = array()) {
if ($state == 'before') {
if (!array_key_exists('value', $query)) {
throw new Exception(__d('findNext', 'query must contain value for findNext: ', true) . var_export($query, true));
}
$field = isset($query['field']) ? $field : $Model->alias . '.' . $Model->primaryKey;
unset($query['field']);
$greaterQuery = Set::merge($query, array(
'conditions' => array(
"$field >" => $query['value'],
),
));
$greaterExists = $Model->find('count', $greaterQuery);
if (!$greaterExists) {
$littlerQuery = Set::merge($query, array(
'conditions' => array(
"$field <" => $query['value'],
),
));
$query = $littlerQuery;
} else {
$query = $greaterQuery;
}
unset($query['value']);
$query['order'] = array(
$field => 'ASC',
);
return $Model->_findFirst($state, $query, $results);
} elseif ($state == 'after') {
return $Model->_findFirst($state, $query, $results);
}
}
}
<?php
/**
* Available find keys:
* 'tag': adding creteria and additional association with name of tag.
*/
class TaggableBehavior extends ModelBehavior {
public $defaultSettings = array(
'key' => 'tag',
'joinModel' => null,
'tagModel' => 'Tag',
'field' => 'name',
'group' => 'id',
);
public function setup($Model, $config = array()) {
$this->settings[$Model->alias] = Set::merge($this->defaultSettings, $config);
}
public function beforeFind($Model, $query = array()) {
$settings = $this->settings[$Model->alias];
if (array_key_exists($settings['key'], $query)) {
if (!$settings['joinModel']) {
$settings['joinModel'] = Inflector::pluralize($Model->alias) . $settings['tagModel'];
}
$query = Set::merge($query, array(
'conditions' => array(
$settings['tagModel'] . '.' . $settings['field'] => $query[$settings['key']],
),
'joins' => array(
array(
'alias' => $settings['joinModel'],
'table' => Inflector::tableize($settings['joinModel']),
'conditions' => sprintf(
'`%s`.`%s` = `%s`.`id`',
$settings['joinModel'],
Inflector::underscore($Model->alias) . '_id',
$Model->alias
),
'type' => 'RIGHT',
),
array(
'alias' => $settings['tagModel'],
'table' => Inflector::tableize($settings['tagModel']),
'conditions' => sprintf(
'`%s`.`%s` = `%s`.`id`',
$settings['joinModel'],
Inflector::underscore($settings['tagModel']) . '_id',
$settings['tagModel']
),
'type' => 'RIGHT',
),
),
'group' => $Model->escapeField($settings['group']),
));
unset($query[$settings['key']]);
}
return $query;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment