Created
October 12, 2016 16:13
-
-
Save HumanG33k/80f3ec02565037764d49c743b2e1007a to your computer and use it in GitHub Desktop.
This file contains hidden or 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; | |
/** | |
* Cars Model | |
* | |
* @property \Cake\ORM\Association\HasMany $Pilots | |
* | |
* @method \App\Model\Entity\Car get($primaryKey, $options = []) | |
* @method \App\Model\Entity\Car newEntity($data = null, array $options = []) | |
* @method \App\Model\Entity\Car[] newEntities(array $data, array $options = []) | |
* @method \App\Model\Entity\Car|bool save(\Cake\Datasource\EntityInterface $entity, $options = []) | |
* @method \App\Model\Entity\Car patchEntity(\Cake\Datasource\EntityInterface $entity, array $data, array $options = []) | |
* @method \App\Model\Entity\Car[] patchEntities($entities, array $data, array $options = []) | |
* @method \App\Model\Entity\Car findOrCreate($search, callable $callback = null) | |
* | |
* @mixin \Cake\ORM\Behavior\TimestampBehavior | |
*/ | |
class CarsTable extends Table | |
{ | |
/** | |
* Initialize method | |
* | |
* @param array $config The configuration for the Table. | |
* @return void | |
*/ | |
public function initialize(array $config) | |
{ | |
parent::initialize($config); | |
$this->table('cars'); | |
$this->displayField('name'); | |
$this->primaryKey('id'); | |
$this->addBehavior('Timestamp'); | |
$this->hasMany('Pilots', [ | |
'foreignKey' => 'car_id' | |
]); | |
} | |
/** | |
* Default validation rules. | |
* | |
* @param \Cake\Validation\Validator $validator Validator instance. | |
* @return \Cake\Validation\Validator | |
*/ | |
public function validationDefault(Validator $validator) | |
{ | |
$validator | |
->allowEmpty('id', 'create') | |
->add('id', 'unique', ['rule' => 'validateUnique', 'provider' => 'table']); | |
$validator | |
->allowEmpty('name'); | |
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->isUnique(['id'])); | |
return $rules; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment