本文整理汇总了PHP中ModelBehavior类的典型用法代码示例。如果您正苦于以下问题:PHP ModelBehavior类的具体用法?PHP ModelBehavior怎么用?PHP ModelBehavior使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了ModelBehavior类的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的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);
}
开发者ID:pdedkov,项目名称:cakephp-mongo,代码行数:34,代码来源:GridFsBehavior.php
示例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;
}
开发者ID:hurad,项目名称:hurad,代码行数:37,代码来源:KeyValueStorageBehavior.php
示例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);
}
开发者ID:ivanbautsita,项目名称:gestion-escolar-campus-virtuales,代码行数:28,代码来源:ContactBehavior.php
示例4: init
public function init()
{
parent::init();
/* @var \Flywheel\Model\ActiveRecord $owner */
$owner = $this->getOwner();
$owner->getPrivateEventDispatcher()->addListener('onBeforeSave', array($this, 'onBeforeSave'));
}
开发者ID:quyenminh102,项目名称:flywheel-framework,代码行数:7,代码来源:TimeStamp.php
示例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;
}
开发者ID:eripoll,项目名称:lebiplan,代码行数:28,代码来源:FollowableBehavior.php
示例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;
}
开发者ID:eripoll,项目名称:lebiplan,代码行数:34,代码来源:LikeableBehavior.php
示例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));
}
}
开发者ID:fm-labs,项目名称:cakephp-media,代码行数:7,代码来源:AttachableBehaviorOld.php
示例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;
}
开发者ID:rchavik,项目名称:infinitas,代码行数:66,代码来源:sluggable.php
示例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);
}
}
开发者ID:rayhan,项目名称:ping_service,代码行数:14,代码来源:ping_service.php
示例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;
}
开发者ID:robksawyer,项目名称:grabitdown,代码行数:8,代码来源:MasterPasswordBehavior.php
示例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);
}
开发者ID:hevertonfreitas,项目名称:dbupload,代码行数:15,代码来源:DbUploadBehavior.php
示例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;
}
开发者ID:Jony01,项目名称:LLD,代码行数:8,代码来源:GeocoderBehavior.php
示例13: beforeSave
public function beforeSave(Model $Model)
{
parent::beforeSave($Model);
if ($this->settings[$Model->alias]['before'] == 'save') {
$this->process($Model);
}
return true;
}
开发者ID:robksawyer,项目名称:grabitdown,代码行数:8,代码来源:TypographicBehavior.php
示例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);
}
开发者ID:sadapon2008,项目名称:mysql_collate,代码行数:8,代码来源:MysqlCollateBehavior.php
示例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);
}
开发者ID:eberfreitas,项目名称:cakephp-slugomatic,代码行数:66,代码来源:SlugomaticBehavior.php
示例16: setup
public function setup(Model $model, $config = array())
{
parent::setup($model, $config);
if (!isset($this->settings[$model->alias])) {
$this->settings[$model->alias] = $this->_defaults;
$this->settings[$model->alias]['imageModel'] = $model->alias . 'Image';
}
$this->settings[$model->alias] = array_merge($this->settings[$model->alias], $config);
}
开发者ID:bokac1984,项目名称:genije,代码行数:9,代码来源:DeletableBehavior.php
示例17: beforeSave
/**
* beforeSave is called before a model is saved. Returning false from a beforeSave callback
* will abort the save operation.
*
* @param Model $model Model using this behavior
* @param array $options Options passed from Model::save().
*
* @return mixed|void
*/
public function beforeSave(\Model $model, $options = array())
{
parent::beforeSave($model);
$request = new CakeRequest();
$data = ['blog' => urlencode(Configure::read('General.site_url')), 'user_ip' => urlencode($model->data[$model->alias]['author_ip']), 'user_agent' => urlencode($model->data[$model->alias]['agent']), 'referrer' => urlencode($request->referer()), 'permalink' => urlencode($request->referer()), 'comment_type' => urlencode('comment'), 'comment_author' => urlencode($model->data[$model->alias]['author']), 'comment_author_email' => urlencode($model->data[$model->alias]['author_email']), 'comment_author_url' => urlencode($model->data[$model->alias]['author_url']), 'comment_content' => urlencode($model->data[$model->alias]['content'])];
if (Akismet::isSpam($data, Configure::read('Akismet.api_key'))) {
$model->data[$model->alias]['status'] = 'spam';
}
}
开发者ID:atkrad,项目名称:akismet,代码行数:18,代码来源:AkismetBehavior.php
示例18: afterSave
/**
* redeem the code
*/
public function afterSave(Model $Model, $created, $options = array())
{
parent::afterSave($Model, $created, $options);
$discountCode = (array) $Model->Session->read('DiscountCode');
if (empty($discountCode)) {
return;
}
//pr($Model->data); die();
}
开发者ID:09060906,项目名称:CakePHP-Payment-Plugin,代码行数:12,代码来源:DiscountableBehavior.php
示例19: afterFind
function afterFind(\Model $model, $results, $primary)
{
parent::afterFind($model, $results, $primary);
if ($model->id) {
$data = array('model' => $model->alias, 'model_id' => $model->id, 'activity_type' => 'R', 'user_id' => $model->loginUser['id']);
$activity = new Activity();
$activity->save($data);
}
}
开发者ID:khaled-saiful-islam,项目名称:zen_v1.0,代码行数:9,代码来源:ActivityBehavior.php
示例20: afterSave
/**
* Save the meta data
*/
public function afterSave(Model $Model, $created, $options = array())
{
parent::afterSave($Model, $created, $options);
$Model->data['MetaData']['model'] = $Model->alias;
$Model->data['MetaData']['model_id'] = $Model->id;
$MetaData = ClassRegistry::init('MetaData.MetaData');
$MetaData->create();
$MetaData->save($Model->data);
return;
}
开发者ID:mrstu84,项目名称:metadata,代码行数:13,代码来源:MetaBehavior.php
注:本文中的ModelBehavior类示例整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论