開発メモ,主に補足by子連れ親父プログラマー

2011-10-03

CakePHP1.3で作る会員管理システム(18) 編集処理

編集画面

続けて編集画面もやってしまいます。 edit.ctp を開いて、 以下のフォーム開始を、

<?php echo $this->Form->create('Member');?>
以下のように変更します。

<?php echo $this->Form->create('Member', array('type' => 'file'));?>
<div style="display:none;">
    <?php echo $this->Form->input('img1', array('type'=>'hidden')); ?>
    <?php echo $this->Form->input('img2', array('type'=>'hidden')); ?>
</div>

以下のフォームのメインの部分は、

    <?php
        echo $this->Form->input('id');
        echo $this->Form->input('email');
        echo $this->Form->input('password');
        echo $this->Form->input('type_id');
        echo $this->Form->input('birthday');
        echo $this->Form->input('img1');
        echo $this->Form->input('img2');
        echo $this->Form->input('Favorite');
    ?>
add.ctp と同じになるように修正しておきます。

    <?php
        echo $this->Form->input('email',array('label' => array('text'=>'メールアドレス')));
        echo $this->Form->input('password',array('label' => array('text'=>'パスワード')));
        echo $this->Form->input('type_id',array('label' => array('text'=>'種別')));
        echo $this->Form->input('birthday',array('dateFormat'=>'YMD','maxYear'=>date('Y'),'minYear'=>date('Y')-100,'monthNames'=>false, 'label' => array('text'=>'誕生日')));
        echo $this->Form->input('solid1', array('type'=>'file', 'label' => '画像1'));
        echo $this->Form->input('solid2', array('type'=>'file', 'label' => '画像2'));
        echo $this->Form->input('favorites', array('multiple'=>'checkbox', 'label' => '好きな物'));
    ?>

次に、

        echo $this->Form->input('solid1', array('type'=>'file', 'label' => '画像1'));
となっている画像のところを、以下のようにちょっと変えます。

        echo $this->Form->input('solid1', array('type'=>'file', 'label' => '画像1'));
        if (!$this->Form->error('solid1')){
            if ($this->data['Member']['img1']){
                echo '<div class="input img1"><label for="MemberImg1"></label>';
                echo $this->Html->image("/files/".$this->data['Member']['img1']);
                echo '</div>';
            }
        }

出力されるソースはこんなになります。

<div class="input file required">
    <label for="MemberSolid1">画像1</label>
    <input type="file" id="MemberSolid1" name="data[Member][solid1]">
</div>
<div class="input img1">
    <label for="MemberImg1"></label>
    <img alt="" src="/cake/files/xxxxxxxxxxxxxxxxx.jpg">
</div>

続いてコントローラを修正します。

デフォルトでは、

    function edit($id = null) {
        if (!$this->Session->check('Member.id')) {
            $this->redirect(array('action'=>'login'));
        }

        if (!$id && empty($this->data)) {
            $this->Session->setFlash(__('Invalid member', true));
            $this->redirect(array('action' => 'index'));
        }
        if (!empty($this->data)) {
            if ($this->Member->save($this->data)) {
                $this->Session->setFlash(__('The member has been saved', true));
                $this->redirect(array('action' => 'index'));
            } else {
                $this->Session->setFlash(__('The member could not be saved. Please, try again.', true));
            }
        }
        if (empty($this->data)) {
            $this->data = $this->Member->read(null, $id);
        }
        $types = $this->Member->Type->find('list');
        $favorites = $this->Member->Favorite->find('list');
        $this->set(compact('types', 'favorites'));
    }
となっているのを、
    function edit() {
        if (!$this->Session->check('Member.id')) {
            $this->redirect(array('action'=>'login'));
        }
        if (!empty($this->data)) {
            $this->data['Favorite']['Favorite'] = $this->data['Member']['favorites'];
            if ($this->data['Member']['solid1']['type']){
                $this->data['Member']['img1'] = $this->_imgUpload('solid1');
            }
            if ($this->data['Member']['solid2']['type']){
                $this->data['Member']['img2'] = $this->_imgUpload('solid2');
            }
            if ($this->Member->save($this->data)) {
                $this->Session->setFlash(__('会会員情報を更新しました', true));
                $this->redirect(array('action' => 'menu'));
            } else {
                $this->Session->setFlash(__('エラーがあります', true));
            }
        }
        if (empty($this->data)) {
            $this->data = $this->Member->read(null, $this->Session->read('Member.id'));
            if ($this->data['Favorite']){
                foreach ($this->data['Favorite'] as $k => $v){
                    $this->data['Member']['favorites'][] = $v['id'];
                }
            }
        }
        $types = $this->Member->Type->find('list');
        $favorites = $this->Member->Favorite->find('list');
        $this->set(compact('types', 'favorites'));
    }
と編集します。

セッションで認証するので、

function edit($id = null) {
の引数部分はなしにしてます。その代わりに、$this->Member->read に対して、セッションの Member.id を渡しています。

他は add の方と同じですが、 初期に編集画面へ来た時の、DBからデータを読む処理の後に、その「好きな物」データを変換する処理を入れています。上記20〜24行目。 Favorite を favorites に入れ直しています。 これにより、

        echo $this->Form->input('favorites', array('multiple'=>'checkbox', 'label' => '好きな物'));
この書き方のビューに反映されます。

チェックボックスにチェックが入っているを確認します。

このブログを検索

Powered by Blogger.

ラベル

php (17) jQuery (13) OSX (10) MySQL (8) Javascript (7) Postgres (7) port (7) apache (6) Java (3) Smarty (2) html (2) pear (2) FCKEditor (1) XAMPP (1) css (1) git (1) perl (1) ruby (1)

Facebookバナー