CakePHP save()不保存数据

时间:2012-05-02 03:52:36

标签: cakephp insert save

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

<?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');
?>

1 个答案:

答案 0 :(得分: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');
            }
        }


    }