CakePHP:数据未保存在多个表中 [英] CakePHP: Data not saving in multiple tables

查看:114
本文介绍了CakePHP:数据未保存在多个表中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



主要模型代码

  var $ hasMany = array('Option'); 

控制器代码


$ b b

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

查看文件 / p>

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

请告诉我在哪里出问题。

解决方案

如果其工作,请尝试此操作。

 <?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-
$ this-> redirect(array

'controller'=>'questions',
'action'=>'index'
) ;
exit;
}


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.

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;
}

这篇关于CakePHP:数据未保存在多个表中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆