可捕获的致命错误:参数1传递给 Zend \ Validator \ Db \ AbstractDb :: setAdapter()必须是。的实例 Zend \ Db \ Adapter \ Adapter,null给定,调用 /home2/mapasgua/vendor/zendframework/zendframework/library/Zend/Validator/AbstractValidator.php 在第142行并在中定义 /home2/mapasgua/vendor/zendframework/zendframework/library/Zend/Validator/Db/AbstractDb.php 在第168行
Module.php
<?php
namespace User;
use Zend\Mvc\MvcEvent;
use Zend\Mvc\ModuleRouteListener;
use User\Model\UsersTable;
use User\Model\Users;
class Module
{
public function onBootstrap(MvcEvent $e)
{
ini_set('date.timezone', 'America/Sao_Paulo');
$e->getApplication()->getServiceManager()->get('translator');
$eventManager = $e->getApplication()->getEventManager();
$moduleRouteListener = new ModuleRouteListener();
$moduleRouteListener->attach($eventManager);
$e->getApplication()->getEventManager()->getSharedManager()->attach('Zend\Mvc\Controller\AbstractActionController', 'dispatch', function($e) {
$controller = $e->getTarget();
$controllerClass = get_class($controller);
$moduleNamespace = substr($controllerClass, 0, strpos($controllerClass, '\\'));
$config = $e->getApplication()->getServiceManager()->get('config');
if (isset($config['module_layouts'][$moduleNamespace])) {
$controller->layout($config['module_layouts'][$moduleNamespace]);
}
}, 100);
}
public function getAutoloaderConfig()
{
return array(
// 'Zend\Loader\ClassMapAutoloader' => array(
// __DIR__ . '/autoload_classmap.php',
// ),
'Zend\Loader\StandardAutoloader' => array(
'namespaces' => array(
__NAMESPACE__ => __DIR__ . '/src/' . __NAMESPACE__,
),
),
);
}
public function getServiceConfig()
{
return array(
'factories' => array(
'User\Model\UsersTable' => function($sm) {
$dbAdapter = $sm->get('Zend\Db\Adapter\Adapter');
$table = new UsersTable($dbAdapter);
return $table;
},
'User\Model\Users' => function($sm) {
$dbAdapter = $sm->get('Zend\Db\Adapter\Adapter');
$users = new Users();
$users->setDbAdapter($dbAdapter);
return $users;
},
),
);
}
public function getConfig()
{
return include __DIR__ . '/config/module.config.php';
}
}
Users.php
<?php
namespace User\Model;
use Zend\InputFilter\InputFilter;
use Zend\InputFilter\Factory as InputFactory;
use Zend\InputFilter\InputFilterInterface;
use Zend\InputFilter\InputFilterAwareInterface;
use Zend\Db\Adapter\Adapter;
use Zend\Validator\Db\AbstractDb;
class Users implements InputFilterAwareInterface
{
public $id;
public $name;
public $username;
public $password;
protected $inputFilter;
public $_dbAdapter;
public function exchangeArray($data)
{
$this->id = (isset($data['id'])) ? $data['id'] : null;
$this->name = (isset($data['name'])) ? $data['name'] : null;
$this->username = (isset($data['username'])) ? $data['username'] : null;
$this->password = (isset($data['password'])) ? $data['password'] : null;
}
public function getArrayCopy()
{
return get_object_vars($this);
}
public function setInputFilter(InputFilterInterface $inputFilter)
{
throw new \Exception("Not used");
}
public function setDbAdapter($dbAdapter)
{
$this->_dbAdapter = $dbAdapter;
}
public function getDbAdapter()
{
return $this->_dbAdapter;
}
public function getInputFilter()
{
if (!$this->inputFilter) {
$inputFilter = new InputFilter();
$factory = new InputFactory();
$inputFilter->add($factory->createInput(array(
'name' => 'id',
'required' => true,
'filters' => array(
array('name' => 'Int'),
),
)));
$inputFilter->add($factory->createInput(array(
'name' => 'name',
'required' => true,
'filters' => array(
array('name' => 'StripTags'),
array('name' => 'StringTrim'),
),
'validators' => array(
array(
'name' => 'StringLength',
'options' => array(
'encoding' => 'UTF-8',
'min' => 3,
'max' => 32,
),
),
),
)));
$inputFilter->add($factory->createInput(array(
'name' => 'username',
'required' => true,
'filters' => array(
array('name' => 'StripTags'),
array('name' => 'StringTrim'),
),
'validators' => array(
array(
'name' => 'EmailAddress',
'options' => array(
'encoding' => 'UTF-8',
'min' => 3,
'max' => 32,
'message' => 'Endereço de e-mail invalido',
),
),
array(
'name' => 'Db\NoRecordExists',
'options' => array(
'table' => 'users',
'field' => 'username',
'adapter' => $this->getDbAdapter(),
'exclude' => array(
'field' => 'id',
'value' => !is_null( $this->id ) && !empty( $this->id ) ? $this->id : 0,
),
),
),
),
)));
$inputFilter->add($factory->createInput(array(
'name' => 'password',
'required' => true,
'filters' => array(
array('name' => 'StripTags'),
array('name' => 'StringTrim'),
),
'validators' => array(
array(
'name' => 'StringLength',
'options' => array(
'encoding' => 'UTF-8',
'min' => 3,
'max' => 32,
),
),
),
)));
$inputFilter->add($factory->createInput(array(
'name' => 'retype-password',
'required' => true,
'filters' => array(
array('name' => 'StripTags'),
array('name' => 'StringTrim'),
),
'validators' => array(
array(
'name' => 'StringLength',
'options' => array(
'encoding' => 'UTF-8',
'min' => 3,
'max' => 32,
),
),
array(
'name' => 'Identical',
'options' => array(
'token' => 'password', //I have tried $_POST['password'], but it doesnt work either
),
),
),
)));
$this->inputFilter = $inputFilter;
}
return $this->inputFilter;
}
}
UserController.php
命名空间User \ Controller;
<?php
use Zend\Mvc\Controller\AbstractActionController;
use Zend\Authentication\AuthenticationService;
use Zend\Authentication\Adapter\DbTable as AuthAdapter;
use Zend\Authentication\Result as Result;
use Zend\Mail;
use User\Form\LoginForm;
use User\Form\RegisterForm;
use User\Form\RecuperarForm;
use User\Model\Users;
class UserController extends AbstractActionController
{
protected $usersTable;
public function configAction()
{
$auth = new AuthenticationService();
$form = new RegisterForm();
$identity = null;
if ($auth->hasIdentity()) {
$dados = $this->getUsersTable()->getUser($auth->getIdentity());
$request = $this->getRequest();
if ($request->isPost()) {
$users = new Users();
$post = $request->getPost();
$form->setInputFilter($users->getInputFilter());
$form->setData($post);
if ($form->isValid()) {
$users->exchangeArray($post);
$this->getUsersTable()->saveUsers($users);
$this->flashMessenger()->addMessage("Configurações atualizadas com sucesso.");
return $this->redirect()->toRoute('painel');
}
} else {
$post = array(
'id' => $dados->id,
'name' => $dados->name,
'username' => $dados->username,
);
$form->setData($post);
}
} else {
return $this->redirect()->toRoute('login');
}
return array(
'form' => $form,
);
}
答案 0 :(得分:1)
您的问题是您已为Users
定义了工厂,但您没有使用它。行$users = new Users();
直接实例化对象而不使用您的工厂。
将其更改为
$users = $this->getServiceLocator()->get('User\Model\Users');
由于你的对象依赖于Zend\Db\Adapter\Adapter
,你需要你的工厂为你注入这个。