cakephp2 検索の基本

##環境
php5.4 cakephp2.2

##実際のコード
```php5:BoardsController.php
<?php

class BoardsController extends AppController
{
    public $name = 'Board';

    public function index()
    {
        $data = null;
        if (!empty($this->data['Board']['name'])) {
            $data = $this->Board->findAllByName($this->data['Board']['name']);
        } else {
            $data = $this->Board->find('all');
        }
        $this->set('data', $data);
    }
}

```

```php5:index.ctp
<h1>送信フォーム・サンプル</h1>

<?php
echo $this->Form->create(false,array('type'=>'post','url'=>'index'));
echo $this->Form->text("Board.name");
echo $this->Form->submit("送信");
echo $this->Form->end();
?>

<br /><hr><br />

<table>
<?php

for($i = 0;$i < count($data);$i++){
  $arr = $data[$i]['Board'];
  echo "<tr><td>{$arr['id']}</td>";
  echo "<td>{$arr['name']}</td>";
  echo "<td>{$arr['title']}</td>";
  echo "<td>{$arr['content']}</td></tr>";
}

?>
</table>
```
####実行結果
![name2.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/280325/aab00877-5c10-b14f-6d83-d212d3f77d0d.png)
###削除
```php5:BoardsController.php
<?php

class BoardsController extends AppController
{
    public $name = 'Board';

    public function index()
    {
        $data = null;
        if (!empty($this->data['Board']['id'])) {
            $data = $this->Board->findAllByName($this->data['Board']['id']);
        } else {
            $data = $this->Board->find('all');
        }
        $this->set('data', $data);
    }

    public function delRecord()
    {
        if(!empty($this->data['Board']['id']))
        {
            $this->Board->delete($this->data['Board']['id']);
        }
        $this->redirect('index');
    }
}

```

```php5:index.ctp
<h1>送信フォーム・サンプル</h1>

<?php
<h1>送信フォーム・サンプル</h1>

<?php
echo $this->Form->create(false,array('type'=>'post','url'=>'delRecord'));
echo $this->Form->text("Board.id");
echo $this->Form->submit("送信");
echo $this->Form->end();
?>

<br /><hr><br />

<table>
<?php

for($i = 0;$i < count($data);$i++){
  $arr = $data[$i]['Board'];
  echo "<tr><td>{$arr['id']}</td>";
  echo "<td>{$arr['name']}</td>";
  echo "<td>{$arr['title']}</td>";
  echo "<td>{$arr['content']}</td></tr>";
}

?>
</table>
```
####結果(1を入力してボタンをクリックした後)
![1del.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/280325/40b5fa04-72de-c98c-654b-f2bd1be8e46d.png)

####保存について
```php5:BoardsController.php
<?php

class BoardsController extends AppController
{
    public $name = 'Board';

    public function index()
    {
        $data = null;
        if (!empty($this->data['Board']['id'])) {
            $data = $this->Board->findAllByName($this->data['Board']['id']);
        } else {
            $data = $this->Board->find('all');
        }
        $this->set('data', $data);
    }

    public function delRecord()
    {
        if(!empty($this->data['Board']['id']))
        {
            $this->Board->delete($this->data['Board']['id']);
        }
        $this->redirect('index');
    }

    public function updateRecord()
    {
        if(!empty($this->data['Board']['id']))
        {
            $this->Board->save($this->data);
        }
        $this->redirect('index');
    }
}
```
```php5:index.ctp
<h1>送信フォーム・サンプル</h1>

<?php
echo $this->Form->create(false,array('type'=>'post','url'=>'updateRecord'));
echo $this->Form->text("Board.id");
echo $this->Form->text("Board.name");
echo $this->Form->text("Board.title");
echo $this->Form->text("Board.content");
echo $this->Form->submit("送信");
echo $this->Form->end();
?>

<br /><hr><br />

<table>
<?php

for($i = 0;$i < count($data);$i++){
  $arr = $data[$i]['Board'];
  echo "<tr><td>{$arr['id']}</td>";
  echo "<td>{$arr['name']}</td>";
  echo "<td>{$arr['title']}</td>";
  echo "<td>{$arr['content']}</td></tr>";
}

?>
</table>
```
####結果
![save1.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/280325/1e7b46b5-0593-a980-38d2-ea8cc1d7ab62.png)

###特定のレコード変更
```php5:BoardsController.php
<?php

class BoardsController extends AppController
{
    public $name = 'Board';

    public function index()
    {
        $data = null;
        if (!empty($this->data['Board']['id'])) {
            $data = $this->Board->findAllByName($this->data['Board']['id']);
        } else {
            $data = $this->Board->find('all');
        }
        $this->set('data', $data);
    }

    public function delRecord()
    {
        if(!empty($this->data['Board']['id']))
        {
            $this->Board->delete($this->data['Board']['id']);
        }
        $this->redirect('index');
    }

    public function updateRecord()
    {
        if(!empty($this->data['Board']['id']))
        {
            $this->Board->save($this->data);
            $this->Board->id = $this->data['Board']['id'];
            $this->Board->saveField('content', $this->data['Board']['content']);
        }
        $this->redirect('.');
    }
}
```

```php5:index.ctp
<h1>送信フォーム・サンプル</h1>

<?php
echo $this->Form->create(false,array('type'=>'post','url'=>'updateRecord'));
echo $this->Form->text("Board.id");
echo $this->Form->text("Board.content");
echo $this->Form->submit("送信");
echo $this->Form->end();
?>

<br /><hr><br />

<table>
<?php

for($i = 0;$i < count($data);$i++){
  $arr = $data[$i]['Board'];
  echo "<tr><td>{$arr['id']}</td>";
  echo "<td>{$arr['name']}</td>";
  echo "<td>{$arr['title']}</td>";
  echo "<td>{$arr['content']}</td></tr>";
}

?>
</table>
```

####実行結果
![save2.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/280325/f2d2af43-b1da-e5e5-8c35-25509c6692d4.png)

##参考本:
cakephp2.1オープンソース徹底活用
https://www.amazon.co.jp/dp/B00XZTYMP2/ref=dp-kindle-redirect?_encoding=UTF8&btkr=1

###条件に合うレコード変更
```php5:BoardsController.php
<?php

class BoardsController extends AppController
{
    public $name = 'Board';

    public function index()
    {
        $data = null;
        if (!empty($this->data['Board']['id'])) {
            $data = $this->Board->findAllByName($this->data['Board']['id']);
        } else {
            $data = $this->Board->find('all');
        }
        $this->set('data', $data);
    }

    public function delRecord()
    {
        if(!empty($this->data['Board']['id']))
        {
            $this->Board->delete($this->data['Board']['id']);
        }
        $this->redirect('index');
    }

    public function updateRecord()
    {
            if (!empty($this->data['Board']['name'])){
              $arr1 = array('Board.name'=>$this->data['Board']['name']);
              $arr2 = array('Board.title'=>"'{$this->data['Board']['title']}'");
              $this->Board->updateAll($arr2,$arr1);
            }
            $this->redirect('.');
          
        }
}
```

```php5:index.ctp
<h1>送信フォーム・サンプル</h1>

<?php
echo $this->Form->create(false,array('type'=>'post','url'=>'updateRecord'));
echo $this->Form->text("Board.name");
echo $this->Form->text("Board.title");
echo $this->Form->submit("送信");
echo $this->Form->end();
?>

<br /><hr><br />

<table>
<?php

for($i = 0;$i < count($data);$i++){
  $arr = $data[$i]['Board'];
  echo "<tr><td>{$arr['id']}</td>";
  echo "<td>{$arr['name']}</td>";
  echo "<td>{$arr['title']}</td>";
  echo "<td>{$arr['content']}</td></tr>";
}

?>
</table>
```

####実行結果
![save3.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/280325/b1eb6281-a040-2b5f-878a-62ef1be92624.png)

####オマケ
参考文献のリスト3-39は正しくはこう

```php5:BoardController.php
<?php

class BoardsController extends AppController
{
    public $name = 'Board';

    public function index(){
        $data = $this->Board->find('all');
        $this->set('data',$this->Board->queryData);
      }

    public function delRecord()
    {
        if (!empty($this->data['Board']['id'])) {
            $this->Board->delete($this->data['Board']['id']);
        }
        $this->redirect('index');
    }

    public function updateRecord()
    {
        if (!empty($this->data['Board']['name'])) {
            $arr1 = array('Board.name' => $this->data['Board']['name']);
            $arr2 = array('Board.title' => "'{$this->data['Board']['title']}'");
            $this->Board->updateAll($arr2, $arr1);
        }
        $this->redirect('.');
    }
}
```

####結果
![beforefind.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/280325/83ee725d-3079-59f8-07de-b91831b797c1.png)

リスト3-45　warningの消し方　参考リンク : https://www.grafikart.fr/forum/topics/19174

```php5:Board.php
<?php
class Board extends AppModel {
  public $name = 'Board';
  public $delname = 'hanako';

  public function afterFind($results = array(),$primary = false){
    for($i = count($results) - 1;$i >= 0;$i--){
      if($results[$i]['Board']['name'] == $this->delname){
        array_splice($results,$i,1,null);
      }
    }
    return $results;
  }
}
?>
```
####結果
![afterfind2.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/280325/e09f2af1-598a-d456-bfc7-ad92ced528d5.png)