Created
May 28, 2017 23:20
-
-
Save gustavocp/b905f3c657d219573abd56330ab2ce4e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Model\Table; | |
use Cake\ORM\Query; | |
use Cake\ORM\RulesChecker; | |
use Cake\ORM\Table; | |
use Cake\Validation\Validator; | |
/** | |
* Tasks Model | |
* | |
* @property \Cake\ORM\Association\BelongsTo $Users | |
* @property \Cake\ORM\Association\BelongsTo $Projects | |
* | |
* @method \App\Model\Entity\Task get($primaryKey, $options = []) | |
* @method \App\Model\Entity\Task newEntity($data = null, array $options = []) | |
* @method \App\Model\Entity\Task[] newEntities(array $data, array $options = []) | |
* @method \App\Model\Entity\Task|bool save(\Cake\Datasource\EntityInterface $entity, $options = []) | |
* @method \App\Model\Entity\Task patchEntity(\Cake\Datasource\EntityInterface $entity, array $data, array $options = []) | |
* @method \App\Model\Entity\Task[] patchEntities($entities, array $data, array $options = []) | |
* @method \App\Model\Entity\Task findOrCreate($search, callable $callback = null, $options = []) | |
* | |
* @mixin \Cake\ORM\Behavior\TimestampBehavior | |
*/ | |
class TasksTable extends Table | |
{ | |
/** | |
* Initialize method | |
* | |
* @param array $config The configuration for the Table. | |
* @return void | |
*/ | |
public function initialize(array $config) | |
{ | |
parent::initialize($config); | |
$this->setTable('tasks'); | |
$this->setDisplayField('name'); | |
$this->setPrimaryKey('id'); | |
$this->addBehavior('Timestamp'); | |
$this->belongsTo('Users', [ | |
'foreignKey' => 'user_id', | |
'joinType' => 'INNER' | |
]); | |
$this->belongsTo('Projects', [ | |
'foreignKey' => 'project_id', | |
'joinType' => 'INNER' | |
]); | |
} | |
/** | |
* 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('name', 'create') | |
->notEmpty('name'); | |
$validator | |
->requirePresence('description', 'create') | |
->notEmpty('description'); | |
$validator | |
->integer('points') | |
->requirePresence('points', 'create') | |
->notEmpty('points'); | |
$validator | |
->integer('status') | |
->requirePresence('status', 'create') | |
->notEmpty('status'); | |
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(['user_id'], 'Users')); | |
$rules->add($rules->existsIn(['project_id'], 'Projects')); | |
return $rules; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment