我在usereditform上提交保存后尝试访问时收到此消息 错误信息: 在第13行的/var/www/CommunicationApp/module/Users/src/view/users/user-manager/edit.phtml中调用非对象的成员函数prepare() 这是我的档案 任何人都可以告诉我发生了什么事吗>
UserManagerController.php
<?php
namespace Users\Controller;
use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\ViewModel;
class UserManagerController extends AbstractActionController
{
public function indexAction(){
//$form=$this->getServiceLocator()->get('UserManagerForm');
$userTable = $this->getServiceLocator()->get('UserTable');
$viewModel = new ViewModel(array('users'=>$userTable->fetchAll()));
return $viewModel;
}
public function editAction(){
$userTable = $this->getServiceLocator()->get('UserTable');
$user = $userTable->getUser($this->params()->fromRoute('id'));
//print_r($user);die;
$form = $this->getServiceLocator()->get('UserEditForm');
$form->setHydrator(new \Zend\Stdlib\Hydrator\ClassMethods());
$form->bind($user);
$form->setData($user);
$viewModel = new ViewModel(array('form'=>$form,'user_id' => $this->params()->fromRoute('id')));
return $viewModel;
}
public function processAction(){
$post = $this->request->getPost();
$userTable = $this->getServiceLocator()->get('UserTable');
$user = $userTable->getUser($post->id);
$form = $this->getServiceLocator()->get('UserEditForm');
$form->setHydrator(new \Zend\Stdlib\Hydrator\ClassMethods());
$form->bind($user);
$form->setData($post);
$this->getServiceLocator()->get('UserTable')->saveUser($user);
}
public function deleteAction(){
$this->getServiceLocator()->get('UserTable')->deleteUser($this->params()->fromRoute('id'));
}
}
UserEditForm.php
<?php
// filename : module/Users/src/Users/Form/RegisterForm.php
namespace Users\Form;
use Zend\Form\Form;
class UserEditForm extends Form
{
public function __construct($name = null)
{
parent::__construct('UserEdit');
$this->setAttribute('method', 'post');
$this->setAttribute('enctype','multipart/form-data');
$this->add(array('name' => 'id','attributes' => array('type' => 'text',),
'options' => array('label' => 'ID',),));
$this->add(array('name' => 'name','attributes' => array('type' => 'text',),
'options' => array('label' => 'Full Name',),));
$this->add(array('name' => 'email','attributes' => array('type' => 'email',),
'options' => array('label' => 'Email',),
'filters' => array(array('name' => 'StringTrim'),
),));
$this->add(array('name' => 'submit','attributes' => array('type' => 'submit','value' => 'Save'),
));
}
}
edit.phtml
<section class="edit">
<h2>Login</h2>
<?php if ($this->error): ?>
<p class="error">
There were one or more issues with your submission.
Please correct them as
indicated below.
</p>
<?php endif ?>
<?php
$form = $this->form;
//print_r($form);die;
$form->prepare();
$form->setAttribute('action', $this->url(NULL,
array('controller'=>'UserManager', 'action' => 'process')));
$form->setAttribute('method', 'post');
echo $this->form()->openTag($form);
?>
<dl class="zend_form">
<dt><?php echo $this->formLabel($form->get('id')); ?></dt>
<dd><?php
echo $this->formElement($form->get('id'));
echo $this->formElementErrors($form->get('id'));
?></dd>
<dl class="zend_form">
<dt><?php echo $this->formLabel($form->get('name')); ?></dt>
<dd><?php
echo $this->formElement($form->get('name'));
echo $this->formElementErrors($form->get('name'));
?></dd>
<dl class="zend_form">
<dt><?php echo $this->formLabel($form->get('email')); ?></dt>
<dd><?php
echo $this->formElement($form->get('email'));
echo $this->formElementErrors($form->get('email'));
?></dd>
<dd><?php
echo $this->formElement($form->get('submit'));
echo $this->formElementErrors($form->get('submit'));
?></dd>
</dl>
<?php echo $this->form()->closeTag() ?>
</section>
模型/ user.php的
<?php
namespace Users\Model;
class User
{
public $id;
public $name;
public $email;
public $password;
public function setPassword($clear_password)
{
$this->password = md5($clear_password);
}
function exchangeArray($data)
{
$this->id = (isset($data['id'])) ?
$data['id'] : null;
$this->name = (isset($data['name'])) ?
$data['name'] : null;
$this->email = (isset($data['email'])) ?
$data['email'] : null;
if (isset($data["password"]))
{
$this->setPassword($data["password"]);
}
}
}
模型/ usertable.php
<?php
namespace Users\Model;
use Zend\Db\Adapter\Adapter;
use Zend\Db\ResultSet\ResultSet;
use Zend\Db\TableGateway\TableGateway;
class UserTable
{
protected $tableGateway;
public function __construct(TableGateway $tableGateway)
{
$this->tableGateway = $tableGateway;
}
public function saveUser(User $user)
{
$data = array(
'email' => $user->email,
'name' => $user->name,
'password' => $user->password,
);
$id = (int)$user->id;
if ($id == 0) {
$this->tableGateway->insert($data);
} else {
if ($this->getUser($id)) {
$this->tableGateway->update($data, array('id' => $id));
} else {
throw new \Exception('User ID does not exist');
}
}
}
public function getUser($id)
{
$id = (int) $id;
$rowset = $this->tableGateway->select(array('id' => $id));
$row = $rowset->current();
if (!$row) {
throw new \Exception("Could not find row $id");
}
return $row;
}
public function fetchAll(){
$resultSet = $this->tableGateway->select();
return $resultSet;
}
public function getUserByEmail($userEmail){
$rowset = $this->tableGateway->select(array('email'=>$userEmail));
$row = $rowset->current();
if(!$row){
throw new \
Exception("Could not find row $userName");
}
return $row;
}
public function deleteUser($id)
{
$this->tableGateway->delete(array('id' => $id));
}
}
和module.php
<?php
namespace Users;
use Zend\ModuleManager\Feature\AutoloaderProviderInterface;
use Zend\Mvc\ModuleRouteListener;
use Zend\Mvc\MvcEvent;
use Users\Model\User;
use Users\Model\UserTable;
use Users\Model\Uploads;
use Users\Model\UploadsTable;
use Zend\Db\ResultSet\ResultSet;
use Zend\Db\TableGateway\TableGateway;
class Module implements AutoloaderProviderInterface
{
public function getServiceConfig()
{
return array(
'abstract_factories' => array(),
'aliases' => array(),
'factories' => array(
// DB
'UserTable' => function($sm) {
$tableGateway = $sm->get('UserTableGateway');
$table = new UserTable($tableGateway);
return $table;
},
'UploadsTable' => function($sm) {
$tableGateway = $sm->get('UploadsTableGateway');
$table = new UploadsTable($tableGateway);
return $table;
},
'UserTableGateway' => function ($sm) {
$dbAdapter = $sm->get('Zend\Db\Adapter\Adapter');
$resultSetPrototype = new ResultSet();
$resultSetPrototype->setArrayObjectPrototype(new User());
return new TableGateway('user', $dbAdapter, null,
$resultSetPrototype);
},
'UploadsTableGateway' => function ($sm) {
$dbAdapter = $sm->get('Zend\Db\Adapter\Adapter');
$resultSetPrototype = new ResultSet();
$resultSetPrototype->setArrayObjectPrototype(new Uploads());
return new TableGateway('uploads', $dbAdapter, null,
$resultSetPrototype);
},
// FORMS
'LoginForm' => function ($sm) {
$form = new \Users\Form\LoginForm();
$form->setInputFilter($sm->get('LoginFilter'));
return $form;
},
'UploadsManagerForm' => function ($sm) {
$form = new \Users\Form\UploadsManagerForm();
//$form->setInputFilter($sm->get('LoginFilter'));
return $form;
},
'RegisterForm' => function ($sm) {
$form = new \Users\Form\RegisterForm();
$form->setInputFilter($sm->get('RegisterFilter'));
return $form;
},
'UserEditForm' => function ($sm) {
$form = new \Users\Form\UserEditForm();
$form->setInputFilter($sm->get('UserEditFilter'));
return $form;
},
'UserManagerForm' => function ($sm) {
$form = new \Users\Form\UserManagerForm();
//$form->setInputFilter($sm->get('RegisterFilter'));
return $form;
},
// FILTERS
'LoginFilter' => function ($sm) {
return new \Users\Form\LoginFilter();
},
'RegisterFilter' => function ($sm) {
return new \Users\Form\RegisterFilter();
},
'UserEditFilter' => function ($sm) {
return new \Users\Form\UserEditFilter();
},
'AuthenticationService' => function ($sm) {
return new \Zend\Authentication\AuthenticationService();
},
'AuthenticationService' => function ($sm) {
return new \Users\Form\UserManager();
},
),
'invokables' => array('Users\Controller\UserManager' => 'Users\Controller\UserManagerController',
//'Users\Controller\UploadsManager' => 'Users\Controller\UploadsManagerController',
),
'services' => array(),
'shared' => array(),
);
}
public function getAutoloaderConfig()
{
return array(
'Zend\Loader\ClassMapAutoloader' => array(
__DIR__ . '/autoload_classmap.php',
),
'Zend\Loader\StandardAutoloader' => array(
'namespaces' => array(
// if we're in a namespace deeper than one level we need to fix the \ in the path
__NAMESPACE__ => __DIR__ . '/src/' . str_replace('\\', '/' , __NAMESPACE__),
),
),
);
}
public function getConfig()
{
return include __DIR__ . '/config/module.config.php';
}
public function getFileUploadsLocation()
{
// Fetch Configuration from Module Config
$config = $this->getServiceLocator()->get('config');
return $config['module_config']['uploads_location'];
}
public function onBootstrap(MvcEvent $e)
{
// You may not need to do this if you're doing it elsewhere in your
// application
$eventManager = $e->getApplication()->getEventManager();
$moduleRouteListener = new ModuleRouteListener();
$moduleRouteListener->attach($eventManager);
}
}
答案 0 :(得分:0)
原因是您要发布到processAction,它不会在视图模型中返回$ form。不知道为什么这会渲染编辑phtml,但这可能是问题。
可能只是想在processAction的末尾添加重定向和flash消息
$this->flashMessenger()->addSuccessMessage($this->translate('EDIT_SUCCESS'));
return $this->redirect()->toRoute('users');
如果表单未通过验证,则为和另一个。
我可能会发布相同的动作,以便您可以轻松地完成所有验证工作。但这取决于你......在editAction中
if ($this->getRequest()->isPost()) {
$data = $this->getRequest()->getPost();
$form->setData($data);
if ($form->isValid()) {
//saving and stuff
$this->flashMessenger()->addSuccessMessage($this->translate('EDIT_SUCCESS'));
return $this->redirect()->toRoute('users');
} else {
$this->flashMessenger()->addErrorMessage($this->translate('EDIT_ERROR'));
}
}
答案 1 :(得分:0)
我通过在流程操作结束时返回表单来解决它:
public function processAction(){
$post = $this->request->getPost();
$userTable = $this->getServiceLocator()->get('UserTable');
$user = $userTable->getUser($post->id);
$this->getServiceLocator()->get('UserTable')->saveUser($user);
$form = $this->getServiceLocator()->get('UserEditForm');
$form->setHydrator(new \Zend\Stdlib\Hydrator\ClassMethods());
$form->bind($user);
$form->setData($post);
return $form;
}