简体   繁体   English

CakePHP:数据未保存在多个表中

[英]CakePHP: Data not saving in multiple tables

Hey guys i am new to CakePHP so please help me in this,i am unable to save the single form data in two tables.Here is my code. 大家好,我是CakePHP的新手,请帮助我,我无法将单个表单数据保存在两个表中。这是我的代码。

Main model code: 主要型号代码:

var $hasMany = array('Option');

Controller code: 控制器代码:

public function add()
{
    if (!empty($this->request->data))
    {
       $this->Question->saveAll($this->data);
    }
}

View file: 查看文件:

echo $this->Form->create('Question');
echo $this->Form->input('question');
foreach (range(0,2) as $index) {
  echo $this->Form->input('Option.'.$index.'.option');
}
echo $this->Form->end('Save Poll');

Please tell me where i am going wrong. 请告诉我我要去哪里错了。 The data is inserting only in single table and not in other table. 数据仅插入单个表中,而不插入其他表中。

Try this if its work. 尝试一下,如果它的工作。

<?php

if ($this->Question->save($this->data))
{
    $this->Question->Option->saveAll($this->data['Option']);
    $this->Session->setFlash(__('Question has been successfully edited.', true), 'default',array('class'=>'alert alert-success'));
    $this->redirect(array
    (
        'controller' => 'questions',
        'action'     => 'index'
    ));
    exit;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM