【问题标题】:CakePHP 1.3.20: saveAll() for Multi data in form is not workCakePHP 1.3.20:表单中的多数据的 saveAll() 不起作用
【发布时间】:2014-11-11 09:33:51
【问题描述】:

我使用的是 CakePHP 1.3.20。我尝试使添加功能如下:

我有 4 个字段“A、B、C、D”要插入。当我想添加更多数据时,我使用 JQuery 在视图中添加更多字段。所以到那时我将拥有 "A0,B0,C0,D0" "A1,B1,C1,D1"...等等。

echo $this->Form->input('Model.1.A',array('label'=>'','autocomplete'=>'off','id'=>'advoidance_word','class'=>'form-control top noradius'));
echo $this->Form->input('Model.1.B',array('label'=>'','autocomplete'=>'off','id'=>'advoidance_word','class'=>'form-control top noradius'));
echo $this->Form->input('Model.1.C',array('label'=>'','autocomplete'=>'off','id'=>'advoidance_word','class'=>'form-control top noradius'));
echo $this->Form->input('Model.1.D',array('label'=>'','autocomplete'=>'off','id'=>'advoidance_word','class'=>'form-control top noradius'));

echo $this->Form->input('Model.2.A',array('label'=>'','autocomplete'=>'off','id'=>'advoidance_word','class'=>'form-control top noradius'));
echo $this->Form->input('Model.2.B',array('label'=>'','autocomplete'=>'off','id'=>'advoidance_word','class'=>'form-control top noradius'));
echo $this->Form->input('Model.2.C',array('label'=>'','autocomplete'=>'off','id'=>'advoidance_word','class'=>'form-control top noradius'));
echo $this->Form->input('Model.2.D',array('label'=>'','autocomplete'=>'off','id'=>'advoidance_word','class'=>'form-control top noradius'));

在我制作的控制器中:

function add() {
            if (!empty($this->data)) {
                if(isset($_POST['create'])){
                    if ($this->Model->saveAll($this->data)) {
                        debug($this->data);
                        $this->Session->setFlash('Your word has been saved.');
                        $this->redirect(array('action' => 'index'));
                    }
                }
            }
        }

我的副本($this->data)

Array
(
    [Model] => Array
        (
            [0] => Array
                (
                    [A] => A
                    [B] => B
                    [C] => C
                    [D] => D
                )

            [1] => Array
                (
                    [A] => A1
                    [B] => B1
                    [C] => C1
                    [D] => D1
                )

        )

)

我使用 MSSQL Server 2012 作为数据库。它没有错误。只是将任何数据插入数据库。在这种情况下什么是假的?请帮帮我!

【问题讨论】:

  • 你有一个 debug($this->data);在代码中,你能发布它的结果吗?
  • 我刚刚更新了!请检查一下。我试图删除 Model.0.fieldname 之间的数字 0 和 1 并使用 saveAll();这个功能没问题。数据已插入数据库,但仅插入第一行。其他人没有。如果我在 debug($this->data) 之间放置数字 0 将像上面一样,但数据库中没有任何内容。这个怎么样?请

标签: jquery cakephp sql-server-2012 cakephp-1.3


【解决方案1】:

我完全找到了。我已经做对了,但我在小事上失败了。

我的所有代码都已更正。我们将使用:

echo $this->Form->input('**Model**.**1**.**Fieldname**',array(...));

用于插入表单。

但是在控制器中我们必须稍微改变一下。

来自(如上)

function add() {
            if (!empty($this->data)) {
                if(isset($_POST['create'])){
                    if ($this->Model->saveAll($this->data)) {
                        debug($this->data);
                        $this->Session->setFlash('Your word has been saved.');
                        $this->redirect(array('action' => 'index'));
                    }
                }
            }
        }

到这个:

function add() {
            if (!empty($this->data)) {
                if(isset($_POST['create'])){
                    foreach($this->data as $data) {
                        if ($this->MAvoidanceWord->saveAll($data)) {
                            debug($this->data);
                            //$this->Session->setFlash('Your word has been saved.');
                            //$this->redirect(array('action' => 'index'));
                        }
                    }   
                }
            }
        }

两者之间的区别在于:在旧代码中,我没有 foreach() 来查询数组中的每个数据,因此 Cake 不知道如何插入。所以我做到了,并且成功了。希望我的主题对某人有所帮助。 CakePHP 1.3 没有太多的经验文档。谢谢大家观看。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-14
    • 1970-01-01
    相关资源
    最近更新 更多