Laminas component form validator RecordExists, what's the best way to pass dbadapter into?

The configurations of the modules looks good.


Do you override the standard database adapter?

The delegator tries to fetch a database adapter via the name Laminas\Db\Adapter\AdapterInterface from the service container and sets the adapter to the requested service. The adapter itself must be an instance of Laminas\Db\Adapter\Adapter .

Another option is to debug the delegator class for the database adapter:

no, I didn’t overwrite

public function getServiceConfig()
    {
        return [
            'factories' => [
                Model\BaseFuncModel::class => function($container){ //注册基础函数库到服务管理器里面
                    return new Model\BaseFuncModel();
                },

                /** discourse comment_main start */
                Model\CommentMainTable::class => function($container){
                    $tableGateway = $container->get(Model\CommentMainTableGateway::class);
                    return new Model\CommentMainTable($tableGateway);
                },
                Model\CommentMainTableGateway::class => function($container){
                    $dbAdapter = $container->get(AdapterInterface::class);
                    $resultSetPrototype = new ResultSet();
                    $resultSetPrototype->setArrayObjectPrototype(new Model\CommentMain());
                    return new TableGateway('comment_main', $dbAdapter, null, $resultSetPrototype);
                },
                /** discourse comment_main end */

You can find $dbAdapter = $container->get(AdapterInterface::class); in the above codes.

Can I send the whole project to you? I think it is hard to figure out the problem just from code snippet.

Do you have a GitHub repository for it?

no, no github repository, it is our company project deposit on gitlab(built in our inner network, company network.)

I can zip it as a package, and send to your email with sensitive info removed.

Look on GitHub, there you will find my email address.

Email sent, pls let me known, whether you’ve got it.

the problem comes from url /application/forum/threadmainpost

And it works. I do not get an error.

Enable the development mode: The Skeleton Application - tutorials - Laminas Docs

Otherwise the merged application config is cached.

I run with command php -S 0.0.0.0:8080 -t public

Ok, I will check again. thank you for your time and efforts! I will feedback soon.

You are right!!! It works when I change it into develop mode.
Before I run with php command in local and in development mode.
But I forgot that I change it into product mode when I push to gitlab.

Thank you so much for your time and efforts!!!

I still have a question.

I wanna to write a custom Validator, can I use a factory to inject dependency to the customed Validator via __construct()?

As you see in my project, I need to valid a formHash element which need to use the BaseFuncModel->formHashProcess()
I wanna to inject BaseFuncModel::class into my customed Validator.

We need to summarise the most important parts:

  • do not create input filters decoupled from your application
  • do not create forms decoupled from your application
  • use the form element manager to fetch forms

Then you can use custom form elements, validators, filters and more, which themselves have dependencies.

See also the related tutorials:

Please create separate topic for this question because it does not belongs to validators and database adapters. Thanks!

Ok, I will sort my mind and open a new topic later.

Hi, I know this is a very late reply. I would suggest looking into LmcUser module. You’ll find record exists usage there. It may give you an idea about how to use it in your project. Thanks!

The current thread is not about how to use the validator, but how to set the database adapter. Therefore I do not see how your answer help here because this part is missing in your component.

Hi @froschdesign, you’re following this post and many posts which are on this forum. I’ve only tried to give my suggestion based on the question. My answer may not be the exact answer he/she is looking for. But might help if he/she is really trying to learn or developing something. Apologies in advance if I’ve taken this topic off the course.

The problem is: I have searched the LmcUser repository, but it does not answer the question of this forum thread. More than that, the part is completely missing.