Symfony + Doctrine + MD5 issue

I want to use the MD5 sql function in my Symfony package, so I added the file ( https://gist.github.com/Basster/2774738 ) to \ MyCompany \ MyBundle \ DQL \ MD5Function.

Then I changed the config.yml file:

# app/config/config.yml
doctrine:
    orm:
        auto_generate_proxy_classes: "%kernel.debug%"
        naming_strategy: doctrine.orm.naming_strategy.underscore
        auto_mapping: true
        # Added configuration for MD5 function
        entity_managers:
            default:
                dql:
                    string_functions:
                        MD5: MyCompany\MyBundle\DQL\MD5Function

But I have the following error:

InvalidConfigurationException on line ArrayNode.php 309: unrecognized options "naming_strategy, auto_mapping" in the "doctrine.orm" section

+4
source share
2 answers

You mix the configuration of the manager of one object and the manager of managing several entities.

You should use:

# app/config/config.yml
doctrine:
    orm:
        auto_generate_proxy_classes: "%kernel.debug%"
        naming_strategy: doctrine.orm.naming_strategy.underscore
        auto_mapping: true
        # Added configuration for MD5 function
        dql:
            string_functions:
                MD5: MyCompany\MyBundle\DQL\MD5Function

or

# app/config/config.yml
doctrine:
    orm:
        entity_managers:
            default:
                auto_generate_proxy_classes: "%kernel.debug%"
                naming_strategy: doctrine.orm.naming_strategy.underscore
                auto_mapping: true
                # Added configuration for MD5 function
                dql:
                    string_functions:
                        MD5: MyCompany\MyBundle\DQL\MD5Function
+20
source

, , DQL symfony. , SHA1, SH1 MD5, .

MD5

namespace Football\FrontendBundle\DQL;

use Doctrine\ORM\Query\AST\Functions\FunctionNode;
use Doctrine\ORM\Query\Lexer;
use Doctrine\ORM\Query\Parser;
use Doctrine\ORM\Query\SqlWalker;

class Md5 extends FunctionNode
{
    public $value;

    public function parse(Parser $parser)
    {
        $parser->match(Lexer::T_IDENTIFIER);
        $parser->match(Lexer::T_OPEN_PARENTHESIS);
        $this->value = $parser->StringPrimary();
        $parser->match(Lexer::T_CLOSE_PARENTHESIS);
    }

    public function getSql(SqlWalker $sqlWalker)
    {
        return 'MD5(' . $this->value->dispatch($sqlWalker) . ')';
    }
}

config.yml

doctrine:
    orm:
        dql:
            string_functions:
                MD5: Football\FrontendBundle\DQL\Md5

,

namespace Football\FrontendBundle\Repository;

use Doctrine\ORM\EntityRepository;
use Doctrine\ORM\Query;

class CountryRepository extends EntityRepository
{
    public function findOneCountryBy($id)
    {
        return
            $this
                ->createQueryBuilder('c')
                ->select('c, MD5(c.code) AS code')
                ->where('c.id = :id')
                ->setParameter('id', $id)
                ->getQuery()
                ->getSingleResult(Query::HYDRATE_SCALAR);
    }
}
+1

All Articles