Skip to content

Instantly share code, notes, and snippets.

@cpierce
Created June 3, 2016 04:04
Show Gist options
  • Save cpierce/756f37042671d09be17c517c7b02a4e0 to your computer and use it in GitHub Desktop.
Save cpierce/756f37042671d09be17c517c7b02a4e0 to your computer and use it in GitHub Desktop.
<?php
namespace App\Model\Table;
use App\Model\Entity\Calendar;
use Cake\ORM\Query;
use Cake\ORM\RulesChecker;
use Cake\ORM\Table;
use Cake\Validation\Validator;
/**
* Calendars Model
*
* @property \Cake\ORM\Association\BelongsTo $ApiKeys
* @property \Cake\ORM\Association\HasMany $CalendarEvents
* @property \Cake\ORM\Association\HasMany $CalendarsFeeds
*/
class CalendarsTable extends Table
{
/**
* Initialize method
*
* @param array $config The configuration for the Table.
* @return void
*/
public function initialize(array $config)
{
parent::initialize($config);
$this->table('calendars');
$this->displayField('title');
$this->primaryKey('id');
$this->addBehavior('Timestamp');
$this->belongsTo('ApiKeys', [
'foreignKey' => 'api_key_id',
]);
$this->hasMany('CalendarEvents', [
'foreignKey' => 'calendar_id',
'dependent' => true,
]);
$this->belongsToMany('Feeds', [
'className' => 'Calendars',
'joinTable' => 'calendar_feeds',
'foreignKey' => 'id',
'targetForeignKey' => 'id',
'dependent' => false,
]);
}
/**
* Default validation rules.
*
* @param \Cake\Validation\Validator $validator Validator instance.
* @return \Cake\Validation\Validator
*/
public function validationDefault(Validator $validator)
{
$validator
->integer('id')
->allowEmpty('id', 'create');
$validator
->requirePresence('title', true, 'A title is required.')
->notEmpty('title', 'Title cannot be blank.');
$validator
->integer('calendar_event_count')
->allowEmpty('calendar_event_count');
return $validator;
}
/**
* Returns a rules checker object that will be used for validating
* application integrity.
*
* @param \Cake\ORM\RulesChecker $rules The rules object to be modified.
* @return \Cake\ORM\RulesChecker
*/
public function buildRules(RulesChecker $rules)
{
$rules->add($rules->existsIn(['api_key_id'], 'ApiKeys'));
return $rules;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment