Skip to content

A better Levenshtein function for your Symfony 5 project

License

Notifications You must be signed in to change notification settings

AnthonyDUSART/Levenify

Repository files navigation

Symfony Levenify Bundle

A better Levenshtein function in your Symfony project

How to install Levenify

  • Step 1 : Install package
composer req levenify/levenify-bundle
  • Step 2 : Setup your database with command
bin/console levenify:install
  • Step 3 : Setup your config.yml
# app/config/config.yml
doctrine:
    orm:
        dql:
            numeric_functions:
                levenshtein: Levenify\LevenifyBundle\ORM\Doctrine\DQL\Levenshtein
                levenshtein_ratio: Levenify\LevenifyBundle\ORM\Doctrine\DQL\LevenshteinRatio

How to use Levenify

  • With Query Builder
<?php
    public function getProductByName($searchString, $tolerance = 3) {
        $queryBuilder = $this->_em->createQueryBuilder()
           ->select('p')
           ->from('Product::class', 'p')
           ->where('LEVENSHTEIN(p.name, :searchString) <= :tolerance')
           ->setParameter('searchString', $searchString)
           ->setParameter('tolerance', $tolerance)
        ;

        return $queryBuilder->getQuery()->getResult();
    }
?>
  • With DQL
<?php
    public function getProductByName($searchString, $tolerance = 3) {

        $dqlString = '
            SELECT product
            FROM Product::class p
            WHERE LEVENSHTEIN(p.name, :searchString) <= :tolerance
        ';

        $query = $this->_em->createQuery($dqlString)
           ->setParameter('searchString', $searchString)
           ->setParameter('tolerance', $tolerance)
        ;

        return $query->getResult();
    }
?>

--- Q: What is the difference between basic Levenshtein function and Levenify ?

--- A: First parameter of the function can be composed of multiple words !

About

A better Levenshtein function for your Symfony 5 project

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages