ZF2 向 Db 插入数据

ZF2 inserting data to Db

保存过程中出现错误:

values() expects an array of values or Zend\Db\Sql\Select instance

我认为错误来自:

$this->tableGateway->insert($procedure);

我不明白哪里出了问题。

这是我的处理函数:

public function processAction()
    {
         if (!$this->request->isPost()){
            return $this->redirect()->toRoute(null, array('controller'=>'test', 'action'=>'index'));
        }

        $post = $this->request->getPost();
        $form = new TestForm();
        $inputFilter = new \Postepowania\Form\TestFilter();
        $form->setInputFilter($inputFilter);
        $form->setData($post);

        if (!$form->isValid()){
            $model = new ViewModel(
                array(
                    'error' => true,
                    'form' =>$form,
                )
            );            
            $model->setTemplate('postepowania/test/index');
            return $model;
        }

        $this->createTest($form->getData());    
        //return $this->redirect()->toRoute(null,array('controller' => 'test','action' => 'confirm',));
    }

和 createTest 函数:

 public function createTest(array $data){

        $testTable = $this->getServiceLocator()->get('TestTable');
        $test = new Test();
        $test->exchangeArray($data);

        $testTable->save($test);

        return true;
    }

保存功能很简单:

public function save(Test $procedure)
    {
        $id = (int)$procedure->id;
        if($id == 0)
        {
            $this->tableGateway->insert($procedure);
        }

    }
$this->tableGateway->insert()

source 来看,insert() 需要将数组传递给它,而不是对象。我建议在传入之前将对象转换为数组。