JrkLevenshteinBundle requires Symfony and Doctrine
- Using composer
Add jrk/levenshtein-bundle as a dependency in your project's composer.json file:
{
"require": {
"jrk/levenshtein-bundle": "dev-master"
}
}
Update composer
php composer update
or
php composer.phar update
- Add JrkLevenshteinBundle to your application kernel
<?php
// app/AppKernel.php
public function registerBundles()
{
$bundles = array(
// ...
new Jrk\LevenshteinBundle\JrkLevenshteinBundle(),
);
}
- Yml configuration
# app/config/config.yml
doctrine:
orm:
entity_managers:
default:
dql:
numeric_functions:
levenshtein: Jrk\LevenshteinBundle\ORM\Doctrine\DQL\LevenshteinFunction
levenshtein_ratio: Jrk\LevenshteinBundle\ORM\Doctrine\DQL\LevenshteinRatioFunction
or if you're using shortened configuration instead of full configuration
#app/config/config.yml
doctrine:
orm:
# if you have these lines
auto_generate_proxy_classes: '%kernel.debug%'
naming_strategy: doctrine.orm.naming_strategy.underscore
auto_mapping: true
# you've to directly put the dql lines, see below
dql:
numeric_functions:
levenshtein: Jrk\LevenshteinBundle\ORM\Doctrine\DQL\LevenshteinFunction
levenshtein_ratio: Jrk\LevenshteinBundle\ORM\Doctrine\DQL\LevenshteinRatioFunction
- Console usage
Install functions
php app/console jrk:levenshtein:install
That console line will install 2 functions in your database. You can inspect these functions in your mysql database by typing in a phpmyadmin sql prompt or in your favorite mysql client
SHOW FUNCTION STATUS;
- Using QueryBuilder
<?php
public function getUserByFirstname($tolerance = 3) {
$queryBuilder = $this->_em->createQueryBuilder()
->select('user')
->from('FooBundle:User','user')
->where('LEVENSHTEIN(user.firstname,:searchString) <= :tolerance')
->setParameter('searchString',$searchString)
->setParameter('tolerance',$tolerance)
;
return $queryBuilder->getQuery()->getResult();
}
?>
- Using DQL
<?php
public function getUserByFirstname($tolerance = 3) {
$dqlString = '
SELECT user
FROM FooBundle:User user
WHERE LEVENSHTEIN(user.firstname,:searchString) <= :tolerance
';
$query = $this->_em->createQuery($dqlString)
->setParameter('searchString',$searchString)
->setParameter('tolerance',$tolerance)
;
return $query->getResult();
}
?>