Symfony 2 fetching data using getRepository() and DQL

Hi folks,

Happy New Year !!!

Sorry I have been really busy past few months. I always wanted to continue my blog but the amount of workload I had to go through was immense. Fortunately, I found sometime to come up with a new post.

Well, this brief post is about the impact on fetching data mainly using getRepository and DQL (Doctrine Query Language), which you might have already known before. But I took some time out to highlight this fact, mainly for developers who are new to Symfony 2.x & Doctrine 2.x., which can heavily impact on performance on your Symfony application in long run.

Imagine you have 3 tables which are ‘users’, ‘user_roles’ and ‘user_categories’. There are unidirectional many-to-one relationships between ‘users’ to ‘user_roles’ and ‘user_roles’ to ‘user_categories’. Refer to Users.orm, UserRoles.orm and UserCategories.orm files below.

Continue reading

Share

Thoughts on data persist in Symfony 2

Hi All,

Today I will be discussing about persisting form submitted data in Symfony 2. Data persisting is all about inserting/updating records to/in a table in database. In this example I assume that you have a form ready which will contain few fields in order to insert values to the database. This is exactly what I am going to do in the following scenario. I will have a table (Entity) called ‘user_details (UserDetails)’. I will be passing some values from a form (which is not here) to the ‘insertUserDetails‘ action in ‘UserDetails‘ controller. Then using that action, I will be inserting new user details to the ‘user_details‘ table.

Well, in this post I would like to have an open ended discussion regarding data persisting. The approach you are using can be different from mine but still do the same intended job. More or less performance can vary, lines of codes can vary, efficiency can vary and so on.  You can see the controller and function below. First take a look at the code and I will explain the code below. Continue reading

Share

Accessing configuration parameters in Symfony 2

Hi,

Today, in this small post, I am going to show you how to access a parameter which is defined in config.yml file, inside the program.

Imaging you are creating/have a parameter called ‘is_something_enabled‘  inside config.yml. You need to access this parameter inside your code, let’s say inside a controller. This is how you do this. First of all let’s see how the parameter is defined in config.yml file.

parameters:
  is_something_enabled: true
Accessing this in a controller is very easy. Actually you don’t need to be in a controller to access these parameters. What you need is the container. If you can inject the container, to any of your class, that’s pretty much it. Here is how you access the above parameter in a controller,
$this->container->getParameter(‘is_something_enabled’);
In a container injected constructor class, this is how you access it. (Refer to my previous post to see how to inject the container to constructor)
Share

Five months with Symfony 2

Hi folks,

Well, today I am going to briefly discuss on couple of things which I have learned throughout my journey in Symfony 2.  Well the journey is still ongoing and this brief discussion is about first couple of months, in exact terms first five months.

At very beginning, as an experienced CodeIgnitor and CakePHP developer, I found that Symfony 2 is quite confusing for me. The bundle concept started to bring some nightmares on me. The ‘bundle’ concept is a whole new experience for a developer who is sound with PHP frameworks such as CodeIgnitor and CakePHP. But today I found that, I am so lucky to get my self around with ‘bundles’, which makes developers life very straightforward and easy. I honestly believe Symfony 2, SensioLabs, Fabien did the correct thing by introducing ‘bundles’ for Symfony 2 which was not there for Symfony 1.x versions. Bundles let you organise your features, functions within your project in most efficient manner. The knowledge of arranging your projects according to functions, features within bundles will come to you once you spend sometime playing with Symfony 2. Then you come to a point, you get a feeling that how the things should get arranged in terms of Symfony 2, bundles point of view. So try your self by playing with Symfony 2, creating bundles under various vendors, etc, try your self how the bundle is created.

Continue reading

Share

Running console commands using Process component in Symfony 2

Hi All,

Today I am going to talk about process component in Symfony 2. Process component is a useful library which can be used to run console commands as it is. The main driving factor to discuss this topic is, recently I had an issue in running multiple console commands in Symfony 2. I tried using doRun method in Application. It worked for commands like doctrine:mapping:import and doctrine:generate:entities but not for doctrine:generate:form.  Even it worked for doctrine:generate:entities, I didn’t managed to generate a single entity in a bundle. I had to generate all entities for the whole bundle. In other words “php app/console doctrine:generate:entities ACMETestBundle:Person –no-backup” did not worked. So I had a really difficult time with this until someone suggested me about the process component when I posted this question in StackOverflow.. To be honest, I didn’t had the opportunity to explore process component thoroughly before that. But I found that process component is a really cool library which we can use to run console commands in Symfony 2. I thought it might be useful for someone who is trying to run console command using the application. If you have struggled running console commands using Application’s, run or doRun methods, this post is especially for you.

Continue reading

Share