SymfonyCon 2014 – Day #2

10704413_818986304791371_2806292674175600213_o

The second day was a pretty funny for me. This one started when I left my friends and had gone to listen to presentations and they chose to eat breakfast instead. I think this happened just because of hunger – I decided to write to Anne Sophie and make a presentation during lighting talks. Crazy, huh? But, I’ll talk about this later, because a lot happened meanwhile.
Continue reading

Rabbit behind the scenes

rabbit

Queuing in the background – getting started with RabbitMQ message broker

In PHP business logic is usually put right in action’s method or just behind it. Hence, every little piece of delaying and long-running code will be processed with a request. The problem is almost undetectable if a user sends an e-mail but with more complex actions it may take a little bit longer than preferred.
Continue reading

CakePHP with Symfony’s2 router

Zzyzx_road

A couple of months ago I have started my adventure with CakePhp and as every Symfony’s developer I thought that any other framework except Symfony is a piece of crap. Day by day and step by step I began to realize that’s not so bad as it seemed to be in the beginning. Well, the second version of CakePhp still has a lot old-fashioned patterns, singletons or lack of tests, but I can live with that. I saw a lot of better or worse frameworks in my life. However, one module remains a bitter aftertaste – the router.
Continue reading

Query manipulation with open-close principle

solid

Just a week ago I sat down in front of my computer and decided to try to create a common solution for injecting easy pagination. It’s an ordinary problem and I wouldn’t waste my time for doing it every time when I need to display more than 10 or 50 elements on the screen. So, as usually I started with installing Symfony2 and KnpPaginatorBundle, but after a couple of minutes I realized that the implementation can be more general than I assumed at the beginning. I could use the same design also for filtering or manipulating queries… Continue reading

Symfony2 Bootstrap CRUD bundle – 10% time in action

In XSolve each developer has 10% of his working time to create special projects and bring some innovative ideas into life. Most often we use that time to prepare open source bundles and you can find some of them at http://knpbundles.com/organization/xsolve-pl/profile . Please feel free to use them!

In this article I would like to present our brand new creation – Symfony2 Boostrap CRUD bundle and make it easy for you to implement it in your next assignment.
Continue reading

(English) xsolve-google-auth-bundle

Few days ago we released a first edition of xsolve-google-auth-bundle (you can fin it on GitHub) which is connecting google authorization and authentication with FOSUserBundle.

After installation you can define where users can sign in with google account and, if they do not exist in your database when new accounts would be created. Bundle allows to open an application for all users or only from specific domain.
Continue reading

Full text searching in Symfony2

Generally, while working with simple search engines programmers have to anticipate issues involving users’ typos and mistakes. In this article I would like to present, compare and contrast three main types of full text searching in MySQL database – LIKE, SOUNDEX and MATCH AGAINST as these methods are some of the most popular solutions as well as they are readable and easy to implement.

Code examples below are prepared in Symfony 2.1 edition with preinstalled Doctrine 2 ORM extension.

LIKE

It comes as no surprise that LIKE is a first option to consider. It’s a matching pattern used in SQL simple regular expression comparison.

It’s often implemented to search a piece of text in database’s table. There are two main types of regular expression signs:

  • % – matches any number of characters
  • _ – matches exactly one number of character


In example

SELECT login WHERE login LIKE “%@xsolve.pl”;
this statement should return us all logins in domain xsolve.pl.

But we can choose another way and use it to search all logins in all kind of xsolve namespaces:

SELECT login FROM User WHERE login LIKE “%@xsolve.%”;

Symfony 2 implementation

In Symfony 2 framework with Doctrine 2 implementing this kind of searching is very simple, because this part is built in QueryBuilder.

You can use it in repositories’ classes:

$query = $this>createQueryBuilder(‘u’)
->where(‘u.login LIKE :login’)
->setParameter(‘login’,”%$login%”)
->getQuery();

return $query->getResult();

You can find more in MySQL documentstion’s site:
http://dev.mysql.com/doc/refman/5.0/en/string-comparison-functions.html

SOUNDEX

Soundex is a more advanced function to compare text – not only it matches strings but also guesses if there are any typos. Unfortunately, it doesn’t work with foreign characters (for example with Polish or Russian language).
Soundex function returns a soundex string from parameter and that means two strings that sound almost identical should have the same soundex.
Additionally all non-alphabetical characters in parameters are ignored and all international alphabetical characters outside the A-Z range are treated as vowels.

In example

SELECT title FROM posts WHERE title SOUNDS LIKE “Atricle”;
this statement should return a ‘title’ of an ‘Article’ if there is one at all.

Likewise:
SELECT title FROM posts WHERE SOUNDEX(title) = SOUNDEX(“Atricle”);

Symfony2 implementation

Unfortunately, Soundex isn’t supported by Doctrine2.2, however there is a simple method to implement a DQL function.

First of all, we have to create a DQL class with a new method which returns string to SQL.

Then we configure Doctrine to find our new DQL option:

All right. Now we can use it in queryBuilder:

$query->andWhere(‘SOUNDEX(q.street) = SOUNDEX(:street)’);

You can read more about this method in MySQL documentation:
http://dev.mysql.com/doc/refman/5.0/en/string-functions.html#function_soundex

MATCH AGAINST

MySQL performs full text search using match()…against() syntax. MATCH() takes a comma-separated list that indicates columns to be searched. AGAINST() takes a string to search for and an optional modifier that determines type of search to perform.
The search string must be a string value that is constant during query evaluation. This rules out, for example, a table column because it can be different for each row.

This syntax has three types of searches:

  • Boolean – interprets the search string using the rules of a special query language. The IN BOOLEAN MODE modifier specifies a boolean search.
  • Natural – interprets the search string as a phrase in natural human language (a phrase in free text). Full-text searches are natural language searches if no modifier is given.
  • Query expansion – is a modification of a natural language search. The search string is used to perform a natural language search. Then words from the most relevant rows returned by the search are added to the search string and the search is done again. The query returns the rows from the second search. The WITH QUERY EXPANSION modifier specifies a query expansion search.

In example

SELECT * FROM film_text WHERE MATCH (description) AGAINST (‘redeem’);

Symfony2 implementation

This case is similar to SOUNDEX method, but we will take a look at Jérémy Hubert‘s code from https://gist.github.com/1234419

We need to add a new function into config.yml file as in SOUNDEX example. After that we can use it in Symfony application:

You can read more about this method in MySQL documentation:
http://dev.mysql.com/doc/refman/5.0/en/fulltext-search.html

Papi.
Piotr Pasich

Uwierzytelnianie użytkownika przez Google OAuth API

Podczas prac nad jednym z ostatnich projektów spotkaliśmy się z problemem autentykacji użytkowników na stronie przez Google API. Jednak pomimo ogromnego zaangażowania community nie udało nam się znaleźć gotowego i działającego bundle’a przygotowanego dla Symfony 2.1, dlatego chcielibyśmy podzielić się jednym z wypracowanych przez zespół XSolve rozwiązań tego zagadnienia.
Continue reading