<?php
namespace App\Model\Table;

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

class TabUserTable extends Table
{

    public function initialize(array $config)
    {
        parent::initialize($config);

        $this->setTable('tabUser');
        $this->setDisplayField('title');
        $this->setPrimaryKey('id');

        $this->addBehavior('Timestamp');

        $this->belongsTo('TabUser', [
            'foreignKey' => 'user_id'
        ]);
        
    }

    public function validationDefault(Validator $validator)
    {
        $validator
            ->scalar('title')
            ->allowEmpty('title', 'create');

        return $validator;
    }
    

    public function getUser($sel=true) {
        $result = $this->find()
                ->select(['TabUser.id', 'TabUser.name'])
                ->hydrate(false)
                ->toArray();
        $userGroups = [];
        if($sel) {
            $userGroups[''] = __('Select');
        }
        foreach($result as $row) {
            $userGroups[$row['id']] = $row['name'];            
        }
        return $userGroups;
    }
    
}
