Skip to content

Latest commit

 

History

History
66 lines (58 loc) · 1.78 KB

README.md

File metadata and controls

66 lines (58 loc) · 1.78 KB

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 !