0

I'm binding entity manager in the container like this with the configurations including the DATE extension function

.......
Config::class                                   => create(Config::class)->constructor(
        require CONFIG_PATH . '/app.php'
    ),

    EntityManager::class                    => function (Config $config) {

        $configuration = ORMSetup::createAttributeMetadataConfiguration(
            $config->get('doctrine.entity_dir'),
            $config->get('doctrine.dev_mode'),
        );
        $configuration->addCustomDatetimeFunction('DATE', 'DoctrineExtensions\Query\MySql\Date');

        return new EntityManager(new Connection($config->get('doctrine.connection'), new Driver()),
        $configuration
    );},

It works locally. I am able to use the date function to validate user input like this

$v->rule(
            function ($field, $value, $params, $fields) use (&$data) {

                if ($value === '') {
                    return \true;
                }


                $dateToBook = explode('T', $value)[0];
                $today = (new DateTime())->format('Y-m-d');


                $checkDay = $this->entityManager
                    ->createQuery("SELECT DATE(j.dueDate) FROM App\Entity\Job j where DATE(j.dueDate) = :booked AND DATE(j.dueDate) > :today")
                ->setParameters([
                    'booked'=> $dateToBook,
                    'today' => $today
                    ])->getArrayResult();
                    
                    if (count($checkDay) > 0 && $data['forceBooking'] === '') {
                        return false;
                    } elseif (count($checkDay) > 0 && $data['forceBooking'] !== '') {
                        return true;
                    }
                return true;
            },
            'dueDate'
        )->message('Warning: There is an event on this day');

But after deploying it live, the date function fails with this error Class DoctrineExtensions\Query\MySql\Date ; not found File: /home/XXXXX.cloudwaysapps.com/vxbtnydbjs/public_html/vendor/doctrine/orm/lib/Doctrine/ORM/Query/Parser.php\nLine: 3616

I found this instruction

If you are using DoctrineExtensions standalone, you might want to fire up the autoloader:
<?php
$classLoader = new \Doctrine\Common\ClassLoader('DoctrineExtensions', '/path/to/extensions');
$classLoader->register();

but I don't know when to put this. I have tried it in several places. in my container bindings and in my boostrap file like this

<?php

declare(strict_types = 1);

use Doctrine\Common\ClassLoader;
use Dotenv\Dotenv;

require __DIR__ . '/vendor/autoload.php';
require __DIR__ . '/configs/path_constants.php';

$dotenv = Dotenv::createImmutable(__DIR__);
$dotenv->load();

$classLoader = new ClassLoader('DoctrineExtensions', __DIR__ . '/vendor/beberlei/doctrineextensions');
$classLoader->register();

return require CONFIG_PATH . '/container/container.php';

I've pulled the update into the files on the server but the error persists. I'm not even sure if the doctrine class loader is the issue. It still works perfectly on my local server (on my laptop). Any suggestions please?

Zuby
  • 5
  • 5

1 Answers1

0

I found the solution. I had to bind it in entitymanager this way.

$configuration->addCustomDatetimeFunction('DATE', Date::class);

instead of

$configuration->addCustomDatetimeFunction('DATE', 'DoctrineExtensions\Query\MySql\Date');

so my IDE could include the namespace in the script.

the server found the class afterwards

Zuby
  • 5
  • 5