0

我看不出我的代码有什么问题。但它不保存数据:

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

1 回答 1

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


    }
于 2012-06-19T11:52:35.633 回答