Cakephp 3 saving to an different table

我的梦境 提交于 2019-12-08 11:14:56

问题


Hi dear Stackoverflowers, im new to CakePHP 3 and if got a Problem with saving into the Database. Im using CakePHP 3. I want to save the Data of a survey into a SQL-DatabaseTable other than the one of the Controller ("First" in this case). It works so far but i can only save data for one time. After that it shows: "Your Feedback could not be saved. Please, try again.". If I delete the data in the sql table, it works again, but i have to be able to save multiple times. I have created a model for "TestSurvey". It has the same content in table and entity as "First". Please notice if I use the normal model of "First" everything works fine and i can save multiple times. Thank you for your help. (no native english speaker here)

namespace App\Controller;

use App\Controller\AppController;
use Cake\Event\Event;
use Cake\Network\Request;
use Cake\ORM\TableRegistry;

class FirstController extends AppController
public function index()
{

    $first = $this->paginate($this->First);
    $this->set(compact('first'));
    $this->set('_serialize', ['first']);
}

public function localtest()
{
    $testSurveyTable = TableRegistry::get('TestSurvey');
    $testSurvey = $testSurveyTable->newEntity();
    if ($this->request->is('post')) {
         $testSurvey = $testSurveyTable->patchEntity($testSurvey, $this->request->data);
        if ($testSurveyTable->save($testSurvey)) {
            $this->Flash->success(__('Your Feedback has been saved.'));
            return $this->redirect(['action' => 'index']);
        } else {
            $this->Flash->error(__('Your Feedback could not be saved. Please, try again.'));
        }
    }

    $this->set(compact('testSurvey'));
    $this->set('_serialize', ['testSurvey']);
}

来源:https://stackoverflow.com/questions/37707646/cakephp-3-saving-to-an-different-table

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!