How to set db adapter to Validator RecordExists in

2020-03-05 03:01发布

问题:

I'm trying to add validator RecordExists to my form but I get error 'no db adapter present'. How can I set db adapter to this validator? I use examples from skeleton application and I'm trying to do something like this (yes, I know that $dbAdapter is undefined :) I'm searching solution how to change this variable to db adapter resource ):

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

}

回答1:

You can create 'Album/Model/Album' factory and inject dbAdapter to 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;
        },
    ),
),

Now we have to implement setDbAdapter and getDbAdapter methods:

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

You can get dbAdapter in your input filter by calling $this->getDbAdapter();

Remember to get Album model in controller by ServiceLocator, otherwise dbAdapter won't be avaiable in your model.

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


回答2:

For doing the validation for "Username already exists or not", Do the following simple way of Service Manager config settings like:

// 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;
         }
      ),
   ),
);

and this in your controller action to set the static db adapter first (if you did not use the adapter before)

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

and add the following array into getInputFilter():

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

I hope this helps to everyone. Cheers!!!



回答3:

@kierzniak provided a good way to inject DbAdapter, My solution is almost the same, not only inject DbAdapter but the full ServiceLocator, By injecting ServiceLocator, we could also get system configs, EventManagers, I think it is more flexible. Here is the solution:

Step 1: Make Album\Model implements ServiceLocatorAwareInterface, so that the service locator will be able to injected into 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, Register Album\Model as a service in config or Module.php

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

Step3, call AlbumModel by service but not a class:

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

Step4, now you could use DbAdapter in AlbumModel:

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

By above steps, you could share any of services in anywhere, hope this could help you.