Skip to content

Instantly share code, notes, and snippets.

@dakota
Forked from nwoow/MealsController.php
Last active August 29, 2015 14:18
Show Gist options
  • Save dakota/92644802696e40d44928 to your computer and use it in GitHub Desktop.
Save dakota/92644802696e40d44928 to your computer and use it in GitHub Desktop.
<h1>Assign</h1>
<?php
echo $id;
echo $this->Form->create($meal);
echo $this->Form->hidden('d_id', ['value'=>$id]);
echo $this->Form->input('user_id', [
'options' => $users
]);
echo $this->Form->button(__('Submit'));
echo $this->Form->end();
?>
<?php
// src/Controller/UsersController.php
namespace App\Controller;
use App\Controller\AppController;
use Cake\Event\Event;
use Cake\Network\Exception\NotFoundException;
use Cake\ORM\TableRegistry;
class MealsController extends AppController
{
public function isAuthorized($user)
{
if ($user['role'] === 'dietitian') {
//$this->allow();
return true;
} else {
return false;
}
}
public function index (){
$id = $this->Auth->user('id');
$this->set('meals', $this->Meals->find('all')->where(["d_id" => $id]));
}
public function add()
{
$meal = $this->Meals->newEntity();
if ($this->request->is('post')) {
$article = $this->Meals->patchEntity($meal, $this->request->data);
if ($this->Meals->save($meal)) {
$this->Flash->success(__('Your article has been saved.'));
return $this->redirect(['action' => 'index']);
}
$this->Flash->error(__('Unable to add your article.'));
}
$this->set('meal', $meal);
}
public function assign ($id = null) {
if (!$id) {
throw new NotFoundException(__('Invalid article'));
}
$meal = $this->Meals->get($id);
if ($this->request->is(['post', 'put'])) {
$this->Meals->patchEntity($meal, $this->request->data);
if ($this->Meals->save($meal)) {
$this->Flash->success(__('Your meal has been assigned'));
return $this->redirect(['action' => 'index']);
}
$this->Flash->error(__('Unable to update your article.'));
}
$users = TableRegistry::get('Users')
->find('list', ['valueField' => 'username'])
->select(['id' ,'username'])
->where(['role' => 'patient']);
$this->set('users', $users);
$id1 = $this->Auth->user('id');
$this->set('id', $id1);
$this->set('meal', $meal);
}
}
<?php
// src/Model/Table/UsersTable.php
namespace App\Model\Table;
use Cake\ORM\Table;
use Cake\ORM\RulesChecker;
use Cake\ORM\TableRegistry;
class MealsTable extends Table
{
public function buildRules(RulesChecker $rules)
{
return $rules->add($rules->isUnique(['user_id']));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment