Symfony World blog is not maintained anymore. Check new sys.exit() programming blog.

NetBeans code templates for Symfony

Scene from "The Smurfs" by Pierre Culliford (intruduced in 1958)

This short tutorial shows how to add code templates into the NetBeans IDE. It's based on version 6.8 of NetBeans including PHP support.


why using code templates?

Code templates fasten up the work of a programmer, when many templates are configured. You may define a combination of few keys to make the IDE insert a big piece of code for you. Tired of copy-pasting all the time? If so, code templates are just for you!


Go into the 'Tools' menu and choose 'Options':






Choose 'Editor' section and 'Code templates' tab:






Now you're ready to define your custom code templates. The abbreviation is the combination of keys you shall type to make the IDE insert your code (which is present in 'expanded text'). In the example above, you shall type the phrase 'sfexe' and then press tab to get action method template. Easy, isn't it? I hope you'll to find it useful!

Custom query for admin generator

Scene from "Cabaret" by Bob Fosse (1972)

introduction

Do you want to use a specific query for retrieving objects used for running an admin generator? For example, you want to display information about related database records but you see that generating such page generates lots of SQL queries? It's very easy to set a custom query for admin generator in Symfony!


solution

You need to do only 1 simple thing - override the buildQuery method in your actions.class.php (of an admin module).

protected function buildQuery()
{
  return parent::buildQuery()
    ->leftJoin('p.Customer c');
}

parent::buildQuery() is the default query retrieving data for the admin. You may modify it as muh as you want (or you may even build your query from the beginning). The most important use of this feature is decreasing number of SQL queries, which makes an admin panel work a lot faster!

admin generator validation - validating integer input

An easy problem solving solution this time... If you want to have an integer validator for an input, the first thing you may think of using is:

$this->validatorSchema ['number'] =
  new sfValidatorInteger(array('required' => false));

and such code may cause you a lot of troubles because of the following error:
"%value%" is not an integer.
which may be frustrating to find the source of the problem. Integer validator handles validation on schema input value, which means, more or less, complex input (associative array). Need to use the sfValidatorSchemaFilter class:

$this->validatorSchema ['number'] =
  new sfValidatorSchemaFilter('text', new sfValidatorInteger(array('required' => false)));

And that's it!