模块:
public function getServiceConfig() {
return array(
'factories' => array(
// 'user' table-------------------------------------
'Album\Model\AlbumTable\dbtable=user' => function($sm) {
$tableGateway = $sm->get('UserTableGateway');
$table = new SignupTable($tableGateway);
return $table;
},
'UserTableGateway' => function ($sm) {
$dbAdapter = $sm->get('Zend\Db\Adapter\Adapter');
$resultSetPrototype = new ResultSet();
$resultSetPrototype->setArrayObjectPrototype(new Signup());
return new TableGateway('user', $dbAdapter, null, $resultSetPrototype);
},
),
);
}
现在我怎么能在我需要的时候在模型的任何地方调用我的'dbadapter'
new \Zend\Validator\Db\RecordExists(
array(
'table' => 'user',
'field' => 'username',
'adapter' => $dbadapter
)
)
请告诉我在任何地方拨打适配器的方法。
-Thanks。
修改:
我遵循了这个'Get default zend db adapter in table model'主题并在我的'模型'中执行了以下操作,
<?php
namespace Application\Model;
use Zend\InputFilter\InputFilter;
use Zend\InputFilter\InputFilterAwareInterface;
use Zend\InputFilter\InputFilterInterface;
use Zend\Validator;
class Signup implements InputFilterAwareInterface {
public $username;
protected $inputFilter;
protected $serviceLocator;
protected $dbAdapter;
public function getServiceLocator() {
return $this->serviceLocator;
}
public function setServiceLocator(Zend\ServiceManager\ServiceLocatorInterface $serviceLocator) {
$this->serviceLocator = $serviceLocator;
return $this;
}
public function exchangeArray($data) {
$this->username = (!empty($data['username'])) ? $data['username'] : null;
$this->password = (!empty($data['password'])) ? $data['password'] : null;
$this->email = (!empty($data['email'])) ? $data['email'] : null;
}
public function getArrayCopy() {
return get_object_vars($this);
}
public function setInputFilter(InputFilterInterface $inputFilter) {
throw new \Exception("Not used");
}
public function getInputFilter() {
$this->$dbAdapter = $this->getServiceLocator()->get('Zend\Db\Adapter\Adapter');
if (!$this->inputFilter) {
$inputFilter = new InputFilter();
$inputFilter->add(array(
'name' => 'username',
'required' => true,
'filters' => array(
array('name' => 'StripTags'),
array('name' => 'StringTrim'),
),
'validators' => array(
array(
'name' => 'NotEmpty',
'options' => array(
'messages' => array(
\Zend\Validator\NotEmpty::IS_EMPTY => 'Username required',
),
),
),
new \Zend\Validator\Db\RecordExists(
array(
'table' => 'user',
'field' => 'username',
'adapter' => $this->dbAdapter,
)
)
),
));
$this->inputFilter = $inputFilter;
}
return $this->inputFilter;
}
}
但我收到了这个错误,
Fatal error: Call to a member function get() on a non-object in F:\xampp\htdocs\zendtest\module\Application\src\Application\Model\Signup.php on line 41
为什么?
答案 0 :(得分:0)
我做了以下,
模块:
<?php
namespace Application;
use Zend\Mvc\ModuleRouteListener;
use Zend\Mvc\MvcEvent;
use Application\Model\Signup;
use Application\Model\SignupTable;
use Zend\Db\ResultSet\ResultSet;
use Zend\Db\TableGateway\TableGateway;
class Module {
public function onBootstrap(MvcEvent $e) {
$e->getApplication()->getServiceManager()->get('translator');
$eventManager = $e->getApplication()->getEventManager();
$moduleRouteListener = new ModuleRouteListener();
$moduleRouteListener->attach($eventManager);
}
public function getConfig() {
return include __DIR__ . '/config/module.config.php';
}
public function getAutoloaderConfig() {
return array(
'Zend\Loader\StandardAutoloader' => array(
'namespaces' => array(
__NAMESPACE__ => __DIR__ . '/src/' . __NAMESPACE__,
),
),
);
}
public function getServiceConfig() {
return array(
'factories' => array(
'dbAdapter\dbtable=user' => function($sm) {
$dbAdapterr = $sm->get('Zend\Db\Adapter\Adapter');
return $dbAdapterr;
},
),
);
}
}
控制器:
<?php
namespace Application\Controller;
use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\ViewModel;
use Application\Model\Signup;
use Application\Form\SignupForm;
class IndexController extends AbstractActionController {
protected $userTablee;
public function getSignTable($table, $object) {
if (!$this->$object) {
$sm = $this->getServiceLocator();
$this->$object = $sm->get($table);
}
return $this->$object;
}
public function indexAction() {
return new ViewModel();
}
public function signupAction() {
$form = new SignupForm();
$form->get('submi')->setValue('Submit');
$request = $this->getRequest();
if ($request->isPost()) {
$album = new Signup();
$t = $this->getSignTable('dbAdapter\dbtable=user', 'userTablee'); // its to get adapter from Module via getSignTable()
$form->setInputFilter($album->getInputFilter($t)); // passes adapter to Model
$form->setData($request->getPost());
if ($form->isValid()) {
// code goes here
}
}
}
}
型号:
<?php
namespace Application\Model;
use Zend\InputFilter\InputFilter;
use Zend\InputFilter\InputFilterAwareInterface;
use Zend\InputFilter\InputFilterInterface;
use Zend\Validator;
class Signup implements InputFilterAwareInterface {
public $username;
public $password;
public $email;
protected $inputFilter;
protected $adapter;
public function exchangeArray($data) {
$this->username = (!empty($data['username'])) ? $data['username'] : null;
$this->password = (!empty($data['password'])) ? $data['password'] : null;
$this->email = (!empty($data['email'])) ? $data['email'] : null;
}
public function getArrayCopy() {
return get_object_vars($this);
}
public function setInputFilter(InputFilterInterface $inputFilter) {
throw new \Exception("Not used");
}
public function getInputFilter($adapter = null) { // receives adapter here
$this->adapter = $adapter;
if (!$this->inputFilter) {
$inputFilter = new InputFilter();
$inputFilter->add(array(
'name' => 'username',
'required' => true,
'filters' => array(
array('name' => 'StripTags'),
array('name' => 'StringTrim'),
),
'validators' => array(
array(
'name' => 'NotEmpty',
'options' => array(
'messages' => array(
\Zend\Validator\NotEmpty::IS_EMPTY => 'Username required',
),
),
),
new \Zend\Validator\Db\NoRecordExists(
array(
'table' => 'user',
'field' => 'username',
'adapter' => $this->adapter, // this line for adapter
'messages' => array(
\Zend\Validator\Db\AbstractDb::ERROR_RECORD_FOUND => 'Username already exists',
),
)
)
),
));
$this->inputFilter = $inputFilter;
}
return $this->inputFilter;
}
}