Archive for January 2012

London Sencha Touch user group Roundup

Tuesday evening saw our inaugural London Sencha Touch User group kick off in the salubrious surroundings of the Forward Technology amphitheatre with a few slices of pizza and a beer or two.

After a brief intro the evening kicked off with Patrick Hamann giving us a fairly pain free (tech ‘lite’) introduction to the Touch framework and giving us his opinions on where he thought it fitted within the plethora of development tools available.

Firstly giving us a bit of a low down into the development stack and providing us with some incites into when using the Touch framework might be a consideration, he went on to provide some useful examples of apps that have successfully utilised the framework and touched upon the useful templating and leveraging of SASS that is inherent in the framework.

Slides for Patrick’s talk can be viewed here:

Following Patrick, Matt Spence from the University of Kent providing us with an interesting view of how their dev team were pushed to consider developing ‘something’ mobile for the university. Whilst he admitted this wasn’t the best approach to starting a project he went on to demonstrate how, with little development time available, his team were able to produce some rather useful student tools such as building locator (using maps), desktop availability (how many desktops are available in the various public spaces) and timetabling accessed via a student login. Matt demonstrated how the readily available university data can be made available as a useful mobile service for their students and how the rapid application development nature of Sencha Touch enabled them to get their mobile ‘something’ out to a testable market within a 4 week period. We look forward to seeing the results of the next 4 week sprint on the project.

Slides for Matt’s talk can be viewed here:

Rob Squires completed the trio of talks with a discussion primarily focussed on building cross-platform mobile apps using Sencha Touch 2.0 but also giving us a more technical ‘under the hood’ view of the framework. Rob showed us a re-usable technique he had developed utilising the MVC nature of the framework to tackle various challenges that building cross-platform apps can throw such as differences in UI/UX that mobile users come to expect with their particular flavour of devise and OS.

Slides for Rob’s talk can be viewed here:

Some Doctrine 2 Best Practices

Doctrine 2 has a very powerful database abstraction layer and features an Object Relational Mapper (ORM) that makes it very easy to manage relationships between the data in your application and provides tools that aid rapid application development (RAD).

With Doctrine also comes DQL, a powerful language for querying your object models,  which is similar to SQL.

Use of Doctrine abstracts away the database concepts of tables and columns, rather you think in terms of objects, entities, properties and relationships. While this is very useful, it is very important for the developer to have an understanding of the underlying SQL that is generated from using the entity manager/DQL and querying your objects/entities.

Without an understanding of the code that Doctrine generates, it is relatively easy to build an inefficient and slow application, here are a few pitfalls that I have found are easy to fall into, and some tips that may be useful for people starting with Doctrine, or if you are trying to get a little more performance out of an existing application.

For the rest of this post I will use the cookie cutter example of a Blog website with article, user and comment object models (entities). In terms of object mapping we will assume all mapping is specified with the default (lazy) fetch method, and MySQL as our DB.

1. Always write a DQL statement for querying your object models

Doctrine repositories provide some ready made methods for fetching your objects such as “->findAll()” and “->findBy()”. These provide some convenience and brevity when initially developing your application but use of these functions is less flexible when further developing your application and can lead to some of the pitfalls below.

By writing the DQL yourself, you will expand your understanding of Doctrine, you have more control of the data you are trying to query and people reading your code will have a better understanding of what you are trying to do (and will find it easier to update and extend your code).

2. Beware of lazy loading when querying entities with associations

If you do not specify an ‘eager’ fetch in your object mapping, or do not explicitly fetch your relationships in your DQL statements (or if you query your objects using the Doctrine methods mentioned in point 1) Doctrine will not fetch those associations in the SQL it generates. This is helpful as you do not want to be loading resources you do not need, however when you call a related object, Doctrine needs a method of fetching these objects and it does this by lazy loading through proxies.

For example if you have an article entity, with associated comments and you want to display a list of all your articles with number of comments on the front page of your blog, you may do something like this (this code is purely for demonstration).

$articles = $article_repository->findAll();

foreach($articles as $article)
{
    echo $article->getTitle();
    echo count($article->getComments());
}

Using this code, Doctrine will initially generate a query for all the rows in the article table. As we have not ‘fetched’ the comment association, Doctrine will lazy load in the comment object for each article via an additional SQL statement. For a table containing 10 rows, this will result in 11 database queries.

//$em is the entity manager
$qb = $em->createQueryBuilder();

$qb->select('Article', 'Comment')
    ->from('Entity\Article', 'Article')
    ->leftJoin('Article.comment', 'Comment');

$query = $qb->getQuery();

$result = $query->getResult();

return $result;

Instead, by using DQL and specifying our relationships, we can use the above code to fetch the same data with 1 query vs the  11 (unnecessary) database queries generated previously. This problem grows with each additional row in a given database.

3. Use array hydration for read only actions

The Doctrine documentation recommends the use of array hydration for read only actions, this is in part due to the extra overhead (increased memory needed from loading proxies and the like) associated with hydrating objects.

If you are just displaying lists of items, or lists of entity properties for a single item (for example a product page, a list of recent blog articles, or indeed most front end operations!) you can use:

$query->getResult(Query::HYDRATE_ARRAY);

This approach can also prevent any accidental lazy-loading problems that may arise as discussed previously (due to the lack of proxies!)

4. By default Doctrine will fetch all of the properties for a given entity

This doesn’t exactly fall under a best practice, indeed making any changes here too early in the development cycle would fall under premature optimisation. Nonetheless consider the following:

$qb->select('Article')
->from('Entity\Article');

It is somewhat wasteful in terms of resources to fetch data from your database that you aren’t going to use. The above code snippet would fetch all the properties for the article entity.

Rather than selecting the whole object, in DQL you can specify specific properties to return in your select statement:

$qb->select('Article.title')
->from('Entity\Article');

However this will always return a flat array, regardless of relationships between your entities. Instead you can select and hydrate partial objects in your DQL, which will give you an object or a nested array representing your object graph (depending on the hydration selected)

$query = $em->createQuery("SELECT partial Article.{title} Entity\Article Article");

As stated in the Doctrine documents it is important to note here that this optimisation can lead to more fragile code, it may not always be clear (especially if working as part of a team, and working code written by others) that object properties have not been fetched.

5. Use prepared statements

Using prepared statements helps to prevent SQL injection attacks that have proven problematic for developers and businesses alike – Sony notably exposed a lot of user data in Summer 2011. See below for a quick example of how to use prepared statements in Doctrine.

$qb = $em->createQueryBuilder();

$qb->select('Article', 'Comment')
    ->from('Entity\Article', 'Article')
    ->leftJoin('Article.comment', 'Comment')
    ->where($qb->expr()->eq('Article.title', ':filter_title')
    ->setParameter('filter_title', $some_user_input);

$query = $qb->getQuery();

$result = $query->getResult();

return $result;

The Doctrine documentation provides a more in depth explanation for achieving this.

6. Be aware of what Doctrine is up to

If you are using Symfony 2, you can use the in-built profiler for monitoring the number of queries generated by doctrine. If you are using Zend Framework you can refer to my previous blog post covering some methods for profiling doctrine 1 or 2.

 

I hope these tips prove helpful if you are just getting started with Doctrine, or if you have been using it for a little while. If you have any tips of your own let us know on twitter @uv_d or leave a comment

Come to our first London Sencha Touch User Group Meetup on Tues 24th Jan 2012

We’ve got a great venue sorted and finalised a date of Tuesday, January 24, 2012, 6:30 PM to come and chat, share, listen and learn about all things Sencha Touch. If you are interested in cross platform mobile development (native and web) built in web technologies such as HTML5 and JavaScript then please come and join us.

Our user group can be joined here: http://www.meetup.com/London-Sencha-Touch-User-Group

We’ve been lucky enough to have Forward host the event in their amphitheatre in the heart of Camden. Details of how to get there are below:

Forward Technology
Floor 2, Centro 3, 19 Mandela Street, London, NW1 0DU, United Kingdom
http://forwardtechnology.co.uk/venue

Transport Links:

  • Underground: Camden Town, Mornington Crescent (Both Northern Line)
  • Train: Euston, St Pancras International, Camden Road
  • Bus: 274, 46

Being our first event we will be starting off soft with overviews into Sencha Touch and mobile development with an aim of getting a feel from the community as how we want to structure the events and what we all want to learn and share with each other. With that in mind we are on the look out for those of you who want to give a talk at this or future events. They can be anything from 5min lightening talks up to 30min in-depth slots: show us something you’ve worked on or something neat that we’d find useful. Please do get in touch if you want to contribute (it means you wont have to listen to us too much!)

We’re really looking forward to meeting the rest of the Sencha community in London and learning more about mobile development.

UPDATE…. WE HAVE AN AGENDA:

We have 3 sessions planned for this event each lasting 20-30 minutes with an open floor Q&A session following each. We will start the group with a beer and a slice of pizza and move onto the 3 main sessions detailed below:

An Introduction to Sencha Touch

Patrick Hamann is a Front-end developer living and working in London with a keen passion for creating great mobile experiences using HTML5 frameworks. His talk will be a taster session introducing you to Sencha Touch giving some insight into why you might consider it as a solution.

  • What is Sencha Touch?
  • A walk-through of the Sencha Touch structure
  • Capabilities and UI features
  • Real-world examples of it out in the wild

Kent Mobile: Building a mobile “something” for the University of Kent

Matt Spence is from the IS web team at Kent University and will be providing some incites into their experience with Sencha Touch.

  • The pressure to deliver something on a mobile platform
  • How we arrived at Sencha
  • What we’ve done
  • Where we’re going

Building cross platform apps using Sencha Touch

Rob Squires is a Lead Developer with UVd with a penchant for utilising well established programming methodologies with new technologies. He is a Sencha fan and keen squash player.

His talk will focus on some techniques and considerations involved in building cross platform apps using Sencha Touch

  • A quick look at common cross platform challenges
  • Some techniques that could be useful for a Sencha project requiring cross platform deployment

Search