Zend framework2 如何将db适配器设置为Zend Framework 2中存在的验证程序记录
我正在尝试将validator RecordExists添加到表单中,但收到错误“无db适配器存在”。如何将db适配器设置到此验证器?我使用skeleton应用程序中的示例,并尝试这样做(是的,我知道$dbAdapter未定义:),我正在搜索解决方案如何将此变量更改为db适配器资源):Zend framework2 如何将db适配器设置为Zend Framework 2中存在的验证程序记录,zend-framework2,Zend Framework2,我正在尝试将validator RecordExists添加到表单中,但收到错误“无db适配器存在”。如何将db适配器设置到此验证器?我使用skeleton应用程序中的示例,并尝试这样做(是的,我知道$dbAdapter未定义:),我正在搜索解决方案如何将此变量更改为db适配器资源): namespace-Album\Model; 使用Zend\InputFilter\Factory作为InputFactory;//添加($factory->createInput(数组)( 'name'=>'i
namespace-Album\Model;
使用Zend\InputFilter\Factory作为InputFactory;//添加($factory->createInput(数组)(
'name'=>'id',
“必需”=>true,
'过滤器'=>阵列(
数组('name'=>'Int'),
),
“验证程序”=>数组(
排列(
'name'=>'Db\RecordExists',
“选项”=>数组(
'表'=>'相册',
“字段”=>“标题”,
“适配器”=>$dbAdapter
),
),
),
)));
$this->inputFilter=$inputFilter;
}
返回$this->inputFilter;
}
}您可以创建“Album/Model/Album”工厂,并将dbAdapter注入Album Model
'service_manager' => array(
'factories' => array(
'Application/Model/Album' => function($sm){
$dbAdapter = $sm->get('Zend\Db\Adapter\Adapter');
$album = new \Application\Model\Album();
$album->setDbAdapter($dbAdapter);
return $album;
},
),
),
现在我们必须实现setDbAdapter和getDbAdapter方法:
namespace Application\Model;
class Album
{
public $id;
public $artist;
public $title;
private $_dbAdapter;
public function exchangeArray($data)
{
$this->id = (isset($data['id'])) ? $data['id'] : null;
$this->artist = (isset($data['artist'])) ? $data['artist'] : null;
$this->title = (isset($data['title'])) ? $data['title'] : null;
}
public function setDbAdapter($dbAdapter) {
$this->_dbAdapter = $dbAdapter;
}
public function getDbAdapter() {
return $this->_dbAdapter;
}
}
您可以通过调用$this->getDbAdapter()在输入筛选器中获取dbAdapter代码>
记住通过ServiceLocator在controller中获取相册模型,否则dbAdapter将无法在您的模型中使用
$album = $this->getServiceLocator()->get('Application/Model/Album');
@kierzniak提供了一种注入DbAdapter的好方法,我的解决方案几乎是一样的,不仅注入DbAdapter,而且注入完整的ServiceLocator,通过注入ServiceLocator,我们还可以获得系统配置、事件管理器,我认为它更灵活。以下是解决方案:
步骤1:使Album\Model实现ServiceLocatorAwareInterface,以便服务定位器能够注入Album\Model
use Zend\ServiceManager\ServiceLocatorAwareInterface,
Zend\ServiceManager\ServiceLocatorInterface;
class Album AbstractModel implements ServiceLocatorAwareInterface, InputFilterAwareInterface
{
protected $serviceLocator;
public function setServiceLocator(ServiceLocatorInterface $serviceLocator)
{
$this->serviceLocator = $serviceLocator;
return $this;
}
public function getServiceLocator()
{
return $this->serviceLocator;
}
}
步骤2,在config或Module.php中将Album\Model注册为服务
class Module
{
public function getServiceConfig()
{
return array(
'invokables' => array(
'AlbumModel' => 'Album\Model\Album',
),
);
}
}
步骤3,按服务而非类调用AlbumModel:
public function addAction()
{
if ($request->isPost()) {
$album = $this->getServiceLocator()->get('AlbumModel');
}
}
步骤4,现在您可以在AlbumModel中使用DbAdapter:
'adapter' => $this->getServiceLocator()->get('Zend\Db\Adapter\Adapter');
通过以上步骤,您可以在任何地方共享任何服务,希望这对您有所帮助。要验证“用户名是否已存在”,请执行以下简单的Service Manager配置设置方法,如:
//config/autoload/global.php
return array(
'db' => array(
'driver' => 'Pdo',
'dsn' => 'mysql:dbname=zf2tutorial;host=localhost',
),
'service_manager' => array(
'factories' => array(
'Zend\Db\Adapter\Adapter' => function ($serviceManager) {
$adapterFactory = new Zend\Db\Adapter\AdapterServiceFactory();
$adapter = $adapterFactory->createService($serviceManager);
\Zend\Db\TableGateway\Feature\GlobalAdapterFeature::setStaticAdapter($adapter);
return $adapter;
}
),
),
);
这将在控制器操作中首先设置静态db适配器(如果之前未使用适配器)
并将以下数组添加到getInputFilter()中:
我希望这对每个人都有帮助。
干杯 谢谢,这很有帮助。在我的控制器中,我已替换$album=new album();使用$album=$this->getServiceLocator()->get('Application/Model/album');正如你所说。它在add操作中非常有效,但是如何在editAction中使用它呢?我不知道如何替换这个:$album=$this->getAlbumTable()->getAlbum($id)?谢谢很好,但我还需要如何在编辑操作中实现它。我使用了$album=$this->getServiceLocator()->get('Application/Model/album');在add action中,它可以工作,但我花了很长时间才发现我必须在我的操作$this->getServiceLocator()->get('Zend\Db\Adapter\Adapter');。。。首先设置静态适配器(即使Zend\Form\FieldSet实现了ServiceLocatorAwareInterface,但在Zend\Form\FieldSet中似乎不起作用的内容)
return array(
'db' => array(
'driver' => 'Pdo',
'dsn' => 'mysql:dbname=zf2tutorial;host=localhost',
),
'service_manager' => array(
'factories' => array(
'Zend\Db\Adapter\Adapter' => function ($serviceManager) {
$adapterFactory = new Zend\Db\Adapter\AdapterServiceFactory();
$adapter = $adapterFactory->createService($serviceManager);
\Zend\Db\TableGateway\Feature\GlobalAdapterFeature::setStaticAdapter($adapter);
return $adapter;
}
),
),
);
public function myAction () {
$this->getServiceLocator()->get('Zend\Db\Adapter\Adapter');
...
}
array(
'table' => 'users',
'field' => 'username',
'adapter' => \Zend\Db\TableGateway\Feature\GlobalAdapterFeature::getStaticAdapter();
)