大家好,我正在尝试编辑student和education两个表,并编写了以下代码
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);
}
问题是,我得到的错误显示在问题的标题。会出什么问题呢?
模型和inputfilters的代码
<?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 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
实例的对象。要解决这个问题,只需在AdminController
中像这样调用->current()
:$form->bind($student->current());
你确定所有你要保湿的实体都有这个公共getArrayCopy
方法吗?我会说你把它忘在某个地方了,因此出现了错误信息。
编辑
你做$form->bind($student);
。$student
在这种情况下也实现getArrayCopy
方法吗?
有两种方法可以尝试解决这个问题:
1. 添加这个$form->setHydrator(new 'Zend'Stdlib'Hydrator'ObjectProperty());
Before bind方法。
2. 进入Model/Entity/YourTable.php,添加这个函数:public function getArrayCopy(){
//xxx
}