Tag Archives: symfony2

Symfony2 Forms and Doctrine Entity: Unique field values validation

If you’re using Symfony2 forms paired with Doctrine entities, you might sometimes need to validate fields so that the values in that field is always unique. Say – “username” field or “email” field. Two users must not have the same username or email.

There is a form validation constraint – “Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity” which integrates nicely with your entity and allows unique values validation when the form is submitted. Let’s look at some code samples to see how it works:

Here, the email and username fields will be checked for duplicate values and the defined message will be displayed if duplicate values are found.

Symfony2 & Doctrine: Custom Entity Repositories

In Symfony2, we can easily map a custom repository class for an entity. This allows us to extend the functionality of the repository. For example, we can add custom methods for even more customizable data retrieval. Without any ado, let’s jump into codes.

First, create the class which will act as the repository. It must extend “Doctrine\ORM\EntityRepository”. Here, we’re creating a repository for “User” objects inside “WeCodePHP\HomeBundle\Entity”. You can get the entity manager using “$this->getEntityManager()” inside a method.

Now, in the original entity, we need to define which class should be used as the repository class. We do that like this:

The repositoryClass tells Symfony2 about the custom repository.

Now, we can call our custom methods with ease:

Custom repository is what we very often do in models in other frameworks.

Symfony2: Loading external libraries

I have always wanted to play with Symfony since I heard many cool things about the framework. Finally got some time and started studying the framework. I would save the experience for another blog post. However I am going to share how easy it is to load external libraries.

Libraries supporting PSR-0 standard
If the library was written according to the PSR-0 standard, it is already well structured with the proper implimentation of namespace. For such libraries, just open up “app/autoload.php” and add the namespace to the array which is passed to the $loader->registerNamespaces() method. Your codes should look something similar to:




Libraries supporting PEAR Conventions
Libraries with support for PEAR conventions could be easily loaded as well. Just pass the autoloader prefix (the first part of the class name or the root directory name followed by underscore) to the $loader->registerPrefixes() method.

Something like:



In such scenarios the class MyLib_MyClass should reside inside __DIR__.’/../vendor/mylib/src/MyLib/MyClass.php – as the PEAR standard suggests.



The Old and Incompatible ones
PHP has a very long history of evolution and there are plenty of community maintained codes which haven’t yet been updated to reflect the modern development standards. There are many 3rd party libraries out there which are old (was written before PHP 5.3) but useful. While there is no direct way to load them into Symfony, thanks to the nifty autoloader, we can easily use them with Symfony2.

Say, we have a class like this:



It is a single file library – no namespace, no PEAR naming. What do we do? We make it PEAR compatible :)

# First rename the file according to the class name – KillingCurse.php

# Now create a vendor and source directory under the “vendor” directory of the app. Say, we name “voldemort” as the vendor and name the source directory “src”. The file structure would be- “/vendor/voldemort/src”. This is not a requirement, we could just create any directory under the “/vendor” directory but follwing the Symfony naming convention is a good practice.

# We need to create a prefix, so we create a directory named “Spells” (/vendor/voldemort/src/Spells)

# We put the KillingCurse.php under the Spells directory and define a Spells_KillingCurse class in the same file like this:



The full file now looks like:



# Now we register the “Spells_” prefix to the “app/autoload.php”:



# We use the following codes in the controller:



Points to be Noted:

– \Spells_KillingCurse::load(); We are using a static call which does nothing. But this makes the autoloader load the file in which the bare KillingCurse lives. We used a static call instead of having to initiate an object.

– An extra class without any functionality is added just so that entire file is loaded. Any functions, classes or constants will get imported into the global namespace.

– Be sure to use the “\” before the classes, functions etc. You’re inside a namespace and you must refer to the global namespace to use items in the global scope.

– There could be many other ways to do this. Like I mentioned at the top of this post, I am just beginning Symfony2. I would love to hear new ideas or suggestions to improve the codes.