calling adapter in model

2019-08-01 19:49发布

Module :

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

Now how can I call my ‘dbadapter’ in anywhere in the model while I need it for

new \Zend\Validator\Db\RecordExists(
   array(
        'table'   => 'user',
        'field'   => 'username',
        'adapter' => $dbadapter
    )
)

please tell me the way to call adapter in anywhere.

-thanks.

Edit :

I followed this 'Get default zend db adapter in table model' thread and did the following in my ‘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;
    }

}

but I got this error,

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

Why is that ?

1条回答
成全新的幸福
2楼-- · 2019-08-01 20:35

I made it like the following,

Module :

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

Controller :

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

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;
    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;
    }
}
查看更多
登录 后发表回答