如何在Zend Framework 2中将db adapter设置为Validator RecordExists

时间:2012-12-11 10:41:02

标签: zend-framework2

我正在尝试将验证器RecordExists添加到我的表单中,但是我收到错误'no db adapter present'。如何将db adapter设置为此验证器?我使用骨架应用程序中的示例,我正在尝试做这样的事情(是的,我知道$ dbAdapter未定义:)我正在搜索解决方案如何将此变量更改为db adapter资源):

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

}

3 个答案:

答案 0 :(得分:7)

您可以创建“相册/模型/相册”工厂并将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;
   }
}

您可以通过调用$this->getDbAdapter();

在输入过滤器中获取dbAdapter

请记住,ServiceLocator会在控制器中获取Album模型,否则dbAdapter将无法在您的模型中使用。

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

答案 1 :(得分:1)

要对“用户名是否已存在”进行验证,请执行以下简单的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;
         }
      ),
   ),
);

并在控制器操作中首先设置静态数据库适配器(如果之前没有使用适配器)

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

并将以下数组添加到getInputFilter()中:

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

我希望这对每个人都有帮助。 干杯!!!

答案 2 :(得分:0)

@kierzniak提供了一个注入DbAdapter的好方法,我的解决方案几乎相同,不仅注入了DbAdapter而且注入了完整的ServiceLocator,通过注入ServiceLocator,我们还可以获得系统配置,EventManagers,我认为它更灵活。这是解决方案:

步骤1:Make 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;
    }
}

Step2,将Album \ Model注册为config或Module.php中的服务

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

Step3,按服务调用AlbumModel但不是类:

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

Step4,现在你可以在AlbumModel中使用DbAdapter:

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

通过以上步骤,您可以在任何地方共享任何服务,希望这可以帮助您。