Nerds who love the symfony-project
6 Mar
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.
4 Responses for "Symfony application configuration - in your database"
Hi
In our project we had the same need to store configuration values in the database.
To be consistent with the symfony API of sfConfig class, we made a myConfig class with static methods get/set.
We also use Memcache in this class to reduce the performance impact of the values to be stored in database. That way if multiple calls are made to retrieve a value, only the first one will hit the database.
And when the set method is called, we both update database and memcache.
Eric
[...] Shared a link on Google Reader. Symfony application configuration - in your database [...]
I don’t understand why you did that… Simplest way is to set the “attribute” column as UNIQUE in your model, and then just use MyAppConfigPeer::retrieveByAttribute($attribute); which has automatically generated and doesn’t need any object because of the static state
Could me tell me how can i define a custom field in edid using getter / setter with generator.yml?
Thanks,
Tapu
Leave a reply