PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Tuesday, January 11, 2022

[FIXED] CakePHP save multiple data

 January 11, 2022     cakephp, model, model-view-controller, view     No comments   

Issue

I wrote a custom view which looks like this: http://img405.imageshack.us/i/taula.jpg/

This is my controller function with NO save data implemented yet: http://pastebin.com/cU5rprFB

And here is my view: http://pastebin.com/4bYLPp4z

I thought that writing form imput like this:

<td>
<input name="data[Linea][0][proyecto_id]" type="hidden" value=" <?php echo $proyecto['Proyecto']['id'] ?>" />
<input name="data[Linea][0][hito_id]" type="hidden" value=" <?php echo $proyecto['Hito']['id'] ?>" />
<input name="data[Linea][0][tarea_id]" type="hidden" value=" <?php echo $proyecto['Tarea']['id'] ?>" />
<input name="data[Linea][0][total_horas]" type="text" id="LineaTotalHotas" value="" >
</td>

would be sufficient... but it isn't. In the debug_kit I see that hidden data is OK but the data from the input is lost...

Does anyone have an example or something to help me?


UPDATE: I'm trying something like that in my controller:

function addhoras() {
    if (!empty($this->data)) {
        xdebug_break();
        foreach($this->data['Linea'] as $l) {
            if ( ($l['total_horas'] != 0) && ( $l['total_horas']!=NULL ) ) {
                $this->Linea->create();
                if ( $this->Linea->save($l) ) {

                } else {
                    $this->Session->setFlash(__('Arazonbat eon dek', true));
                }
            }
        }
        //$this->redirect(array('action' => 'addhoras'));
    }

But I'm getting a setflash on the if ( $this->Linea->save($l) ) line... so it is not saving any data... this is the var_dump($l):

array
'proyecto_id' => string ' 1' (length=2)

'hito_id' => string ' 3' (length=2)

'usuario_id' => string ' 1' (length=2)

'fecha' => string '2011-01-01 ' (length=11)

'total_horas' => string '33' (length=2)

Solution

Be sure you construct your array correctly http://book.cakephp.org/view/83/hasAndBelongsToMany-HABTM

And if you are saving related data remember to use saveAll instead of save.

Make sure that the array you pass to the "save" method has the typical Cake structure. This would be something like this:

array
(
    [Modelo] => array
    (
         [celda] => valor
    )
)

And remember that if you want to save related data (either HasMany or HasAndBelongsToMany ..) you must use "saveAll" instead of "save"

http://book.cakephp.org/view/83/hasAndBelongsToMany-HABTM



Answered By - elboletaire
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing