tags:

views:

66

answers:

1

hi,

I have two tables,Forms and Attributes. I'm tring to retrieve the last inserted id from the Forms table and insert it to the form_id column of the Attributes table, along with the other field columns.

Earlier I retrieved the form Id from the Forms table and used it to update the value of the Form name column. It worked fine.The code for that is given below:

function saveFormName($data)
    {   
       $this->data['Form']['formname']=$data['Form']['formname'];
       $this->data['Form']['id']=$this->find('all', array(
            'fields' => array('Form.id'),
            'order' => 'Form.id DESC'
                                     ));
    $this->id=$this->data['Form']['id'][0];
    $this->saveField('name',$this->data['Form']['formname']);    
    }

But When I tried to do it in a similar way for updating the attributes table,the row is not saved in the database,since the value of $this->data['Form']['id'][0] is an 'Array'. Even in the saveFormName function, the value of $this->data['Form']['id'][0] is an 'Array',but the form name gets updated correctly. Someone explain me the concept.

function saveFieldEntries($data)
{ 
     $this->data['Form']['id']=$this->find('all', array(
            'fields' => array('Form.id'),
            'order' => 'Form.id DESC'
                                     ));
          $this->data['Attribute']['form_id'] = $this->data['Form']['id'][0];

          $this->data['Attribute']['label']= 'Label';
          $this->data['Attribute']['size']='20';
          $this->data['Attribute']['type']=$data['Attribute']['type'];
          $this->data['Attribute']['sequence_no'] = $data['Attribute']['sequence_no'];
          $this->Attribute->save($this->data);
}

EDIT:

Ok, here is the corresponding code in the controller.

 function insertFormName()
    {    
 $this->data['Form']['formname']=$this->params['form']['formname'];
 $this->Form->saveFormName($this->data);
    }


    function insertFieldEntry()
    {
 $this->data['Attribute']['type']=$this->params['form']['type'];
 $this->data['Attribute']['sequence_no'] = $this->params['form']['sequence_no'];
 $this->Form->saveFieldEntries($this->data);
    }

The parameters form name,type and sequence no are passed to the controller from the corresponding view file.

A: 

$this->data['Form']['id'][0] holds an array because find('all') returns an array.

So if you need first ID from this array, you need to pick it properly in function saveFieldEntries:

...
$this->data['Attribute']['form_id'] = $this->data['Form']['id'][0]['Form']['id'];
...
Sergei
Thank you. It works fine..
Angeline Aarthi