【问题标题】:CakePHP save() doesn't save dataCakePHP save() 不保存数据
【发布时间】:2012-05-02 03:52:36
【问题描述】:

我没有发现我的代码有任何问题。但它不保存数据:

<?php
    class ProductsController extends AppController{
        var $name = 'Products';
        //var $helpers = array('Form');
        //var $scaffold;

        function index(){
            $this->Product->recursive = 1;
            $products = $this->Product->find('all');
            $this->set('products',$products);
            //pr($products);
        }

        function add(){
            $categories = $this->Product->Category->find('list',array(
                'field'=>array('Category.categoryName')
            ));
            $this->set('categories',$categories);

            if(!empty($this->data)){
                if($this->Product->save($this->data)){
                    $this->Session->setFlash('Saved');
                }
            }


        }
    }
?>

它闪烁“已保存”,但我的表中没有插入任何内容。当它应该正常运行时可能会出现什么问题。 :(

下面是我的 add.ctp 模型:

<h2>ADD</h2>

<?php echo $this->Form->create('Product',array('action'=>'add')); ?>
<?php
    echo $form->input('ProductName');
    echo $form->input('categories');
    echo $form->end('DONE');
?>

【问题讨论】:

  • 好吧,在我最好的朋友“pr()”函数的帮助下,我看到了我的错误。我的 add.ctp 是错误的。它应该是这样的:

    ADD

    Form->create('Product',array('action'=>'add')); ?> input('productName'); echo $form->input('category_id');回声 $form->end('DONE'); ?>
  • 在保存之前尝试检查 pr($this->data)...你得到发布的数据了吗..?
  • 是的.. 我做到了.. :) 非常感谢 pr() 函数。

标签: cakephp insert save


【解决方案1】:

必须先使用create()方法才能保存

function add(){
        $categories = $this->Product->Category->find('list',array(
            'field'=>array('Category.categoryName')
        ));
        $this->set('categories',$categories);

        if(!empty($this->data)){

            $this->Product->create();
            if($this->Product->save($this->data)){
                $this->Session->setFlash('Saved');
            }
        }


    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-09-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多