Skip to main content

Use Symfony2 to create template/skeleton code

Pre-requisite: A lamp environment, i will be using a vagrant lamp box (scotch-box) i created earlier.

To understand Symfony2 there is nothing better than getting ones hands dirty. We will try to use doctrine to build models, create views using twig and maybe use a dash of frontend magic using backbone or something similar.

For the test project I chose a sales bundle where we will make models for item, item type and sale.

Using symfony2 command line to create a model.

Because i have used vagrant scotch-box to provision my dev environment, i need to first make sure i do vagrant up to start the VM. then i do vagrant ssh this will take me to the ssh

locate where you have symfony installed, in my case it is /var/www/public/sandbox and give the following command:

php app/console generate:bundle

pick a name any name....well not actually, we need to be following coding standards set out in the symfony guide to ensure we name our bundle like this Bundle, so in my case i didn't have a vendor, so i chose a project name in this case it was Test\SandboxBundle, the bundle was named TestSandboxBundle and relevant files were created for me.

I just chose the defaults and had symfony create all it deemed good and wholesome...this will be additionally helpful if we peep under the hood as to what and where things are normally stored.

Now our bundle is created, we can create some models so in the symfony root in my case /var/www/public/sandbox i type the following command in order to generate the entities.

Entities we need: We are attempting to create a pos system, so we will have the following models:

ItemType - Types of item eg. Toys, Bakery, Drinks etc
  Id, Name (string:50) & Description (string: 250)

Item - The item, this will linked with item type
  Id, ItemTypeId (int), Name (string:50), Description (string:250), Price (descimal), Opening Stock (int)

Sale - the actual sale which will link to the item.
  Id, Date (date time), ItemId (int), Quantity(int), CustomerId(int)

Customer - the customer, to make things easier, we will create only 2 customers 1 pos & the other for WEB orders.
  Id(int), Name (string:50), Address (string:250), postcode (string:12)

So we generate these models one by one, the options i have chosen are as follows:

The Entity shortcut name
: TestSandboxBundle:

Configuration format for all (yml, xml, php, or annotation) [annotation]: annotation

Note that the primary key will be added automatically (named
id). When it asks do you want to generate an empty repository class say yes.

You can now start using the generated code!  



Popular posts from this blog

Create a drupal development environment using docker.

Currently at work i am using docker to streamline and speedup of development and deployment to testing and production, I am using symfony currently and our app is dockerised, i wanted to do the same for drupal based apps.

I searched for a official drupal docker repo and i did find this.

Unfortunately there was very terse explanation but pretty much worked, i found a few hiccups along the way so wanted to share what i did.

Install docker for your environment. Lots of details are available at explaing docker and installing docker for your environment.

The docker repo holds the drupal container at, following the instructions this is how to create the drupal container:

In order to use my code base we need to mount the volume with the -v directive as below, we do this in one command:

docker run -v /$HOME/work/drupal8:/var/www/html -p 8080:80 --name mydrupal -d drupal

this will create…

Use feeds module to import XML data in Drupal 7

Recently i had to setup a xml import process to import data from an external supplier on a regular basis, the filename and format was not changing and i could choose where to store the file. Following is how i set it up.

First we need to download the following modules

feeds admin ui

enable the above modules.

We will need a sample XML so this will be the one we will use for this demonstration:

<?xml version="1.0" encoding="utf-8" ?>
<!-- Sample for demonstration of processing XML feeds with feeds module -->

How do you expose your legacy database tables to views in drupal 6 Part 1

This is a 2 part post, that shows how I exposed a legacy table to views in drupal and used the incredible power of views to create many combinations static and conditional views. This was very helpful for my client.

I had a requirement that warranted exposing existing database table to views, so that views can contain data from this table.  To enable this to happen, the following steps need to be taken.
In a custom module you need to first define the views_api hook as following:
function candidate_views_api(){ $view = array(); $view = array('api' => 2, 'path' => drupal_get_path('module','candidate'). '/views'); return$view; }
candidate module was a custom module which I already had, so it made sense to add this hook here. 
The important bit here is telling the drupal sub-system where the views file will reside. 
Then create a file with your <modulename> in my case it was, this file should have two hooks, one …