How do I properly save data to the database?

后端 未结 2 1024
余生分开走
余生分开走 2021-01-03 12:04

I normally save new data to the database like this:

$this->MyTable->set(array(
 \'id\' => $id,
 \'code\' => $temp_code,
 \'status\' => $status         


        
2条回答
  •  -上瘾入骨i
    2021-01-03 12:49

    Posted data example

    Array
    (
        [ModelName] => Array
            (
                [column1] => value
                [column2] => value
                [column3] => value
            )
    
    )
    

    Try this to add

    if ($this->request->is('post')) {
        $this->ModelName->create();
        $this->ModelName->save($this->request->data);
    }
    

    Try this to edit

    if ($this->request->is('post')) {
        $this->ModelName->id = 2;
        $this->ModelName->save($this->request->data);
     }
    

提交回复
热议问题