Skip to content

Instantly share code, notes, and snippets.

Created February 5, 2012 08:54
Show Gist options
  • Save anonymous/1744216 to your computer and use it in GitHub Desktop.
Save anonymous/1744216 to your computer and use it in GitHub Desktop.
<?php
App::uses('AppModel', 'Model');
/**
* Anime Model
*
* @property User $User
* @property Language $Language
* @property EpisodeLimit $EpisodeLimit
* @property Release $Release
* @property Country $Country
* @property News $News
*/
class Anime extends AppModel {
var $actAs = array(
'Ownable'
);
/**
* Display field
*
* @var string
*/
public $displayField = 'name';
/**
* Validation rules
*
* @var array
*/
public $validate = array(
'user_id' => array(
'notempty' => array(
'rule' => array('notempty'),
//'message' => 'Your custom message here',
//'allowEmpty' => false,
//'required' => false,
//'last' => false, // Stop validation after this rule
//'on' => 'create', // Limit validation to 'create' or 'update' operations
),
'numeric' => array(
'rule' => array('numeric'),
//'message' => 'Your custom message here',
//'allowEmpty' => false,
//'required' => false,
//'last' => false, // Stop validation after this rule
//'on' => 'create', // Limit validation to 'create' or 'update' operations
),
),
'approved_by' => array(
'numeric' => array(
'rule' => array('numeric'),
//'message' => 'Your custom message here',
//'allowEmpty' => false,
//'required' => false,
//'last' => false, // Stop validation after this rule
//'on' => 'create', // Limit validation to 'create' or 'update' operations
),
),
'language_id' => array(
'notempty' => array(
'rule' => array('notempty'),
//'message' => 'Your custom message here',
//'allowEmpty' => false,
//'required' => false,
//'last' => false, // Stop validation after this rule
//'on' => 'create', // Limit validation to 'create' or 'update' operations
),
'numeric' => array(
'rule' => array('numeric'),
//'message' => 'Your custom message here',
//'allowEmpty' => false,
//'required' => false,
//'last' => false, // Stop validation after this rule
//'on' => 'create', // Limit validation to 'create' or 'update' operations
),
),
);
//The Associations below have been created with all possible keys, those that are not needed can be removed
/**
* belongsTo associations
*
* @var array
*/
public $belongsTo = array(
'User' => array(
'className' => 'User',
'foreignKey' => 'user_id',
'conditions' => '',
'fields' => '',
'order' => ''
),
'Language' => array(
'className' => 'Language',
'foreignKey' => 'language_id',
'conditions' => '',
'fields' => '',
'order' => ''
)
);
/**
* hasMany associations
*
* @var array
*/
public $hasMany = array(
'EpisodeLimit' => array(
'className' => 'EpisodeLimit',
'foreignKey' => 'anime_id',
'dependent' => false,
'conditions' => '',
'fields' => '',
'order' => '',
'limit' => '',
'offset' => '',
'exclusive' => '',
'finderQuery' => '',
'counterQuery' => ''
),
'Release' => array(
'className' => 'Release',
'foreignKey' => 'anime_id',
'dependent' => false,
'conditions' => '',
'fields' => '',
'order' => '',
'limit' => '',
'offset' => '',
'exclusive' => '',
'finderQuery' => '',
'counterQuery' => ''
)
);
/**
* hasAndBelongsToMany associations
*
* @var array
*/
public $hasAndBelongsToMany = array(
'Country' => array(
'className' => 'Country',
'joinTable' => 'animes_countries',
'foreignKey' => 'anime_id',
'associationForeignKey' => 'country_id',
'unique' => 'keepExisting',
'conditions' => '',
'fields' => '',
'order' => '',
'limit' => '',
'offset' => '',
'finderQuery' => '',
'deleteQuery' => '',
'insertQuery' => ''
),
'News' => array(
'className' => 'News',
'joinTable' => 'news_animes',
'foreignKey' => 'anime_id',
'associationForeignKey' => 'news_id',
'unique' => 'keepExisting',
'conditions' => '',
'fields' => '',
'order' => '',
'limit' => '',
'offset' => '',
'finderQuery' => '',
'deleteQuery' => '',
'insertQuery' => ''
)
);
}
<?php
class OwnableBehavior extends ModelBehavior{
var $settings = array();
var $user_id = false;
function setup($Model, $settings) {
if (!isset($this->settings[$Model->alias])) {
$this->settings[$Model->alias] = array(
'option1_key' => 'option1_default_value',
'option2_key' => 'option2_default_value',
'option3_key' => 'option3_default_value',
);
}
$this->settings[$Model->alias] = array_merge(
$this->settings[$Model->alias], (array)$settings);
$this->user_id = CakeSession::read('Auth.User.id');
pr($this->user_id);
}
function beforeFind(&$model, $conditions = array()) {
print_r($conditions);
if (empty($conditions['conditions'])) {
$conditions['conditions'] = array(array('user_id' => $this->user_id));
} else {
$old_condition = $conditions['conditions'];
$conditions['conditions'] = array($old_condition, array('user_id' => $this->user_id));
}
return $conditions;
}
}
<?php
class OwnableBehavior extends ModelBehavior{
var $settings = array();
var $user_id = false;
function setup($Model, $settings) {
if (!isset($this->settings[$Model->alias])) {
$this->settings[$Model->alias] = array(
'option1_key' => 'option1_default_value',
'option2_key' => 'option2_default_value',
'option3_key' => 'option3_default_value',
);
}
$this->settings[$Model->alias] = array_merge(
$this->settings[$Model->alias], (array)$settings);
$this->user_id = CakeSession::read('Auth.User.id');
pr($this->user_id);
}
function beforeFind(&$model, $conditions = array()) {
print_r($conditions);
if (empty($conditions['conditions'])) {
$conditions['conditions'] = array(array('user_id' => $this->user_id));
} else {
$old_condition = $conditions['conditions'];
$conditions['conditions'] = array($old_condition, array('user_id' => $this->user_id));
}
return $conditions;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment