1

i am trying to SAVE data of Sizes which is coming from with multiple same name input.

Problem is i am not able to save data. Only last value "sizes" is saving in database.

if ($this->EquipmentType->save($this->request->data['EquipmentType'], false)) {
    $id = $this->EquipmentType->getLastInsertId();
    $this->loadModel('EquipmentTypesSize');
    $sizesArray = $this->request->data['EquipmentType']['size'];
    foreach($sizesArray as $val){
        $data[] = array('EquipmentTypesSize' => array('sizes' =>  $val));
    }
    $this->request->data['EquipmentTypesSize']['equipment_type_id'] = $id;
    $this->EquipmentTypesSize->set($this->request->data);
    $this->EquipmentTypesSize->save($this->request->data['EquipmentTypesSize']);
    $this->Session->setFlash('Equipment type has been added successfully ', 'default', 'success');
    $this->redirect(array('controller' => 'equipments', 'action' => 'listequipmenttypes', 'admin' => true));
}

I want to save this as

equipment_type_id | size1
equipment_type_id | size2
equipment_type_id | size3
1
  • book.cakephp.org/2.0/en/models/… : "If you want to insert a new row instead of updating an existing one you should always call create() first. This avoids conflicts with possible prior save calls in callbacks or other places." Commented Jul 10, 2014 at 19:03

1 Answer 1

4

There are several issues - the call to create that Kai mentioned is one of them.

First, here's your code, but I've added comments in places where I see obvious issues (I many not have caught all of them)

<?php 
if ($this->EquipmentType->save($this->request->data['EquipmentType'], false)) {
    $id = $this->EquipmentType->getLastInsertId();
    $this->loadModel('EquipmentTypesSize');
    $sizesArray = $this->request->data['EquipmentType']['size'];
    foreach($sizesArray as $val){
        $data[] = array('EquipmentTypesSize' => array('sizes' =>  $val));
        // WHERE IS THIS $data VARIABLE USED? It seems it's never used?
    }
    $this->request->data['EquipmentTypesSize']['equipment_type_id'] = $id;
    $this->EquipmentTypesSize->set($this->request->data); // NO NEED TO CALL SET HERE - YOU ALREADY PASS DATA IN AS A PARAM TO SAVE
    // YOU MUST CALL CREATE BEFORE ADDING A NEW RECORD
    $this->EquipmentTypesSize->save($this->request->data['EquipmentTypesSize']);
    // I DON'T THINK $this->request->data['EquipmentTypesSize'] HOLDS THE VALUE YOU THINK IT DOES AT THIS POINT
    $this->Session->setFlash('Equipment type has been added successfully ', 'default', 'success');
    $this->redirect(array('controller' => 'equipments', 'action' => 'listequipmenttypes', 'admin' => true));
}

Now, it's hard to tell exactly what you're wanting to do, but here's my attempt to write what you intended. If it doesn't work, it should hopefully set you on the right track.

if ($this->EquipmentType->save($this->request->data['EquipmentType'], false)) {
    $id = $this->EquipmentType->getLastInsertId();
    $this->loadModel('EquipmentTypesSize');
    $sizesArray = $this->request->data['EquipmentType']['size'];
    foreach($sizesArray as $val){
        $this->EquipmentTypesSize->create();
        $this->EquipmentTypesSize->save(array(
            'sizes' =>  $val,
            'equipment_type_id' => $id,
        ));
    }
    $this->Session->setFlash('Equipment type has been added successfully ', 'default', 'success');
    $this->redirect(array('controller' => 'equipments', 'action' => 'listequipmenttypes', 'admin' => true));
}

Lastly, two things:

  1. Really, you should push as much of that logic into your EquipmentType model as possible. So you might create a saveWithSizes method in your EquipmentType model that holds most of that code.

  2. You should look into Cake's saveAll method (http://book.cakephp.org/2.0/en/models/saving-your-data.html#model-saveall-array-data-null-array-options-array). Ideally, you'd set your form data up so that you could just call saveAll and have Cake handle it all automatically. That may or may not be possible for your situation though.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.