Skip to content

Commit

Permalink
第6回 Lesson1 サーバサイド
Browse files Browse the repository at this point in the history
  • Loading branch information
ks-ocean committed Jun 25, 2015
1 parent d4ae8a4 commit d4fe1a4
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 1 deletion.
25 changes: 24 additions & 1 deletion app/Controller/TodoListsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,29 @@
class TodoListsController extends AppController {

public function index() {
$res = $this->TodoList->find('all');
$query = array (
'fields' => array (
'TodoList.id',
'TodoList.todo',
'TodoList.status',
'Owner.id',
'Owner.name',
'Assignee.id',
'Assignee.name'
),
'order' => "TodoList.id"
);
$res = $this->TodoList->find('all', $query);
// 整形
if (count($res) > 0) {
$loginUserId = $this->Auth->user()['id'];
foreach ( $res as $key => $row ) {
//「ログインユーザがオーナである」フラグ
$res[$key]['TodoList']['owned'] = $row['Owner']['id'] === $loginUserId;
//「ログインユーザが担当である」フラグ
$res[$key]['TodoList']['assigned'] = $row['Assignee']['id'] === $loginUserId;
}
}
$this->set(compact('res'));
$this->set('_serialize', 'res');
}
Expand All @@ -18,6 +40,7 @@ public function view($id = null) {

public function add() {
$data = $this->request->data;
$data['owner'] = $this->Auth->user()['id'];
$res = $this->TodoList->save($data);
$this->set(compact('res'));
$this->set('_serialize', 'res');
Expand Down
12 changes: 12 additions & 0 deletions app/Controller/UsersController.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,18 @@

class UsersController extends AppController {

public function index() {
$res = $this->User->find('all', array (
'fields' => array (
'User.id',
'User.username',
'User.name'
)
));
$this->set(compact('res'));
$this->set('_serialize', 'res');
}

public function login() {
$user = $this->Auth->user();
$res = array();
Expand Down
10 changes: 10 additions & 0 deletions app/Model/TodoList.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,14 @@
App::uses('AppModel', 'Model');

class TodoList extends AppModel {
public $belongsTo = array (
'Owner' => array (
'className' => 'User',
'foreignKey' => 'owner',
),
'Assignee' => array (
'className' => 'User',
'foreignKey' => 'assignee'
)
);
}

0 comments on commit d4fe1a4

Please sign in to comment.