symfonynerds.com

Nerds who love the symfony-project

Archive for March, 2009

Symfony 2! A sneak peak

Fabian has recently posted a presentation he has given on Symfony 2.0.

The framework has undergone a complete overall making it faster and light-weight. Checkout all the details of Symfony 2.0 here.

If you are defining your schema.yml and have a field that is a decimal value, you will find you will need to somehow pass through the precision (maximum number of digits to the left of the point) and the scale (total number after the decimal point). The documentation for the syntax of this is quite poor.
For all those that want to know, in Symfony 1.0 this is how you would define a decimal:

my_decimal: { type: decimal, size: 8,3 }

For Symfony 1.1+, the Syntax is a bit different:

my_decimal:
      type: decimal
        size:    8
        scale:   3

Where 8 is the precision and 3 is the scale. Hope this helps.

Symfony application configuration can be configured for each application in the app.yml file. One of they key problems with this is if you want an end-user to be able to change some of this application configuration, they can’t. You would need to do that for them.

A good way to fix this is to store your applications configuration in the database. This comes with a performance cost (extra calls to your database) - so its up to you to decide if this is the most efficient solution for you.

Here is an outline on how to get started building your application’s config in the database.

Step 1: Define your model
Create the columns you want to store for your application config. In this instance, we are just going to store name and value pairs (ie Attribute & Value). Here is a sample below:

propel:
  #Application configuration table
  my_app_config:
    _attributes:                { phpName: MyAppConfig }
    id:                         { type: integer, required: true, primaryKey: true, autoIncrement: true }
    attribute:                  { type: varchar(255) }
    value:                      { type: varchar(255) }
...

Once you have finished that, go ahead and build your model

Step 2: Extend Your MyAppConfig class
So the next step is for us to make it easy to access application configuration. For us to do this, go to your lib/model/MyAppConfig.class.php and extend your base class as follows:

<?php
class MyAppConfig extends BaseMyAppConfig
{
 
  /**
   * Returns a value of an attribute string for my app's config table
   * @param string attribute name
   * @return string The value of that attribute
   * @author eHabib
   */
	public function lookupValueFromAttribute($attribute){
  		$c = new Criteria();
  		$c->add(MyAppConfigPeer::ATTRIBUTE, $attribute);
  		$result = new MyAppConfig();
  		$result = MyAppConfig::doSelectOne($c);
		return ($result->getValue());
	}
}

Step 3: Use it!
Let’s say that in our database we have the following attribute/value paris:

Attribute: app_support_email, Value: myemail@mydomain.com
Attribute: app_base_url, Value: http://whatever

You could access this data in your code by doing this:

$myConfig = new MyAppConfig();
$appEmail = $smyConfig->lookupValueFromAttribute("app_support_email");
$appURL = $smyConfig->lookupValueFromAttribute("app_base_url");
...
echo $appEmail.",".$appURL; //lets print it

That’s it!

Step 4: Configure it via your front-end
The last step (if you wish) is to allow the end-user to update the value of these fields. To do this, you can build a form to update these fields, or use Symfony’s admin generator.

One of the things that might not be clear to Symfony developers is when to use app.yml for your application config and when to use project-level configuration for all your applications.

If your configuration parameter is for all your applications you should place this configuration setting within your config/ProjectConfiguration.class.php (Symfony 1.2.x). This will save you duplicating the config within each app’s app.yml and you only have to specify it once.

Setting up project-level config
You can set project-wide config by editing this file.

....
class ProjectConfiguration extends sfProjectConfiguration
{
  public function setup()
  {
    //In my example below I want to tell my app if it should use a proxy or not 
    sfConfig::set('use_proxy', '1');	//1 = True, 0=False
    $this->enableAllPluginsExcept(array('sfDoctrinePlugin', 'sfCompat10Plugin'));
  }
}

Anything you place in your project-level config can be accessed from any application (or Symfony task) by using the standard sfConfig::get() static method.

Note: You would normally have to get the application context if you want to read an application config within a Symfony Task. In this case, you wouldn’t because the config is set at a project level.