<?php
namespace App\Model\Table;

use Cake\ORM\Query;
use Cake\ORM\RulesChecker;
use Cake\ORM\Table;
use Cake\Validation\Validator;

/**
 * Schooltypes Model
 *
 * @property \Cake\ORM\Association\HasMany $Departments
 * @property \Cake\ORM\Association\HasMany $Examinations
 * @property \Cake\ORM\Association\HasMany $Imports
 * @property \Cake\ORM\Association\HasMany $Subjects
 * @property \Cake\ORM\Association\HasMany $Supervisings
 *
 * @method \App\Model\Entity\Schooltype get($primaryKey, $options = [])
 * @method \App\Model\Entity\Schooltype newEntity($data = null, array $options = [])
 * @method \App\Model\Entity\Schooltype[] newEntities(array $data, array $options = [])
 * @method \App\Model\Entity\Schooltype|bool save(\Cake\Datasource\EntityInterface $entity, $options = [])
 * @method \App\Model\Entity\Schooltype patchEntity(\Cake\Datasource\EntityInterface $entity, array $data, array $options = [])
 * @method \App\Model\Entity\Schooltype[] patchEntities($entities, array $data, array $options = [])
 * @method \App\Model\Entity\Schooltype findOrCreate($search, callable $callback = null, $options = [])
 *
 * @mixin \Cake\ORM\Behavior\TimestampBehavior
 */
class SchooltypesTable extends Table
{

    /**
     * Initialize method
     *
     * @param array $config The configuration for the Table.
     * @return void
     */
    public function initialize(array $config)
    {
        parent::initialize($config);

        $this->table('schooltypes');
        $this->displayField('name');
        $this->primaryKey('id');

        $this->addBehavior('Timestamp');

        $this->hasMany('Departments', [
            'foreignKey' => 'schooltype_id'
        ]);
        $this->hasMany('Examinations', [
            'foreignKey' => 'schooltype_id'
        ]);
        $this->hasMany('Imports', [
            'foreignKey' => 'schooltype_id'
        ]);
        $this->hasMany('Subjects', [
            'foreignKey' => 'schooltype_id'
        ]);
        $this->hasMany('Supervisings', [
            'foreignKey' => 'schooltype_id'
        ]);
    }

    /**
     * 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('code', 'create')
            ->notEmpty('code');

        return $validator;
    }
}
