jrk/levenshtein-bundle

Levenshtein function bundle for symfony2 and doctrine

Installs: 42 304

Dependents: 0

Suggesters: 0

Security: 0

Stars: 3

Watchers: 2

Forks: 3

Open Issues: 0

Type:symfony-bundle

v1.1.1 2018-05-21 16:49 UTC

This package is auto-updated.

Last update: 2024-05-04 04:44:23 UTC


README

Setup

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;

Usage

  • 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();
    }
?>