如何DB适配器设置为验证RecordExists Zend框架2(How to set db ada

2019-07-04 23:15发布

我试图验证RecordExists添加到我的形式,但我得到错误“目前还没有数据库适配器”。 我怎样才能分贝适配器设置到这个验证? 我使用框架的应用实例,我试图做这样的事情(是的,我知道,$将对DBAdapter未定义:)我在寻找解决方案如何改变这个变量为DB适配器资源):

namespace Album\Model;

use Zend\InputFilter\Factory as InputFactory;     // <-- Add this import
use Zend\InputFilter\InputFilter;                 // <-- Add this import
use Zend\InputFilter\InputFilterAwareInterface;   // <-- Add this import
use Zend\InputFilter\InputFilterInterface;        // <-- Add this import

class Album implements InputFilterAwareInterface
{
    public $id;
    public $artist;
    public $title;
    protected $inputFilter;                       // <-- Add this variable

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;
}

// Add content to this method:
public function setInputFilter(InputFilterInterface $inputFilter)
{
    throw new \Exception("Not used");
}

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'),
            ),
            'validators' => array(
                array(
                    'name'    => 'Db\RecordExists',
                    'options' => array(
                        'table' => 'album',
                        'field' => 'title',
                        'adapter' => $dbAdapter
                    ),
                ),
            ),
        )));

        $this->inputFilter = $inputFilter;
    }

    return $this->inputFilter;
}

}

Answer 1:

您可以创建“相册/型号/专辑”工厂,注入将对DBAdapter到相册样板。

'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;
   }
}

您可以通过调用您的输入滤波器获得将对DBAdapter $this->getDbAdapter();

记住要相册样板通过服务定位控制器,否则将对DBAdapter不会在你的模型缴费。

$album = $this->getServiceLocator()->get('Application/Model/Album');


Answer 2:

对于做验证的“用户名已存在或不”,不要像服务管理器的配置设置以下简单的方法:

//配置/自动加载/ 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;
         }
      ),
   ),
);

这在你的控制器动作先设置静态数据库适配器(如果您之前没有使用适配器)

public function myAction () {
    $this->getServiceLocator()->get('Zend\Db\Adapter\Adapter');
    ...
}

并添加以下数组getInputFilter():

array(
  'table'   => 'users',
  'field'   => 'username',
  'adapter' => \Zend\Db\TableGateway\Feature\GlobalAdapterFeature::getStaticAdapter();
)

我希望这有助于大家。 干杯!!!



Answer 3:

@kierzniak提供注入将对DBAdapter一个很好的方式,我的解决方案几乎是一样的,不仅注入将对DBAdapter但完整的服务定位,通过注入ServiceLocator的,我们也可以得到系统CONFIGS,EventManagers,我觉得这是更灵活。 这里是解决方案:

第1步:制作专辑\型号实现ServiceLocatorAwareInterface,使服务定位器将能够注入专辑\型号。

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;
    }
}

第二步,注册专辑\型号在配置或Module.php服务

class Module
{
    public function getServiceConfig()
    {
        return array(
            'invokables' => array(
                'AlbumModel' => 'Album\Model\Album',
            ),
        );
    }
}

第三步,调用AlbumModel的服务,但不是一类:

public function addAction()
{
    if ($request->isPost()) {
        $album = $this->getServiceLocator()->get('AlbumModel');
    }
}

第四步,现在你可以在AlbumModel使用将对DBAdapter:

'adapter' => $this->getServiceLocator()->get('Zend\Db\Adapter\Adapter');

通过以上的步骤,你可以分享任何在任何地方的服务,希望能帮助你。



文章来源: How to set db adapter to Validator RecordExists in Zend Framework 2