Monday, February 13, 2012

Symfony: get the working environment(Development or Production) | DecentMind

Symfony: get the working environment(Development or Production) | DecentMind

While developing an application in Symfony , You might want to run some part of your code in development environment and other part in production environment. In such situation you can use sfConfig::get class to get the environment in which your application is running, And you can use this as a condition to run your code.
web/index.php is the file where you declare your Symfony environment:

define('SF_ROOT_DIR',realpath(dirname(__FILE__).'/..')); define('SF_APP','myapp'); define('SF_ENVIRONMENT', 'prod'); define('SF_DEBUG', false); require_once(SF_ROOT_DIR.DIRECTORY_SEPARATOR.'apps'.DIRECTORY_SEPARATOR.SF_APP.DIRECTORY_SEPARATOR.'config'.DIRECTORY_SEPARATOR.'config.php'); sfContext::getInstance()->getController()->dispatch();

Now you can get the environment using following:

$sf_environment = sfConfig::get('sf_environment');

$sf_environment variable will return string as ‘prod’ and you can use it as condition

$sf_environment = sfConfig::get('sf_environment'); if($sf_environment==prod) {     do this..... } else {     do this..... }

In-fact you can use sfConfig::get to access any configuration of your application declared in yml files.

Happy Coding….. :)



More than 3 requests, I'll translate this to Chinese.
超过3个请求,我就会把这篇文章翻译成中文。

Thursday, February 02, 2012

MySQL: find week start/end given week number | Terminally Incoherent

MySQL: find week start/end given week number | Terminally Incoherent

I have a table with some dated records. I wanted to do some weekly reports on this data. MySQL has a nifty function dubbed WEEK() which will return a week number. It allows you to break your data into week long intervals very easily. For example, the following query will tell me how many records came in each week:

SELECT COUNT(*), WEEK(mydate) FROM mytable GROUP BY WEEK(mydate)

The problem here is, that the output is totally meaningless to me as I do not have a clue what does week 36 mean. What I really want is to have on the screen is a nice, human readable date interval – the beginning and ending date of any given week.

Surprisingly, this turns out the be a major pain in the ass. There is no simple function that will yield a week interval (or start/end date of a week) given a week number. You have to find these dates manually. Here is how I did it:

SELECT     COUNT(*) AS reports_in_week,     DATE_ADD(mydate, INTERVAL(1-DAYOFWEEK(mydate)) DAY),     DATE_ADD(mydate, INTERVAL(7-DAYOFWEEK(mydate)) DAY) FROM     mytable GROUP BY     WEEK(mydate)

How does it work? The DAYOFWEEK() function returns an integer ranging from 1 (Sunday) to 7 (Saturday). So if mydate happens to be Tuesday we get the following statements:

DATE_ADD(mydate, INTERVAL -2 DAY)

which essentially means “subtract 2 days from mydate (which is that week’s Sunday) and also:

DATE_ADD(mydate, INTERVAL 4 DAY)

which yields the date of that week’s Friday.

One would think that there would be a function to accomplish this automatically, but alas there is none. I think this is as simple as it gets. I hope this helps someone, because it took me quite a while to figure this out.


More than 3 requests, I'll translate this to Chinese.
超过3个请求,我就会把这篇文章翻译成中文。