亲爱的Stackoverflowers,我是CakePHP 3的新手,如果在保存到数据库时遇到问题。我正在使用CakePHP 3.我想将调查数据保存到除了控制器之外的SQL-DatabaseTable(在本例中为“First”)。 它工作到目前为止,但我只能保存一次数据。之后它显示: “您的反馈无法保存。请再试一次。”如果我删除sql表中的数据,它会再次工作,但我必须能够保存多次。 我为“TestSurvey”创建了一个模型。它在表和实体中具有与“First”相同的内容。请注意,如果我使用“First”的正常模型,一切正常,我可以保存多次。谢谢您的帮助。 (这里没有母语为英语的人)
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']);
}