

データベースに以下の3つのテーブルを作る。

[code]

    create table users(
        id int not null auto_increment primary key,
        name varchar(50),
        comment_id varchar(50),
        created datetime not null,
        modified datetime not null);
    
    create table comments(
        id int not null auto_increment primary key,
        name varchar(50));
    
    create table groups(
        id int not null auto_increment primary key,
        name varchar(50));
    
[/code]

# cake bake all

[code]

    app/Console/cake bake model all
    app/Console/cake bake controller all
    app/Console/cake bake view all
    
[/code]

# app/Controller/UsersController.php

[code]

    <?php
    App::uses('AppController', 'Controller');
    
    class UsersController extends AppController {
    
        public $components = array('Paginator');
    
        public function index() {
            $this->User->recursive = 0;
            $this->set('users', $this->Paginator->paginate());
        }
    
        public function view($id = null) {
            if (!$this->User->exists($id)) {
                throw new NotFoundException(__('Invalid user'));
            }
            $options = array('conditions' => array('User.' . $this->User->primaryKey => $id));
            $this->set('user', $this->User->find('first', $options));
        }
    
        public function add() {
            if ($this->request->is('post')) {
                $this->User->create();
    
                //Edit from here
                if ($this->request->data['User']['comment_id']['group_id'] !== '') {
                    $csv = implode(',', $this->request->data['User']['comment_id']['group_id']);
                    $this->request->data['User']['comment_id']['group_id'] = $csv;
                } else {
                    $this->request->data['User']['comment_id']['group_id'] = '';
                }
    
                //Edit far
                if ($this->User->save($this->request->data)) {
                    $this->Session->setFlash(__('The user has been saved.'));
                    return $this->redirect(array('action' => 'index'));
                } else {
                    $this->Session->setFlash(__('The user could not be saved. Please, try again.'));
                }
            }
            $comments = $this->User->Comment->find('list');
            $groups = $this->User->Group->find('list');
            $this->set(compact('comments', 'groups'));
        }
    
        public function edit($id = null) {
            if (!$this->User->exists($id)) {
                throw new NotFoundException(__('Invalid user'));
            }
            if ($this->request->is(array('post', 'put'))) {
    
                //Edit from here
                if ($this->request->data['User']['comment_id']['group_id'] !== '') {
                    $csv = implode(',', $this->request->data['User']['comment_id']['group_id']));
                    $this->request->data['User']['comment_id']['group_id'] = $csv;
                }
    
                //Edit far
                if ($this->User->save($this->request->data)) {
                    $this->Session->setFlash(__('The user has been saved.'));
                    return $this->redirect(array('action' => 'index'));
                } else {
                    $this->Session->setFlash(__('The user could not be saved. Please, try again.'));
                }
            } else {
                $options = array('conditions' => array('User.' . $this->User->primaryKey => $id));
                $this->request->data = $this->User->find('first', $options);
            }
            $comments = $this->User->Comment->find('list');
            $groups = $this->User->Group->find('list');
            $this->set(compact('comments', 'groups'));
        }
    
        public function delete($id = null) {
            $this->User->id = $id;
            if (!$this->User->exists()) {
                throw new NotFoundException(__('Invalid user'));
            }
            $this->request->allowMethod('post', 'delete');
            if ($this->User->delete()) {
                $this->Session->setFlash(__('The user has been deleted.'));
            } else {
                $this->Session->setFlash(__('The user could not be deleted. Please, try again.'));
            }
            return $this->redirect(array('action' => 'index'));
        }
    }
    
[/code]

# app/View/Users/add.ctp

[code]

    <div class="users form">
    <?php echo $this->Form->create('User'); ?>
      <fieldset>
        <legend><?php echo __('Add User'); ?></legend>
        <?php
          echo $this->Form->input('name');
          echo $this->Form->input('comment_id',
                                  array(
                                      'type' => 'select',
                                      'options' => $comments,
                                      'multiple' => 'checkbox'
                                  ));
          echo $this->Form->input('group_id',
                                  array(
                                      'type' => 'select',
                                      'options' => $groups,
                                      'multiple' => 'checkbox'
                                  ));
        ?>
      </fieldset>
      <?php echo $this->Form->end(__('Submit')); ?>
    </div>
    <div class="actions">
      <h3><?php echo __('Actions'); ?></h3>
      <ul>
        <li><?php echo $this->Html->link(__('List Users'), array('action' => 'index')); ?></li>
        <li><?php echo $this->Html->link(__('List Comments'), array('controller' => 'comments', 'action' => 'index')); ?> </li>
        <li><?php echo $this->Html->link(__('New Comment'), array('controller' => 'comments', 'action' => 'add')); ?> </li>
        <li><?php echo $this->Html->link(__('List Groups'), array('controller' => 'groups', 'action' => 'index')); ?> </li>
        <li><?php echo $this->Html->link(__('New Group'), array('controller' => 'groups', 'action' => 'add')); ?> </li>
      </ul>
    </div>
    
[/code]

# app/View/Users/edit.ctp

[code]

    <div class="users form">
      <?php echo $this->Form->create('User'); ?>
      <fieldset>
        <legend><?php echo __('Edit User'); ?></legend>
        <?php
          echo $this->Form->input('id');
          echo $this->Form->input('name');
          echo $this->Form->input('comment_id',
                                  array(
                                      'type' => 'select',
                                      'options' => $comments,
                                      'multiple' => 'checkbox',
                                      'value' => explode(',', $this->data['User']['comment_id'])
                                  ));
          echo $this->Form->input('group_id',
                                  array(
                                      'type' => 'select',
                                      'options' => $groups,
                                      'multiple' => 'checkbox',
                                      'value' => explode(',', $this->data['User']['group_id'])
                                  ));
        ?>
      </fieldset>
      <?php echo $this->Form->end(__('Submit')); ?>
    </div>
    <div class="actions">
      <h3><?php echo __('Actions'); ?></h3>
      <ul>
        <li><?php echo $this->Form->postLink(__('Delete'), array('action' => 'delete', $this->Form->value('User.id')), array(), __('Are you sure you want to delete', $this->Form->value('User.id'))); ?></li>
        <li><?php echo $this->Html->link(__('List Users'), array('action' => 'index')); ?></li>
        <li><?php echo $this->Html->link(__('List Comments'), array('controller' => 'comments', 'action' => 'index')); ?> </li>
        <li><?php echo $this->Html->link(__('New Comment'), array('controller' => 'comments', 'action' => 'add')); ?> </li>
        <li><?php echo $this->Html->link(__('List Groups'), array('controller' => 'groups', 'action' => 'index')); ?> </li>
        <li><?php echo $this->Html->link(__('New Group'), array('controller' => 'groups', 'action' => 'add')); ?> </li>
      </ul>
    </div>
    
[/code]

データベースに保存時グループIDがArrayになりCSVで保存できません。

[code]

    $values = array(
        (int) 0 => 'testuser01',
        (int) 1 => '1,2,3,4',
        (int) 2 => array(
            (int) 0 => '1',
            (int) 1 => '2',
            (int) 2 => '3'
        ),
        (int) 3 => '2015-01-11 04:41:20',
        (int) 4 => '2015-01-11 04:41:20'
    )
    $id = null
    $count = (int) 5
    $i = (int) 5
    $valueInsert = array(
        (int) 0 => ''testuser01'',
        (int) 1 => ''1,2,3,4'',
        (int) 2 => array(
            (int) 0 => ''1'',
            (int) 1 => ''2'',
            (int) 2 => ''3''
        ),
        (int) 3 => ''2015-01-11 04:41:20'',
        (int) 4 => ''2015-01-11 04:41:20''
    )
    $fieldInsert = array(
        (int) 0 => '`name`',
        (int) 1 => '`comment_id`',
        (int) 2 => '`group_id`',
        (int) 3 => '`modified`',
        (int) 4 => '`created`'
    )
    
[/code]

以下のようにグループIDもCSVで保存できないのでしょうか。

[code]

    $values = array(
        (int) 0 => 'testuser01',
        (int) 1 => '1,2,3,4',
        (int) 2 => '1,2,3,',
        (int) 3 => '2015-01-11 04:41:20',
        (int) 4 => '2015-01-11 04:41:20'
    )
    $id = null
    $count = (int) 5
    $i = (int) 5
    $valueInsert = array(
        (int) 0 => ''testuser01'',
        (int) 1 => ''1,2,3,4'',
        (int) 2 => ''1,2,3,'',
        (int) 3 => ''2015-01-11 04:41:20'',
        (int) 4 => ''2015-01-11 04:41:20''
    )
    $fieldInsert = array(
        (int) 0 => '`name`',
        (int) 1 => '`comment_id`',
        (int) 2 => '`group_id`',
        (int) 3 => '`modified`',
        (int) 4 => '`created`'
    )
    
[/code]


CC BY-SA 3.0
2015-01-11T05:54:51.587
4396
2015-01-11T23:42:39.483
2015-01-11T10:10:31.123
7303
question
php cakephp
CakePHP2.6.0でチェックボックスをCSV保存したい。


まず、users テーブルの定義が、

[code]

    create table users(
      id int not null auto_increment primary key,
      name varchar(50),
      comment_id varchar(50),
      created datetime not null,
      modified datetime not null
    );
    
[/code]

となっていますが、おそらく group_id が抜けていますよね？

[code]

    group_id varchar(50),
    
[/code]

次に、`add()` function 内で implode を実行している部分ですが、`app/View/Users/add.ctp`
を見る限り、以下の様になるのではないでしょうか？

[code]

    public function add() {
      if ($this->request->is('post')) {
        $this->User->create();
    
        // comment_id
        $this->request->data['User']['comment_id']
            = implode(',', $this->request->data['User']['comment_id']);
        // group_id
        $this->request->data['User']['group_id']
            = implode(',', $this->request->data['User']['group_id']);
    
        if ($this->User->save($this->request->data)) {
                         :
    
[/code]

ただ、現状で、

[code]

    $values = array(
        (int) 0 => 'testuser01',
        (int) 1 => '1,2,3,4',
        (int) 2 => array(
            (int) 0 => '1',
            (int) 1 => '2',
            (int) 2 => '3'
        ),
        (int) 3 => '2015-01-11 04:41:20',
        (int) 4 => '2015-01-11 04:41:20'
    )
    
[/code]

となっていますので、前述の変更で期待通りの結果になるのかは分かりません。


CC BY-SA 3.0
2015-01-11T09:08:02.197
4399
2015-01-11T09:57:56.467
2015-01-11T09:57:56.467
4396
answer


本当に、本当にありがとうございました。

質問時に記述した部分で、テーブル作成時「group_id」が確かに抜けていました。

1か月近く、分からなくて、困っていましたので、本当に助かります。  
CakePHPもPHPも初心者なもので、こんな事で、いつまでも止まっていました。

このスタック・オーバーフローって凄いところですね。  
今後もこのサイトを利用させて頂きたいと思います。

ありがとうございました。


CC BY-SA 3.0
2015-01-11T23:42:39.483
4423
2015-01-11T23:42:39.483
7303
4396
answer