本文整理汇总了PHP中ModelBehavior类的典型用法代码示例。如果您正苦于以下问题:PHP ModelBehavior类的具体用法?PHP ModelBehavior怎么用?PHP ModelBehavior使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了ModelBehavior类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: beforeSave
/**
* (non-PHPdoc)
* @see ModelBehavior::beforeSave()
*/
public function beforeSave(Model $Model)
{
// проверяем необходимость сворачивания полей в gridfs
foreach ($this->_settings['fields'] as $field) {
$data = $Model->data[$Model->alias];
$paths = explode('.', $field);
foreach ($paths as $path) {
if (!empty($data[$path])) {
$data = $data[$path];
} else {
$data = null;
}
}
$bytes = serialize($data);
if (!empty($data) && mb_strlen($bytes, '8bit') > $this->_settings['size']) {
// сохраняем содержимое поля в gridfs, а в значение поля записываем id в grid-е
$value = $this->_Grid->storeBytes($bytes);
if (count($paths) == 1) {
$Model->data[$Model->alias][$paths[0]] = $value;
} elseif (count($paths) == 2) {
$Model->data[$Model->alias][$paths[0]][$paths[1]] = $value;
} elseif (count($paths) == 3) {
$Model->data[$Model->alias][$paths[0]][$paths[1]][$paths[2]] = $value;
} elseif (count($paths) == 4) {
$Model->data[$Model->alias][$paths[0]][$paths[1]][$paths[2]][$paths[3]] = $value;
}
}
}
return parent::beforeSave($Model);
}
示例2: afterFind
/**
* After find callback. Can be used to modify any results returned by find.
*
* @param Model $model Model using this behavior
* @param mixed $results The results of the find operation
* @param boolean $primary Whether this model is being queried directly (vs. being queried as an association)
*
* @return mixed An array value will replace the value of $results - any other value will be ignored.
*/
public function afterFind(Model $model, $results, $primary = false)
{
parent::afterFind($model, $results, $primary);
if ($primary && array_key_exists($model->alias, $results[0])) {
$arrObj = new ArrayObject($results);
$iterator = $arrObj->getIterator();
while ($iterator->valid()) {
$result = [];
if (isset($iterator->current()[$model->alias]) && count($iterator->current()[$model->alias]) > 0) {
$key = "{$model->alias}.{$this->settings[$model->alias]['key']}";
$value = "{$model->alias}.{$this->settings[$model->alias]['value']}";
$result = Hash::combine($iterator->current(), $key, $value);
}
if (!array_key_exists($this->settings[$model->alias]['key'], $iterator->current()[$model->alias]) && !array_key_exists($this->settings[$model->alias]['value'], $iterator->current()[$model->alias])) {
$results[$iterator->key()][$model->alias] = Hash::merge($iterator->current()[$model->alias], $result);
} else {
$results[$iterator->key()][$model->alias] = $result;
}
$iterator->next();
}
} elseif (array_key_exists($model->alias, $results)) {
$key = "{n}.{$model->alias}.{$this->settings[$model->alias]['key']}";
$value = "{n}.{$model->alias}.{$this->settings[$model->alias]['value']}";
$output = Hash::combine($results, $key, $value);
$results[$model->alias] = $output;
}
return $results;
}
示例3: beforeValidate
public function beforeValidate(Model $model, $options = array())
{
if (array_key_exists($model->alias, $model->request->data)) {
if (array_key_exists('contact_type_id', $model->data[$model->alias])) {
$model->validator()->remove('value');
switch ($model->data[$model->alias]['contact_type_id']) {
// 1 -> Mobile, 2 -> Phone, 3 -> Fax, 4 -> Email, 5 -> Other
case '1':
case '2':
case '3':
$model->validator()->add('value', 'required', array('rule' => 'Numeric', 'message' => $model->getErrorMessage('valuePhone')));
break;
case '4':
$model->validator()->add('value', 'required', array('rule' => array('email', true), 'message' => $model->getErrorMessage('valueEmail')));
break;
default:
// 5 -> Other
$model->validator()->add('value', 'required', array('rule' => 'notEmpty', 'message' => $model->getErrorMessage('value')));
break;
}
}
}
$model->validator()->remove('name');
$model->validator()->add('name', 'required', array('rule' => 'notEmpty', 'message' => $model->getErrorMessage('value')));
$model->validator()->remove('contact_type_id');
$model->validator()->add('contact_type_id', 'required', array('rule' => 'notEmpty', 'message' => $model->getErrorMessage('contact_type_id')));
return parent::beforeValidate($model, $options);
}
示例4: init
public function init()
{
parent::init();
/* @var \Flywheel\Model\ActiveRecord $owner */
$owner = $this->getOwner();
$owner->getPrivateEventDispatcher()->addListener('onBeforeSave', array($this, 'onBeforeSave'));
}
示例5: afterFind
public function afterFind(Model $Model, $results, $primary = false)
{
if (!$primary) {
return parent::afterFind($results, $primary);
}
foreach ($results as $key => $val) {
if (array_key_exists($Model->alias, $val) && array_key_exists('id', $val[$Model->alias]) && array_key_exists('Feed', $val)) {
//debug($val);
$Subscription = ClassRegistry::init('Social.Subscription');
// Getting total Subscriptions
$totalSubscriptions = $Subscription->find('count', array('conditions' => array('Subscription.feed_id' => $val['Feed']['id'])));
$results[$key]['Subscribed']['total'] = $totalSubscriptions;
// Checking if current content is liked by current user
$uid = CakeSession::read("Auth.User.id");
if ($uid) {
$_subscription = $Subscription->find('first', array('conditions' => array('Subscription.feed_id' => $val['Feed']['id'], 'Subscription.user_id' => $uid)));
if ($_subscription) {
$results[$key]['Subscribed']['is_subscribed'] = 1;
} else {
$results[$key]['Subscribed']['is_subscribed'] = 0;
}
} else {
$results[$key]['Subscribed']['is_subscribed'] = 0;
}
}
}
return $results;
}
示例6: afterFind
public function afterFind(Model $Model, $results, $primary = false)
{
if (!$primary) {
return parent::afterFind($results, $primary);
}
foreach ($results as $key => $val) {
if ($primary && array_key_exists($Model->alias, $val) && array_key_exists('id', $val[$Model->alias])) {
$Like = ClassRegistry::init('Social.Like');
// Getting total likes
$totalLikes = $Like->find('count', array('conditions' => array('Like.object_id' => $val[$Model->alias]['id'], 'Like.object' => $Model->alias)));
$results[$key]['Liked']['total'] = $totalLikes;
// Checking if current content is liked by current user
$uid = CakeSession::read("Auth.User.id");
$isLiked = 0;
if ($uid) {
$_like = $Like->find('first', array('conditions' => array('Like.object_id' => $val[$Model->alias]['id'], 'Like.object' => $Model->alias, 'Like.user_id' => $uid)));
if ($_like) {
$results[$key]['Liked']['id'] = $_like['Like']['id'];
$results[$key]['Liked']['is_liked'] = 1;
} else {
$results[$key]['Liked']['is_liked'] = 0;
}
} else {
$results[$key]['Liked']['is_liked'] = 0;
}
// Getting the name of the last two persons who liked this besides the current user
$Like->bindModel(array('belongsTo' => array('User')), true);
$options = array('conditions' => array('Like.object_id' => $val[$Model->alias]['id'], 'Like.object' => $Model->alias, 'User.id NOT' => $uid), 'contain' => array('User' => array('fields' => array('id', 'first_name', 'last_name'))), 'limit' => 2, 'order' => 'Like.created');
$_like = $Like->find('all', $options);
$results[$key]['Liked']['last_liked'] = $_like;
}
}
return $results;
}
示例7: __construct
public function __construct()
{
parent::__construct();
if (!Cache::config(self::CACHE_CONFIG)) {
Cache::config(self::CACHE_CONFIG, array('engine' => 'File', 'duration' => 300, 'prefix' => 'attachable_', 'probability' => 100, 'serialize' => true, 'path' => MEDIA_CACHE_DIR));
}
}
示例8: beforeSave
/**
* Run before a model is saved, used to set up slug for model.
*
* @param object $Model Model about to be saved.
* @return boolean true if save should proceed, false otherwise
* @access public
*/
function beforeSave(&$Model)
{
$return = parent::beforeSave($Model);
// Make label fields an array
if (!is_array($this->__settings[$Model->alias]['label'])) {
$this->__settings[$Model->alias]['label'] = array($this->__settings[$Model->alias]['label']);
}
// Make sure all label fields are available
foreach ($this->__settings[$Model->alias]['label'] as $field) {
if (!$Model->hasField($field)) {
return $return;
}
}
// See if we should be generating a slug
if ($Model->hasField($this->__settings[$Model->alias]['slug']) && ($this->__settings[$Model->alias]['overwrite'] || empty($Model->id))) {
// Build label out of data in label fields, if available, or using a default slug otherwise
$label = '';
foreach ($this->__settings[$Model->alias]['label'] as $field) {
if (!empty($Model->data[$Model->alias][$field])) {
$label .= ife(!empty($label), ' ', '') . $Model->data[$Model->alias][$field];
}
}
// Keep on going only if we've got something to slug
if (!empty($label)) {
// Get the slug
$slug = $this->__slug($label, $this->__settings[$Model->alias]);
// Look for slugs that start with the same slug we've just generated
$conditions = array($Model->alias . '.' . $this->__settings[$Model->alias]['slug'] => 'LIKE ' . $slug . '%');
if (!empty($Model->id)) {
$conditions[$Model->alias . '.' . $Model->primaryKey] = '!= ' . $Model->id;
}
$result = $Model->find('all', array('conditions' => $conditions, 'fields' => array($Model->primaryKey, $this->__settings[$Model->alias]['slug']), 'recursive' => -1));
$sameUrls = null;
if (!empty($result)) {
$sameUrls = Set::extract($result, '{n}.' . $Model->alias . '.' . $this->__settings[$Model->alias]['slug']);
}
// If we have collissions
if (!empty($sameUrls)) {
$begginingSlug = $slug;
$index = 1;
// Attach an ending incremental number until we find a free slug
while ($index > 0) {
if (!in_array($begginingSlug . $this->__settings[$Model->alias]['separator'] . $index, $sameUrls)) {
$slug = $begginingSlug . $this->__settings[$Model->alias]['separator'] . $index;
$index = -1;
}
$index++;
}
}
// Now set the slug as part of the model data to be saved, making sure that
// we are on the white list of fields to be saved
if (!empty($Model->whitelist) && !in_array($this->__settings[$Model->alias]['slug'], $Model->whitelist)) {
$Model->whitelist[] = $this->__settings[$Model->alias]['slug'];
}
$Model->data[$Model->alias][$this->__settings[$Model->alias]['slug']] = $slug;
}
}
return $return;
}
示例9: afterSave
/**
* afterSave Hook
*
* @param object $model
* @param integer $created
*/
function afterSave(&$model, $created)
{
parent::afterSave($model, $created);
if (!empty($model->data['Node']['status'])) {
$options = array('name' => Configure::read('Site.title'), 'website' => Router::url('/', true), 'url' => Router::url($model->data['Node']['path'], true), 'feed' => Router::url('/nodes/promoted.rss', true));
$this->ping($options);
}
}
示例10: beforeSave
public function beforeSave(Model $Model)
{
$return = parent::beforeSave($Model);
if ($this->settings[$Model->alias]['before'] == 'save') {
return $this->confirm($Model, $return);
}
return $return;
}
示例11: setup
/**
* Setup this behavior with the specified configuration settings.
*
* @param Model $model Model using this behavior
* @param array $config Configuration settings for $model
* @return void
*/
public function setup(Model $model, $config = array())
{
parent::setup($model, $config);
foreach ($config as $field) {
$this->settings[$model->alias]['fields'][] = $field;
}
$this->validateDb($model);
}
示例12: beforeSave
public function beforeSave(Model $Model, $options = array())
{
parent::beforeSave($Model, $options);
if ($this->settings[$Model->alias]['before'] === 'save') {
return $this->geocode($Model);
}
return true;
}
示例13: beforeSave
public function beforeSave(Model $Model)
{
parent::beforeSave($Model);
if ($this->settings[$Model->alias]['before'] == 'save') {
$this->process($Model);
}
return true;
}
示例14: setup
public function setup(Model $model, $config = array())
{
parent::setup($model, $config);
if (!isset($this->settings[$model->alias])) {
$this->settings[$model->alias] = array();
}
$this->settings[$model->alias] = array_merge($this->settings[$model->alias], (array) $config);
}
示例15: beforeSave
/**
* beforeSave callback
*
* @param Model $model The current model being used
* @param array $options Callback options
*
* @return mixed
*/
public function beforeSave(Model $model, $options = array())
{
$fields = (array) $this->__settings[$model->alias]['fields'];
$scope = (array) $this->__settings[$model->alias]['scope'];
$conditions = !empty($this->__settings[$model->alias]['conditions']) ? (array) $this->__settings[$model->alias]['conditions'] : array();
$slugfield = $this->__settings[$model->alias]['slugfield'];
$hasFields = true;
foreach ($fields as $field) {
if (!$model->hasField($field)) {
$hasFields = false;
}
if (!isset($model->data[$model->alias][$field])) {
$hasFields = false;
}
}
if ($hasFields && $model->hasField($slugfield) && ($this->__settings[$model->alias]['overwrite'] || empty($model->id))) {
$toSlug = array();
foreach ($fields as $field) {
$toSlug[] = $model->data[$model->alias][$field];
}
$toSlug = join(' ', $toSlug);
$slug = Inflector::slug($toSlug, $this->__settings[$model->alias]['separator']);
if ($this->__settings[$model->alias]['lower']) {
$slug = strtolower($slug);
}
if (strlen($slug) > $this->__settings[$model->alias]['length']) {
$slug = substr($slug, 0, $this->__settings[$model->alias]['length']);
}
$conditions[$model->alias . '.' . $slugfield . ' LIKE'] = $slug . '%';
if (!empty($model->id)) {
$conditions[$model->alias . '.' . $model->primaryKey . ' !='] = $model->id;
}
if (!empty($scope)) {
foreach ($scope as $s) {
if (isset($model->data[$model->alias][$s])) {
$conditions[$model->alias . '.' . $s] = $model->data[$model->alias][$s];
}
}
}
$sameUrls = $model->find('all', array('recursive' => -1, 'conditions' => $conditions));
$sameUrls = !empty($sameUrls) ? Hash::extract($sameUrls, '{n}.' . $model->alias . '.' . $slugfield) : array();
if (!empty($sameUrls)) {
if (in_array($slug, $sameUrls)) {
$begginingSlug = $slug;
$index = 1;
while ($index > 0) {
if (!in_array($begginingSlug . $this->__settings[$model->alias]['separator'] . $index, $sameUrls)) {
$slug = $begginingSlug . $this->__settings[$model->alias]['separator'] . $index;
$index = -1;
}
$index++;
}
}
}
$model->data[$model->alias][$slugfield] = $slug;
}
return parent::beforeSave($model);
}