CakePHP3勉強会 in Fusic ハンズオン資料 part.2

前. CakePHP3勉強会 in Fusic ハンズオン資料 part.1
http://qiita.com/Junkins/items/f1f830b33e52dc1cbc38

## 3. Auth
### ログイン画面の作成

#### 参考URL
http://book.cakephp.org/3.0/en/tutorials-and-examples/blog-auth-example/auth.html

- src/Controller/AppController.php の修正

```bash
command
    vim src/Controller/AppController.php
```

```php
vim
    public function initialize()
    {
        parent::initialize();

        $this->loadComponent('RequestHandler');
        $this->loadComponent('Flash');
        $this->loadComponent('Auth', [
            'authenticate' => [
                'Form' => [
                    'userModel' => 'Accounts',
                    'fields' => [
                        'username' => 'username',
                        'password' => 'password'
                    ]
                ]
            ],
            'loginRedirect' => [
                'controller' => 'Accounts',
                'action' => 'index'
            ],
            'loginAction' => [
                'controller' => 'Accounts',
                'action' => 'login',
            ],
        ]);
    }
```

- src/Controller/AccountsController.php の修正

```bash
command
    vim src/Controller/AccountsController.php
```
```php
vim
    public function login()
    {
        if ($this->request->is('post')) {
            $user = $this->Auth->identify();
            if ($user) {
                $this->Auth->setUser($user);
                return $this->redirect($this->Auth->redirectUrl());
            }
            $this->Flash->error(__('Invalid username or password, try again'));
        }
    }

    public function logout()
    {
        return $this->redirect($this->Auth->logout());
    }
```

- src/Template/Accounts/login.ctp の作成

```bash
command
    vim src/Template/Accounts/login.ctp
```
```html
vim
    <!-- File: src/Template/Users/login.ctp -->
    <div class="users form">
        <?= $this->Flash->render('auth') ?>
        <?= $this->Form->create() ?>
            <fieldset>
                <legend><?= __('Please enter your username and password') ?></legend>
                <?= $this->Form->input('username') ?>
                <?= $this->Form->input('password') ?>
            </fieldset>
        <?= $this->Form->button(__('Login')); ?>
        <?= $this->Form->end() ?>
    </div>
```

- src/Model/Entity/Account.php の修正

```bash
command
    vim src/Model/Entity/Account.php 
```

```bash
vim
    use Cake\Auth\DefaultPasswordHasher;
```
```php
vim
    protected function _setPassword($password)
    {
        return (new DefaultPasswordHasher)->hash($password);
    }
```

## 4. Query Builder
### Query Builderで遊ぼう

#### 参考URL
http://book.cakephp.org/3.0/ja/orm/query-builder.html

- Query Builder デバック

```bash
comannd
    vim src/Controller/AccountsController.php
```

```php
vim
    public function debug()
    {
        $this->autoRender = false;
        $query = $this->Accounts->find();
        debug($query);
    }
```
- サンプル

```php
vim
    $this->autoRender = false;
    $query = $this->Accounts->find();
    $query->where([$this->Accounts->alias() . '.name' => 'AccountA']);
    debug($query);
    // SQLの実行
    debug($query->firstOrFail());
```

```php
vim
    public function sql1()
    {
        $this->autoRender = false;
        $query = $this->Accounts->find();
        $query->where([$this->Accounts->alias() . '.name' => 'AccountA']);
        debug($query);
        // SQLの実行
        debug($query->firstOrFail());
    }
```

```php
vim
    public function sql2()
    {
        $this->autoRender = false;
        $query = $this->Accounts->find();
        $query->matching('Projects', function ($q) {
            return $q->where(['Projects.name' => 'ProjectB']);
        });

        debug($query);
        // SQLの実行
        debug($query->all());
    }
```
```php
vim
    public function sql3()
    {
        $this->autoRender = false;
        $query = $this->Accounts->find();
        $query->matching(
            'Projects.Tasks', function ($q) {
                return $q->where(['Tasks.name' => 'TaskE']);
            }
        );

        debug($query);
        // SQLの実行
        debug($query->all());
    }
```

前. CakePHP3勉強会 in Fusic ハンズオン資料 part.1
http://qiita.com/Junkins/items/f1f830b33e52dc1cbc38