CakePHP2.6.0でチェックボックスをCSV保存したい。
データベースに以下の3つのテーブルを作る。
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));
cake bake all
app/Console/cake bake model all
app/Console/cake bake controller all
app/Console/cake bake view all
app/Controller/UsersController.php
<?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'));
}
}
app/View/Users/add.ctp
<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>
app/View/Users/edit.ctp
<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>
データベースに保存時グループIDがArrayになりCSVで保存できません。
$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`'
)
以下のようにグループIDもCSVで保存できないのでしょうか。
$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`'
)