Created
April 7, 2015 06:21
-
-
Save nwoow/3cd6ca3a9d93d168df16 to your computer and use it in GitHub Desktop.
This file contains 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
<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(); | |
?> | |
This file contains 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 | |
// 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); | |
} | |
} |
This file contains 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 | |
// src/Model/Table/UsersTable.php | |
namespace App\Model\Table; | |
use Cake\ORM\Table; | |
use Cake\Validation\Validator; | |
use Cake\ORM\TableRegistry; | |
class MealsTable extends Table | |
{ | |
public function validationDefault(Validator $validator) | |
{ | |
$validator->add('user_id', ['unique' => ['rule' => 'validateUnique' ,'provide' => 'table'] | |
]); | |
return $validator; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment