<?php
    namespace App\Controller;

    use App\Controller\AppController;
    use Cake\I18n\Time;

    class MatriculasUsersController extends AppController
    {
        public function home()
        {

            $this->paginate = [
                'contain' => ['Matriculas', 'Users'],
                ];

            $matriculasusers = $this->paginate($this->MatriculasUsers);

            $this->set(compact('matriculasusers'));

            $this->loadModel('Users');

            // Get the current time.
            $time = Time::now();
            $this->set(['time' => $time]);

            $this->loadModel('Matriculas');
            $matricula_id = $this->Matriculas->find('list', ['keyField' => 'matricula_id', 'valueField' => 'matricula_id']);
            $this->set(['matricula_id' => $matricula_id]);

            $matriculas = $this->Matriculas->find('list', ['keyField' => 'matricula_id', 'valueField' => 'matricula']);
            $this->set(['matriculas' => $matriculas]);


            $add = $this->MatriculasUsers->newEmptyEntity();
            if($this->request->is('post')){
                $add = $this->MatriculasUsers->patchEntity($add, $this->request->getData());
                if ($this->MatriculasUsers->save($add)){
                    $this->Flash->success(__('Dados registados com sucesso!'));
                    return $this->redirect(['controller' => 'Users', 'action' => 'index']);
                }else{
                    $this->Flash->error(__('Erro ao registar os seus dados!'));
                }
            }
            $this->set(compact('add'));
        }

    }
