ZF2 setObject期望收到一个对象参数""

时间:2013-09-02 13:20:34

标签: php zend-framework2

我遇到bind()表单的问题。当我在editAction中调用bind时发生此错误。 错误是:

的Zend \表格\异常\ InvalidArgumentException

文件:

/var/www/html/zf/zf-local/vendor/ZF2/library/Zend/Form/Fieldset.php:439

消息:

Zend\Form\Fieldset::setObject expects an object argument; received ""

堆栈跟踪:

#0 /var/www/html/zf/zf-local/vendor/ZF2/library/Zend/Form/Form.php(271): Zend\Form\Fieldset->setObject(NULL)
#1 /var/www/html/zf/zf-local/module/Phonebook/src/Phonebook/Controller/PhonebookController.php(123): Zend\Form\Form->bind(NULL)
#2 /var/www/html/zf/zf-local/vendor/ZF2/library/Zend/Mvc/Controller/AbstractActionController.php(83): Phonebook\Controller\PhonebookController->editAction()
#3 [internal function]: Zend\Mvc\Controller\AbstractActionController->onDispatch(Object(Zend\Mvc\MvcEvent))
#4 /var/www/html/zf/zf-local/vendor/ZF2/library/Zend/EventManager/EventManager.php(468): call_user_func(Array, Object(Zend\Mvc\MvcEvent))
#5 /var/www/html/zf/zf-local/vendor/ZF2/library/Zend/EventManager/EventManager.php(207): Zend\EventManager\EventManager->triggerListeners('dispatch', Object(Zend\Mvc\MvcEvent), Object(Closure))
#6 /var/www/html/zf/zf-local/vendor/ZF2/library/Zend/Mvc/Controller/AbstractController.php(117): Zend\EventManager\EventManager->trigger('dispatch', Object(Zend\Mvc\MvcEvent), Object(Closure))
#7 /var/www/html/zf/zf-local/vendor/ZF2/library/Zend/Mvc/DispatchListener.php(114): Zend\Mvc\Controller\AbstractController->dispatch(Object(Zend\Http\PhpEnvironment\Request), Object(Zend\Http\PhpEnvironment\Response))
#8 [internal function]: Zend\Mvc\DispatchListener->onDispatch(Object(Zend\Mvc\MvcEvent))
#9 /var/www/html/zf/zf-local/vendor/ZF2/library/Zend/EventManager/EventManager.php(468): call_user_func(Array, Object(Zend\Mvc\MvcEvent))
#10 /var/www/html/zf/zf-local/vendor/ZF2/library/Zend/EventManager/EventManager.php(207): Zend\EventManager\EventManager->triggerListeners('dispatch', Object(Zend\Mvc\MvcEvent), Object(Closure))
#11 /var/www/html/zf/zf-local/vendor/ZF2/library/Zend/Mvc/Application.php(309): Zend\EventManager\EventManager->trigger('dispatch', Object(Zend\Mvc\MvcEvent), Object(Closure))
#12 /var/www/html/zf/zf-local/public/index.php(12): Zend\Mvc\Application->run()
#13 {main}

当我删除bind()时,所有成功执行但当我调用form-> bind时发生此错误。我读了doc,又读了一些。

我的编辑操作是:

    /**
     * Edit Action
     */
 public function editAction()
    {
        $id = (int) $this->params()->fromRoute('id', 0);
        if (!$id) {
            return $this->redirect()->toRoute('phonebook', array(
                'action' => 'add'
            ));
        }

        // Get the Phonebook with the specified id.  An exception is thrown
        // if it cannot be found, in which case go to the index page.
        try {
            $phonebook = $this->getPhonebookTable()->getPhonebookItem($id);
        }
        catch (\Exception $ex) {
            return $this->redirect()->toRoute('phonebook', array(
                'action' => 'index'
            ));
        }

        $form  = new PhonebookForm();
        $form->bind($phonebook);
        $form->get('submit')->setAttribute('value', 'Edit');

        $request = $this->getRequest();
        if ($request->isPost()) {

            $form->setInputFilter($phonebook->getInputFilter());
            $form->setData($request->getPost());

            if ($form->isValid()) {

                $this->getPhonebookTable()->saveItem($phonebook);

                // Redirect to list of albums
                return $this->redirect()->toRoute('phonebook');
            }
        }

        return array(
            'id' => $id,
            'form' => $form,
        );
    }

调试 $ phonebook

object(Phonebook\Model\Phonebook)[254]
  protected 'filter' => null
  public 'id' => string '1' (length=1)
  public 'firstname' => string 'Ivan' (length=4)
  public 'lastname' => string 'Stojkovic' (length=11)
  public 'homephone' => string '0616278054' (length=10)
  public 'workphone' => string '013351456' (length=9)
  public 'company' => string 'Delix' (length=5)
  public 'email' => string 'office.stojmenovic@gmail.com' (length=28)
  public 'address' => string 'Voislava Ilica 8' (length=16)
  public 'city' => string 'Pancevo' (length=7)
  public 'fax' => string '21565486' (length=8)

4 个答案:

答案 0 :(得分:0)

您的$phonebook变量似乎为空。因此,如果没有找到记录,您的getPhonebookItem()方法似乎不会抛出异常。

答案 1 :(得分:0)

请看你的getPhonebookItem函数,可能是你没有回报。 你必须返回一些$ row。

像这样......

$row = $rowset->current();

if(!$row) {
    throw new \Exception("Article #$id not found");
}
return $row; //!!!!!!!!!!!!!

答案 2 :(得分:0)

我的情况中的错误是当我尝试使用Doctrine的实体管理器检索实体时它返回null。然后我将null / false值传递给$ form-> bind():

    $form->bind( $requestEntity );

这产生了错误。

答案 3 :(得分:0)

我在使用Doctrine开发的项目中遇到了同样的错误。使用Doctrine,相当于你的

$phonebook = $this->getPhonebookTable()->getPhonebookItem($id);

可能类似于

$phonebook = $this->getEntityManager()->find('PhonebookModule\Entity\Phonebook', $id);

我的表单一直有效,直到我需要使用不同的列从数据库中选择一行。而不是

public function editAction()
    {
        $id = (int) $this->params()->fromRoute('id', 0);

        // ...

            $phonebook = $this->getEntityManager()->find('PhonebookModule\Entity\Phonebook', $id);

            // ...

我需要像

这样的东西
public function editAction()
    {
        $firstname = $this->params()->fromRoute('firstname', 0);

        // ...

            $phonebook = $this->getEntityManager()->find('PhonebookModule\Entity\Phonebook', $firstname);

            // ...

虽然我在两种方法中采用了相同的策略,但后者导致setObject expects an object argument received “”错误。经过大量的血,汗和泪,我最终发现填充$phonebook变量的功能不起作用。在Doctrine中,find()函数仅在列参数用于已设置为实体中数据集的id的列时才有效。如果要根据其他列选择行,则必须使用findBy()findOneBy()。换句话说,如果$id已被命名为实体中数据集的ID,则

// this works:
$phonebook = $this->getEntityManager()->find('PhonebookModule\Entity\Phonebook', $id);

// this doesn’t work:     
$phonebook = $this->getEntityManager()->find('PhonebookModule\Entity\Phonebook', $firstname);

// this works:
$phonebook = $this->getEntityManager()->findOneBy('PhonebookModule\Entity\Phonebook', $firstname);

虽然我们的代码不同,但也许我的经验可以帮助您找出问题所在:数据存在于数据模型中;但是,无论出于何种原因,将该数据放入变量的功能都不起作用。