Home » Php » php – How to implement beberlei doctrine extensions in zend framework 2

php – How to implement beberlei doctrine extensions in zend framework 2

Posted by: admin July 12, 2020 Leave a comment

Questions:

i dont know how to integrate the beberlei doctrine-extensions:
https://github.com/beberlei/DoctrineExtensions
in Zend Framework 2 with Doctrine Module.
I installed it with composer:

“beberlei/DoctrineExtensions”: “dev-master”

I tried in my module.config.php from th application module:

'doctrine' => array(
    'driver' => array(
         __NAMESPACE__ .'_driver' => array(
            'class' => 'Doctrine\ORM\Mapping\Driver\AnnotationDriver',
            'cache' => 'array',
            'paths' => array(__DIR__ . '/../src/'.__NAMESPACE__.'/Entity')
        ),
        'orm_default' => array(
            'drivers' => array(
               __NAMESPACE__.'\Entity' => __NAMESPACE__. '_driver'

            )
        )
    ),
    'configuration' => array(
        'orm_default' => array(
            'string_functions' => array(
                'GroupConcat' => '/vendor/beberlei/DoctrineExtensions\Query\MsySql\GroupConcat'
            )
        )
    )
),

but this Exception was thrown:

Fatal error: Class
‘/vendor/beberlei/DoctrineExtensions\Query\MsySql\GroupConcat’ not
found

How to&Answers:

The configuration doesn’t need to reference the /vendor/beberlei folder as this is handled by the autoloader.
The configuration should probably look something like this :

'doctrine' => array(
    'driver' => array(
         __NAMESPACE__ .'_driver' => array(
            'class' => 'Doctrine\ORM\Mapping\Driver\AnnotationDriver',
            'cache' => 'array',
            'paths' => array(__DIR__ . '/../src/'.__NAMESPACE__.'/Entity')
        ),
        'orm_default' => array(
            'drivers' => array(
               __NAMESPACE__.'\Entity' => __NAMESPACE__. '_driver'

            )
        )
    ),
    'configuration' => array(
        'orm_default' => array(
            'string_functions' => array(
                'GroupConcat' => 'DoctrineExtensions\Query\Mysql\GroupConcat'
            )
        )
    )
),

Answer:

After installing with Composer

"beberlei/DoctrineExtensions": "dev-master"

use this code in your service class:

$config = $this->getEntityManager()->getConfiguration();
$config->addCustomStringFunction('GROUP_CONCAT', 'DoctrineExtensions\Query\MySq\GroupConcat');

now you can use ‘GROUP_CONCAT’ in your DQL like this:

    $dql = "SELECT GROUP_CONCAT(elem.name) FROM ..... GROUP BY ...";
$query = $this->getEntityManager()->createQuery($dql);
return $query->getArrayResult();

Answer:

It worked for me by

1- install DoctrineExtensions with composer

composer require beberlei/DoctrineExtensions

2- Add this line to autoload_classmapp.php

'DoctrineExtensions' => 'vendor/beberlei/DoctrineExtensions'

3- Add this to module.config.php

'doctrine'=>array(
'configuration' => array(
        'orm_default' => array(
            'datetime_functions' => array(
                'date' => 'DoctrineExtensions\Query\Mysql\Date',
                'date_format' => 'DoctrineExtensions\Query\Mysql\DateFormat',
                'dateadd' => 'DoctrineExtensions\Query\Mysql\DateAdd',
                'datediff' => 'DoctrineExtensions\Query\Mysql\DateDiff',
                'day' => 'DoctrineExtensions\Query\Mysql\Day',
                'dayname' => 'DoctrineExtensions\Query\Mysql\DayName',
                'last_day' => 'DoctrineExtensions\Query\Mysql\LastDay',
                'minute' => 'DoctrineExtensions\Query\Mysql\Minute',
                'second' => 'DoctrineExtensions\Query\Mysql\Second',
                'strtodate' => 'DoctrineExtensions\Query\Mysql\StrToDate',
                'time' => 'DoctrineExtensions\Query\Mysql\Time',
                'timestampadd' => 'DoctrineExtensions\Query\Mysql\TimestampAdd',
                'timestampdiff' => 'DoctrineExtensions\Query\Mysql\TimestampDiff',
                'week' => 'DoctrineExtensions\Query\Mysql\Week',
                'weekday' => 'DoctrineExtensions\Query\Mysql\WeekDay',
                'year' => 'DoctrineExtensions\Query\Mysql\Year',
            ),
            'numeric_functions' => array(
                'acos'  => 'DoctrineExtensions\Query\Mysql\Acos',
                'asin' => 'DoctrineExtensions\Query\Mysql\Asin',
                'atan2' => 'DoctrineExtensions\Query\Mysql\Atan2',
                'atan' => 'DoctrineExtensions\Query\Mysql\Atan',
                'cos' => 'DoctrineExtensions\Query\Mysql\Cos',
                'cot' => 'DoctrineExtensions\Query\Mysql\Cot',
                'hour' => 'DoctrineExtensions\Query\Mysql\Hour',
                'pi' => 'DoctrineExtensions\Query\Mysql\Pi',
                'power' => 'DoctrineExtensions\Query\Mysql\Power',
                'quarter' => 'DoctrineExtensions\Query\Mysql\Quarter',
                'rand' => 'DoctrineExtensions\Query\Mysql\Rand',
                'round' => 'DoctrineExtensions\Query\Mysql\Round',
                'sin' => 'DoctrineExtensions\Query\Mysql\Sin',
                'std' => 'DoctrineExtensions\Query\Mysql\Std',
                'tan' => 'DoctrineExtensions\Query\Mysql\Tan',
            ),
            'string_functions' => array(
                'binary' => 'DoctrineExtensions\Query\Mysql\Binary',
                'char_length' => 'DoctrineExtensions\Query\Mysql\CharLength',
                'concat_ws' => 'DoctrineExtensions\Query\Mysql\ConcatWs',
                'countif' => 'DoctrineExtensions\Query\Mysql\CountIf',
                'crc32' => ' DoctrineExtensions\Query\Mysql\Crc32',
                'degrees' => 'DoctrineExtensions\Query\Mysql\Degrees',
                'field' => 'DoctrineExtensions\Query\Mysql\Field',
                'find_in_set' => 'DoctrineExtensions\Query\Mysql\FindInSet',
                'group_concat' => 'DoctrineExtensions\Query\Mysql\GroupConcat',
                'ifelse' => 'DoctrineExtensions\Query\Mysql\IfElse',
                'ifnull' => 'DoctrineExtensions\Query\Mysql\IfNull',
                'match_against' => 'DoctrineExtensions\Query\Mysql\MatchAgainst',
                'md5' => 'DoctrineExtensions\Query\Mysql\Md5',
                'month' => 'DoctrineExtensions\Query\Mysql\Month',
                'monthname' => 'DoctrineExtensions\Query\Mysql\MonthName',
                'nullif' => 'DoctrineExtensions\Query\Mysql\NullIf',
                'radians' => 'DoctrineExtensions\Query\Mysql\Radians',
                'regexp' => 'DoctrineExtensions\Query\Mysql\Regexp',
                'replace' => 'DoctrineExtensions\Query\Mysql\Replace',
                'sha1' => 'DoctrineExtensions\Query\Mysql\Sha1',
                'sha2' => 'DoctrineExtensions\Query\Mysql\Sha2',
                'soundex' => 'DoctrineExtensions\Query\Mysql\Soundex',
                'uuid_short' => 'DoctrineExtensions\Query\Mysql\UuidShort',
            ),

        )
    )
)

Answer:

My reputation is too low to comment but:

'doctrine' => array(
'driver' => array(
     __NAMESPACE__ .'_driver' => array(
        'class' => 'Doctrine\ORM\Mapping\Driver\AnnotationDriver',
        'cache' => 'array',
        'paths' => array(__DIR__ . '/../src/'.__NAMESPACE__.'/Entity')
    ),
    'orm_default' => array(
        'drivers' => array(
           __NAMESPACE__.'\Entity' => __NAMESPACE__. '_driver'

        )
    )
),
'configuration' => array(
    'orm_default' => array(
        'string_functions' => array(
            'GroupConcat' => 'DoctrineExtensions\Query\**MsySql**\GroupConcat'
        )
    )
)),

should be:

'GroupConcat' => 'DoctrineExtensions\Query\**Mysql**\GroupConcat'

notice the Mysql instead of MsySql