I want to save the check box CSV in CakePHP 2.6.0.

Asked 2 years ago, Updated 2 years ago, 54 views

Create three tables in the database:

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);
app/Console/cake bake model all
app/Console/cake bake controller all
app/Console/cake bake view all
<?php
App::uses('AppController', 'Controller');

class UsersController extensions 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)){
            through 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)){
            through 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()){
            through 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'));
    }
}
<div class="users form">
<?phpecho$this->Form->create('User');?>
  <fieldset>
    <legend><?phpecho__('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>
  <?phpecho$this->Form->end(__('Submit'));?>
</div>
<div class="actions">
  <h3><?phpecho__('Actions');?>/h3>
  <ul>
    <li><?phpecho$this->Html->link(__('List Users'), array('action'=>'index'));?>/li>
    <li><?phpecho$this->Html->link(__('List Comments'), array('controller'=>'comments', 'action'=>'index'));?>></li>
    <li><?phpecho$this->Html->link(__('New Comment'), array('controller'=>'comments', 'action'=>'add'));?>></li>
    <li><?phpecho$this->Html->link(__('List Groups'), array('controller'=>'groups', 'action'=>'index'));?>>/li>
    <li><?phpecho$this->Html->link(__('New Group'), array('controller'=>'groups', 'action'=>'add'));?>></li>
  </ul>
</div>
<div class="users form">
  <?phpecho$this->Form->create('User');?>
  <fieldset>
    <legend><?phpecho__('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>
  <?phpecho$this->Form->end(__('Submit'));?>
</div>
<div class="actions">
  <h3><?phpecho__('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;';$gt;User.id';';';)')
    <li><?phpecho$this->Html->link(__('List Users'), array('action'=>'index'));?>/li>
    <li><?phpecho$this->Html->link(__('List Comments'), array('controller'=>'comments', 'action'=>'index'));?>></li>
    <li><?phpecho$this->Html->link(__('New Comment'), array('controller'=>'comments', 'action'=>'add'));?>></li>
    <li><?phpecho$this->Html->link(__('List Groups'), array('controller'=>'groups', 'action'=>'index'));?>>/li>
    <li><?phpecho$this->Html->link(__('New Group'), array('controller'=>'groups', 'action'=>'add'));?>></li>
  </ul>
</div>

When saving to the database, the group ID becomes Array and cannot be saved in 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-1104:41:20',
    (int) 4=>'2015-01-1104: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' 1',
        (int) 1 = > '2',
        (int) 2 = > '3'
    ),
    (int) 3=>'2015-01-1104:41:20',
    (int) 4=>'2015-01-1104:41:20'
)
$fieldInsert=array(
    (int) 0 = > '`name`',
    (int) 1 = > '`comment_id`',
    (int) 2 = > '`group_id`',
    (int) 3=>'modified'',
    (int) 4 = > '`created'
)

Can't you save the group ID in CSV as shown below?

$values=array(
    (int) 0 = > 'testuser01',
    (int) 1 = > '1, 2, 3, 4',
    (int) 2 = > '1, 2, 3,',
    (int) 3=>'2015-01-1104:41:20',
    (int) 4=>'2015-01-1104: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-1104:41:20',
    (int) 4=>'2015-01-1104:41:20'
)
$fieldInsert=array(
    (int) 0 = > '`name`',
    (int) 1 = > '`comment_id`',
    (int) 2 = > '`group_id`',
    (int) 3=>'modified'',
    (int) 4 = > '`created'
)

php cakephp

2022-09-30 11:19

2 Answers

First, the definition of the users table is

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
);

It says, but the group_id is probably missing, right?

group_id varchar(50),

Next, regarding the part of the add() function that is running implode, as far as app/View/Users/add.ctp is concerned, isn't it as follows?

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)){
                     :

However, as it is,

$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-1104:41:20',
    (int) 4=>'2015-01-1104:41:20'
)

Therefore, I am not sure if the above changes will work out as expected.


2022-09-30 11:19

Thank you so much.

In the part I mentioned when I asked the question, "group_id" was definitely missing when I created the table.

It's really helpful because I didn't understand for almost a month.
Both CakePHP and PHP are beginners, and this has stopped me forever.

This stack overflow is amazing.
I would like to continue using this site.

Thank you.


2022-09-30 11:19

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.