Zend \ Stdlib \ Hydrator \ ArraySerializable :: extract期望提供的对象实现getArrayCopy()

时间:2015-10-08 09:04:06

标签: php zend-framework zend-framework2

大家好我正在尝试编辑两个表学生和教育并编写以下代码

studentTable.php

public function Editstudent($admin,$id)
{
    $data = array(
        'fio' => $student->fio,
        'birthdate' => $student->birthdate,
        'edge' => $student->edge,
        'gender' => $student->gender,
        'homeaddress' => $student->homeaddress,
        'actualaddress' => $student->actualaddress,
        'phone' => $student->phone,
        'workplace' => $student->workplace,
        'enterence' => $student->enterence,
        'financesource' => $student->financesource,
        'studyform' => $student->studyform,
    );

    $query = "select s.ID, s.fio,s.birthdate,edge,gender,homeaddress,actualaddress,phone,workplace,enterence,financesource,studyform , c.gruppa \n"
        . "from student s, education_to_student b, education c\n"
        . "where b.student_id = s.id and b.education_id = c.id and s.ID = $id ";

    $adapter = $this->tableGateway->getAdapter();

    $row = $adapter->query($query, \Zend\Db\Adapter\Adapter::QUERY_MODE_EXECUTE);
    if (!$row) {
        throw new \Exception("Could not find row $id");
    }
    $education_id = $row['education_id'];
    $education = new TableGateway('education', $adapter);

    $this->tableGateway->update($data, array('id' => $id));

    $edu = array(
        'gruppa' => $student->group,
        'departmant' => $student->department,
        'greate' => $student->grate,
        'services' => $student->services,
    );
    $education->update($edu,array('id' => $education_id));
}

控制器

public function editstudentAction()
{
    $id =  $this->params()->fromQuery('id');
    $student=$this->getStudentTable()->viewstudent($id);
    //here is the form
    $form= new AddstudentForm();
    $form->bind($student);
    $request = $this->getRequest();
    if ($request->isPost()) {
        $admin = new Admin();
        $form->setInputFilter($admin->getInputFilter());
        $form->setData($request->getPost());

        if ($form->isValid()) {
            // $admin->exchangeArray($form->getData());
            // $admin->getArrayCopy();
            // echo var_dump($admin);
            $this->getStudentTable()->Editstudent($admin,$id);
            // Redirect to list of albums
            return $this->redirect()->toRoute('admin');
        }
    }
    return array('id'=>$id,'form' => $form);

    //form end
}

根据文档,我在交换数组函数

之后将以下方法添加到实体模型中
public function getArrayCopy()
{
    return get_object_vars($this);
}

问题是我收到问题标题中显示的错误。什么可能是错的?

模型和输入过滤器的代码

<?php 
namespace admin\Model;

 // Add these import statements
 use Zend\InputFilter\InputFilter;
 use Zend\InputFilter\InputFilterAwareInterface;
 use Zend\InputFilter\InputFilterInterface;

 class Admin implements InputFilterAwareInterface
 {
     public $fio;
     public $gender;
     public $birthdate;
     public $edge;
     public $university;
     public $group;
     public $department;
     public $grate;
     public $enterence;
     public $financesource;
     public $studyform;
     public $homeaddress;
     public $actualaddress;
     public $phone;
     public $workplace;
     public $services;
     protected $inputFilter;                       // <-- Add this variable

     public function exchangeArray($data)
     {
         $this->fio = (isset($data['fio'])) ? $data['fio'] : null;
        // $this->title  = (isset($data['title']))  ? $data['title']  : null;
         $this->gender = (isset($data['gender'])) ? $data['gender'] : null;
         $this->birthdate = (isset($data['birthdate'])) ? $data['birthdate'] : null;
         $this->edge = (isset($data['edge'])) ? $data['edge'] : null;
         $this->university = (isset($data['university'])) ? $data['university'] : null;
         $this->group = (isset($data['group'])) ? $data['group'] : null;
         $this->department = (isset($data['department'])) ? $data['department'] : null;
         $this->grate = (isset($data['grate'])) ? $data['grate'] : null;
         $this->enterence = (isset($data['enterence'])) ? $data['enterence'] : null;
         $this->financesource = (isset($data['financesource'])) ? $data['financesource'] : null;
         $this->studyform = (isset($data['studyform'])) ? $data['studyform'] : null;
         $this->homeaddress = (isset($data['homeaddress'])) ? $data['homeaddress'] : null;
         $this->actualaddress = (isset($data['actualaddress'])) ? $data['actualaddress'] : null;
         $this->phone = (isset($data['phone'])) ? $data['phone'] : null;
         $this->workplace = (isset($data['workplace'])) ? $data['workplace'] : null;
         $this->services = (isset($data['services'])) ? $data['services'] : null;
   //      $escaper = new Zend\Escaper\Escaper('utf-8');
     }
     public function getArrayCopy()
     {
      echo var_dump(get_object_vars($this)
     );

         return get_object_vars($this);
     }


     // Add content to these methods:
     public function setInputFilter(InputFilterInterface $inputFilter)
     {

       //  throw new \Exception("Not used");
     }

     public function getInputFilter()
     {
         if (!$this->inputFilter) {
             $inputFilter = new InputFilter();


             $inputFilter->add(array(
                 'name'     => 'fio',
                 'required' => true,
                 'filters'  => array(
                     array('name' => 'StripTags'),
                     array('name' => 'StringTrim'),
                 ),
                 'validators' => array(
                     array(
                         'name'    => 'StringLength',
                         'options' => array(
                             'encoding' => 'UTF-8',
                             'min'      => 1,
                             'max'      => 100,
                         ),
                     ),
                 ),
             ));

             $inputFilter->add(array(
                 'name'     => 'birthdate',
                 'required' => true,
                 'filters'  => array(
                     array('name' => 'StripTags'),
                     array('name' => 'StringTrim'),
                 ),
                 'validators' => array(
                     array(
                         'name'    => 'StringLength',
                         'options' => array(
                             'encoding' => 'UTF-8',
                             'min'      => 1,
                             'max'      => 100,
                         ),
                     ),
                 ),
             ));
             $inputFilter->add(array(
                 'name'     => 'university',
                 'required' => true,
                 'filters'  => array(
                     array('name' => 'StripTags'),
                     array('name' => 'StringTrim'),
                 ),
                 'validators' => array(
                     array(
                         'name'    => 'StringLength',
                         'options' => array(
                             'encoding' => 'UTF-8',
                             'min'      => 1,
                             'max'      => 100,
                         ),
                     ),
                 ),
             ));

             $inputFilter->add(array(
                 'name'     => 'group',
                 'required' => true,
                 'filters'  => array(
                     array('name' => 'StripTags'),
                     array('name' => 'StringTrim'),
                 ),
                 'validators' => array(
                     array(
                         'name'    => 'StringLength',
                         'options' => array(
                             'encoding' => 'UTF-8',
                             'min'      => 1,
                             'max'      => 100,
                         ),
                     ),
                 ),
             ));

             $inputFilter->add(array(
                 'name'     => 'department',
                 'required' => true,
                 'filters'  => array(
                     array('name' => 'StripTags'),
                     array('name' => 'StringTrim'),
                 ),
                 'validators' => array(
                     array(
                         'name'    => 'StringLength',
                         'options' => array(
                             'encoding' => 'UTF-8',
                             'min'      => 1,
                             'max'      => 100,
                         ),
                     ),
                 ),
             ));
             $inputFilter->add(array(
                 'name'     => 'enterence',
                 'required' => true,
                 'filters'  => array(
                     array('name' => 'StripTags'),
                     array('name' => 'StringTrim'),
                 ),
                 'validators' => array(
                     array(
                         'name'    => 'StringLength',
                         'options' => array(
                             'encoding' => 'UTF-8',
                             'min'      => 1,
                             'max'      => 100,
                         ),
                     ),
                 ),
             ));
             $inputFilter->add(array(
                 'name'     => 'homeaddress',
                 'required' => true,
                 'filters'  => array(
                     array('name' => 'StripTags'),
                     array('name' => 'StringTrim'),
                 ),
                 'validators' => array(
                     array(
                         'name'    => 'StringLength',
                         'options' => array(
                             'encoding' => 'UTF-8',
                             'min'      => 1,
                             'max'      => 100,
                         ),
                     ),
                 ),
             ));
             $inputFilter->add(array(
                 'name'     => 'actualaddress',
                 'required' => false,
                 'filters'  => array(
                     array('name' => 'StripTags'),
                     array('name' => 'StringTrim'),
                 ),
                 'validators' => array(
                     array(
                         'name'    => 'StringLength',
                         'options' => array(
                             'encoding' => 'UTF-8',
                             'min'      => 1,
                             'max'      => 100,
                         ),
                     ),
                 ),
             ));
             $inputFilter->add(array(
                 'name'     => 'phone',
                 'required' => false,
                 'filters'  => array(
                     array('name' => 'StripTags'),
                     array('name' => 'StringTrim'),
                 ),
                 'validators' => array(
                     array(
                         'name'    => 'StringLength',
                         'options' => array(
                             'encoding' => 'UTF-8',
                             'min'      => 1,
                             'max'      => 100,
                         ),
                     ),
                 ),
             ));
             $inputFilter->add(array(
                 'name'     => 'workplace',
                 'required' => false,
                 'filters'  => array(
                     array('name' => 'StripTags'),
                     array('name' => 'StringTrim'),
                 ),
                 'validators' => array(
                     array(
                         'name'    => 'StringLength',
                         'options' => array(
                             'encoding' => 'UTF-8',
                             'min'      => 1,
                             'max'      => 100,
                         ),
                     ),
                 ),
             ));


             $inputFilter->add(array(
                 'name'     => 'services',
                 'required' => false,
                 'filters'  => array(
                     array('name' => 'StripTags'),
                     array('name' => 'StringTrim'),
                 ),
                 'validators' => array(
                     array(
                         'name'    => 'StringLength',
                         'options' => array(
                             'encoding' => 'UTF-8',
                             'min'      => 1,
                             'max'      => 2000,
                         ),
                     ),
                 ),
             ));

         }
             $this->inputFilter = $inputFilter;

         return $this->inputFilter;
     }
 }
 ?>


   Just refactored the code a litlebit renamed model class  Admin to Student 

堆栈跟踪

#0 C:\xampp\htdocs\disability\vendor\zendframework\zendframework\library\Zend\Form\Fieldset.php(641): Zend\Stdlib\Hydrator\ArraySerializable->extract(Object(Zend\Db\ResultSet\ResultSet))
#1 C:\xampp\htdocs\disability\vendor\zendframework\zendframework\library\Zend\Form\Form.php(900): Zend\Form\Fieldset->extract()
#2 C:\xampp\htdocs\disability\vendor\zendframework\zendframework\library\Zend\Form\Form.php(303): Zend\Form\Form->extract()
#3 C:\xampp\htdocs\disability\module\Admin\src\Admin\Controller\AdminController.php(558): Zend\Form\Form->bind(Object(Zend\Db\ResultSet\ResultSet))
#4 C:\xampp\htdocs\disability\vendor\zendframework\zendframework\library\Zend\Mvc\Controller\AbstractActionController.php(83): Admin\Controller\AdminController->editstudentAction()
#5 [internal function]: Zend\Mvc\Controller\AbstractActionController->onDispatch(Object(Zend\Mvc\MvcEvent))
#6 C:\xampp\htdocs\disability\vendor\zendframework\zendframework\library\Zend\EventManager\EventManager.php(468): call_user_func(Array, Object(Zend\Mvc\MvcEvent))
#7 C:\xampp\htdocs\disability\vendor\zendframework\zendframework\library\Zend\EventManager\EventManager.php(207): Zend\EventManager\EventManager->triggerListeners('dispatch', Object(Zend\Mvc\MvcEvent), Object(Closure))
#8 C:\xampp\htdocs\disability\vendor\zendframework\zendframework\library\Zend\Mvc\Controller\AbstractController.php(116): Zend\EventManager\EventManager->trigger('dispatch', Object(Zend\Mvc\MvcEvent), Object(Closure))
#9 C:\xampp\htdocs\disability\vendor\zendframework\zendframework\library\Zend\Mvc\DispatchListener.php(113): Zend\Mvc\Controller\AbstractController->dispatch(Object(Zend\Http\PhpEnvironment\Request), Object(Zend\Http\PhpEnvironment\Response))
#10 [internal function]: Zend\Mvc\DispatchListener->onDispatch(Object(Zend\Mvc\MvcEvent))
#11 C:\xampp\htdocs\disability\vendor\zendframework\zendframework\library\Zend\EventManager\EventManager.php(468): call_user_func(Array, Object(Zend\Mvc\MvcEvent))
#12 C:\xampp\htdocs\disability\vendor\zendframework\zendframework\library\Zend\EventManager\EventManager.php(207): Zend\EventManager\EventManager->triggerListeners('dispatch', Object(Zend\Mvc\MvcEvent), Object(Closure))
#13 C:\xampp\htdocs\disability\vendor\zendframework\zendframework\library\Zend\Mvc\Application.php(313): Zend\EventManager\EventManager->trigger('dispatch', Object(Zend\Mvc\MvcEvent), Object(Closure))
#14 C:\xampp\htdocs\disability\public\index.php(17): Zend\Mvc\Application->run()
#15 {main}

3 个答案:

答案 0 :(得分:4)

这是你的问题:

#3 C:\xampp\htdocs\disability\module\Admin\src\Admin\Controller\AdminController.php(558): Zend\Form\Form->bind(Object(Zend\Db\ResultSet\ResultSet)) 您正在传递一个实例为Zend\Db\ResultSet\ResultSet的对象,而您的表单需要一个实例为Admin\Model\Admin的对象。要解决此问题,只需在->current()中调用AdminController即可: $form->bind($student->current());

答案 1 :(得分:1)

您确定所有正在保湿的实体都采用这种公开getArrayCopy方法吗?我会说你忘了它,因此错误信息。

修改

你做$form->bind($student);。在这种情况下$student是否也实现了getArrayCopy方法?

答案 2 :(得分:0)

有两种方法可以尝试解决此问题:
1.添加  $form->setHydrator(new \Zend\Stdlib\Hydrator\ObjectProperty()); 绑定方法之前。
2.转到Model / Entity / YourTable.php并添加以下功能:
    public function getArrayCopy(){ //xxx }
希望有帮助!