简体   繁体   中英

How can I save this kind of data in cakePHP 2.3?

First of all am using cakePHP 2.3 framework and I have created form where I also have some relationships. The good thing is that I'm able to save the data, if the fields are not in array form like below.

<input type="text" name="data['Academicrecord']['school'][]" />
<input type="text" name="data['Academicrecord']['award'][]" />
<input type="text" name="data['Academicrecord']['from'][]" />
<input type="text" name="data['Academicrecord']['to'][]" />

But I get this error when I submit the form data above.

Error: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'Array' in 'field list'

Look at the info generated by the cakePHP in built debug function.

debug($this->request->data);

'Academicrecord' => array(
    'school' => array(
        (int) 0 => 'Kansanga Primary School',
        (int) 1 => 'Tropical High School'
    ),
    'award' => array(
        (int) 0 => 'P.L.E',
        (int) 1 => 'U.C.E'
    ),
    'from' => array(
        (int) 0 => '1997',
        (int) 1 => '2003'
    ),
    'to' => array(
        (int) 0 => '2002',
        (int) 1 => '2006'
    )
)

On my way to solving this problem, I thought I could use the for loop to iterate the submitted items and then send them(data) to the model. Unfortunately my for loops cannot iterate the until the second row.

          echo  "The counts: ".count( $this->request->data['Academicrecord']['school']).'<br/>';

       for($i=0;$i<count( $this->request->data['Academicrecord']['school']);$i++){

         $this->request->data['Academicrecord']['staff_id'] = $this->Staff->id;
         echo "{$i} :".$this->request->data['Academicrecord']['school'] =   $this->request->data['Academicrecord']['school'][$i];
         echo ' '.$this->request->data['Academicrecord']['award']  =   $this->request->data['Academicrecord']['award'][$i];
         echo ' '.$this->request->data['Academicrecord']['from']   =   $this->request->data['Academicrecord']['from'][$i];
         echo ' '.$this->request->data['Academicrecord']['to']     =   $this->request->data['Academicrecord']['to'][$i];
         echo "<br/>";

           //$this->Staff->Academicrecord->save($this->request->data);
       }

Sample result after submitting.

The counts of rows: 2

Row 0 :Kansanga Primary School PLE 1990 2004

In loop you need to create the data like:

echo  "The counts: ".count( $this->request->data['Academicrecord']['school']).'<br/>';

       for($i=0;$i<count( $this->request->data['Academicrecord']['school']);$i++){

         $this->request->data['Academicrecord']['staff_id'] = $this->Staff->id;
         echo "{$i} :".$this->request->data['Academicrecord']['school'] =   $this->request->data['Academicrecord']['school'][$i];
         echo ' '.$this->request->data['Academicrecord']['award']  =   $this->request->data['Academicrecord']['award'][$i];
         echo ' '.$this->request->data['Academicrecord']['from']   =   $this->request->data['Academicrecord']['from'][$i];
         echo ' '.$this->request->data['Academicrecord']['to']     =   $this->request->data['Academicrecord']['to'][$i];
         //echo "<br/>";
         **$this->Staff->Academicrecord->create();**

         $this->Staff->Academicrecord->save($this->request->data);
       }

And i am not sure why you are using $this->Staff->Academicrecord instead of $this->Academicrecord.

I finally got the answer from here. http://topdsoft.com/stories/view/186

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM