I guess that this is really trivial and stupid question, but I don't know how to install Doctrine Extensions - https://github.com/beberlei/DoctrineExtensions in my Symfony2 project. I need them because of the MONTH, YEAR functions. Where should I put their folder? And should I put the whole DoctrineExtensions
folder? And where to write this:
<?php
$classLoader = new \Doctrine\Common\ClassLoader('DoctrineExtensions', "/path/to/extensions");
$classLoader->register();
In a separate file? Where to put it and how to call it?
And then is this all I need to use them:
public function findOneByYearMonthDay($year, $month, $day)
{
$emConfig = $this->getEntityManager()->getConfiguration();
$emConfig->addCustomDatetimeFunction('YEAR', 'DoctrineExtensions\Query\Mysql\Year');
$emConfig->addCustomDatetimeFunction('MONTH', 'DoctrineExtensions\Query\Mysql\Month');
$emConfig->addCustomDatetimeFunction('DAY', 'DoctrineExtensions\Query\Mysql\Day');
Thank you very much in advance and sorry once again for the question, but I couldn't find a tutorial (which makes me feel even more guilty, because I guess it's too trivial when there isn't even a tutorial)
You can install it via composer. Just add it to your composer.json and then php composer.phar update beberlei/DoctrineExtensions
"beberlei/DoctrineExtensions": "*",
Then you can register functions to your ORM
doctrine:
orm:
auto_generate_proxy_classes: %kernel.debug%
entity_managers:
default:
auto_mapping: true
dql:
datetime_functions:
MONTH: DoctrineExtensions\Query\Mysql\Month
YEAR: DoctrineExtensions\Query\Mysql\Year
There also is a nice fork by wiredmedia of @beberlei which includes even more datetime_functions like DATE() itself:
This is an unsanctioned fork of https://github.com/beberlei/DoctrineExtensions since he seems to have gone off grid and is not merging pull requests.
Unfortunately Version 0.1 just includes the fork and not all the functions. We are waiting for a stable release:
Please create taged stable version for set in my composer #2
But you can add them manually unitl a stable version is out.
Here is how to use DoctrineExtensions in the context of Symfony using the DoctrineBundle.
First install the package DoctrineExtensions:
composer require beberlei/doctrineextensions
Then add to your doctrine configuration (doctrine.yaml
file) the DQL functions you want to include in your application:
doctrine:
# Register types this way in the dbal config part
dbal:
types:
carbondatetime: DoctrineExtensions\Types\CarbonDateTimeType
# Register DQL functions in the ORM part
orm:
dql:
string_functions:
FIND_IN_SET: DoctrineExtensions\Query\Mysql\FindInSet
numeric_functions:
SIN: DoctrineExtensions\Query\Mysql\Sin
datetime_functions:
DATE: DoctrineExtensions\Query\Mysql\Date
This is an example, feel free to adjust to your needs (you can remove sections).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With