How to call an Oracle Function in Symfony 2

Hi All,

How are you doing? Seems like my fingers do not want me to stay away from Symfony2 despite the Spring Bank Holiday. ūüôā

Today, I am going to talk about how to call an Oracle Function in Symfony2. A function (aka stored function) is a collection of SQL statements which is used to perform various different activities within the Database. The good thing about the function is you can write your chunks of SQL inside the function and call it using a name to initiate. I will be focusing on how to call an Oracle Function in Symfony2 and if you would like to know more about Oracle functions please click here.

First, let’s take a look at a sample function which I have created and is saved in Oracle 12c Database.

create or replace 
FUNCTION testFunc123(old_bname IN VARCHAR2) 
   RETURN VARCHAR2 IS 
   BEGIN 
      IF old_bname = 'TEST' THEN
          RETURN 'Y';
      ELSE
        RETURN 'N';
      END IF;  
    END;

The role of the above created Oracle function is to check whether the value of ‘old_name’ is equal to ‘TEST’ or not. If it is ‘TEST’, then the function will return ‘Y’ otherwise ‘N’. So, now what we going to do is call this function inside a Symfony2 controller. This is really easy, thankfully to wonderful Doctrine2.

Continue reading

Share

Symfony 2, five quick tips

Hi all, today I am going to share with you five quick, but important tips in Symfony 2 programming.

  1. Use bundles where appropriate, such as to wrap set of reusable functionalities (business logic), reusable services and reusable views. But, don’t over use bundles as this will include unnecessary headache on performance and maintenance in long run.
  2. If you use a functionality in more than five different places, make it as a services function (Service container) . Also you might need to inject varies different services to the services class (Injecting services). In that case make sure you inject starting from the most specific to most generic service. If possible try not to inject big entities such as Container, unless you left out with no other option.
  3. Make the view less brainy. What I mean by that is, let the view (such as twig) to do less work than the Controller. In some rare cases this can be hardly achievable but in most cases you could easily make the view less brainy by doing most of the logical operations inside the Controller, Repository, Services etc. Let the view to render without it doing complex logical operations.
  4. Use Doctrine Queries Language (DQL) to query data¬†instead of Repository class (such as $em->getRepository()) to get the maximum efficiency of your project. If you are querying from one table, using either option won’t make a big difference. But if you are querying varies tables through joins, definitely use DQL.
  5. In terms of security, try not to send parameters in the URL as it is, if possible (including non sensitive parameters). What I mean by that is, if you use¬†URL patterns like this ‘route_to_page/{id1}/{id2}’, encrypt¬†those ‘id1’ and ‘id2’ through a custom encrypt¬†function created by you. Then decrypt (using a custom decrypt function created by you) those ‘id1’ and ‘id2’ in the Controller to continue with your logic.
    (Ideally through an injected services class which contains your security functionalities – How to create and inject services ? )
    See below,

Continue reading

Share

Create your first bundle | Symfony 2 | Beginners Guide

Hi All,

Welcome back. Today I am going to focus your attention on bundles. Well, the bundle is the most famous term you will frequently hear, during your adventure in Symfony 2. Actually, the bundle is a really handy feature which has been introduced by any PHP framework (not quite sure about rest of the frameworks) so far. Well, a Bundle is a package, collection of views, entities, controllers and so on. You can organize your code around bundles in a very smart way. And if you use bundles smartly, you can develop a system with highly independent features. Why not we discuss some examples before creating bundles,

  • Example 1 : Assume you are creating a basic employee management system. The basic features in a employee¬†management system are employee registration, employee profile, employee payroll, employee attendance¬†and so on. For employee registration, we have a separate UI with relevant registration form fields, we have separate functions (let’s call them controllers), such as checking existence of the employee, validity of the user input values, validity of employee national insurance number (in UK), and so on. And also, we got separate tables to insert values once the user submitted his/her employee registration form. Likewise we can group things (mainly model, view and controllers) in a functional way (but, it’s totally up to you).¬†We call¬†¬†these grouped things as ‘bundles‘ in Symfony 2.
  • Example 2: Let’s say you are creating a shopping cart system. In a shopping cart system, we know there are products, categories, sub categories, payment methods and so on. In here, we can have a separate bundles¬†for products, categories and so on. For an example, inside categories bundle, we can nicely arrange things which are mostly relevant to managing categories. These things can be views (html and associated js, css files and so on), models (tables, schema and so on) and controllers (functions such as adding a category, deleting category and so on). So, in here, we are arranging bundles in a logical way. Likewise, you can organize your bundles for products, sub categories and so on. Continue reading
Share