我想向数据库添加多条记录,这些记录是根据cakephp2.6.1中的某些选择动态生成的

问题描述 投票:0回答:1

我正在尝试根据选择和输入动态生成学期行,并将所有行存储在同一位置。

<pre>
public function saveData() {
        $this->autoRender = false; // Disable view rendering

        if ($this->request->is('post')) {
            $data = $this->request->data;

            // Assuming you have a model called Data in your application
            $this->loadModel('ETest');

            foreach ($data as $row) {
                $this->ETest->create();
                $entity = array(
                    'cycle' => $row['cycle'],
                    'course' => $row['course'],
                    'year' => $row['year'],
                    'semester' => $row['semester']
                );
                $this->ETest->save($entity);
            }

            $response = array('message' => 'Data saved successfully.');
            echo json_encode($response);
        }
</pre>```


This is my controller method.
cakephp
1个回答
0
投票

你可以使用

$this->ETest->saveMany($this->request->data)

或者你可以添加

$this->ETest->clear(); 

调用 ->save() 之后我建议添加

'fieldList' => ['cycle', 'course', 'year', 'semester'] 

出于安全原因

© www.soinside.com 2019 - 2024. All rights reserved.