Unix dmesg timestamp

Handy script to convert unix’s dmesg timestamp to a human readable format – dmesg_realtime.sh:

1
2
3
4
5
#!/bin/bash
ut=`cut -d' ' -f1 < /proc/uptime`
ts=`date +%s`
realtime_date=`date -d"70-1-1 + $ts sec - $ut sec + $1 sec" +"%F %T"`
echo $realtime_date

Usage:

1
2
3
4
5
6
7
$ dmesg
...
...
[102927.525349] 954929 pages non-shared

$ ./dmesg_realtime.sh 102927.525349
2014-06-17 13:59:17

Force MySQL timezone on RDS in a Symfony1 project

If you need to force a specific MySQL timezone for your symfony1 project you should edit ProjectConfiguration.class.php:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
class ProjectConfiguration extends sfProjectConfiguration
{
    ...
    public function configureDoctrineConnection(Doctrine_Connection $connection)
    {
        $mysql_timezone = sfConfig::get('app_mysql_timezone', 'SYSTEM');
        try
        {
            $connection->exec('SET time_zone = "' . $mysql_timezone . '"');
        } catch (Exception $e)
        {
            /* I'm here because if an invalid timezone
             * is given I'm silently (and cowardly),
             * ignoring it and using MySQL's default
             * timezone instead */

        }
    }
    ...
}

PHP read POST json data

Imagine you have a webhook on your server and someone sends a JSON body POST to it.

The data is not available in the global $_REQUEST or $_POST variables as you’d expect.

To access the JSON data you’ll need to get the POST’s RAW DATA:

1
2
3
<?php
$postData = file_get_contents("php://input"); // <-- eg: {"key":1, "key2":2}
$arr_data = json_decode($postData, true); // <-- array('key1' => 1, 'key2' => 2)

More on this: http://us.php.net/manual/en/wrappers.php.php#wrappers.php.input

up and down arrow

Symfony 1.4 embed ordered Doctrine relations

Imagine a scenario where a “Product has N ordered Photos”:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
Product:
  columns
:
    name
:              { type: string(128), notnull: true }
 
Photo
:
  columns
:
    url
:         { type: string(255) }
    product_id
:  { type: integer }
    position
:    { type: integer }
  relations
:
    Product
:
      local
:           product_id
      foreign
:         id
      foreignAlias
:    Photos
      onDelete
:        CASCADE

In the ProductForm you can embed the related Photos with:

1
2
3
4
5
6
7
class ProductForm extends BaseProductForm
{
    public function configure()
    {
        $this->embedRelation('Photos')
    }
}

Problem: The related Photos will be displayed ordered by their id

Solution: Specify the relation between Product and Photos on the schema.yml file and provide a orderBy attribute:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
Product:
  columns
:
    name
:              { type: string(128), notnull: true }
  relations
:
    Photos
:
      type
:     many
      class
:    Photo
      local
:    id
      foreign
:  product_id
      orderBy
:  position ASC   # this is the magic part!
      onDelete
: CASCADE
 
Photo
:
  columns
:
    url
:         { type: string(255) }
    product_id
:  { type: integer }
    position
:    { type: integer }
  relations
:
    Product
:
      local
:           product_id
      foreign
:         id
      foreignAlias
:    Photos
      onDelete
:        CASCADE

Example adapted from: http://devblog.lexik.fr/symfony/tips-symfony-1-31-4-orderby-des-relations-1026

Apache setting and reading Environmental Variables

A common .htaccess issue: you need one for production and another for development.

Here’s a simple trick to set and read apache env vars.

1
2
3
4
5
6
7
8
9
10
11
12
13
<IfModule mod_rewrite.c>
  RewriteEngine On

  # do not force https on local environment
  RewriteCond %{SERVER_NAME} local.yoursite.net
  RewriteRule .? - [E=siteenv:local]

  RewriteCond %{HTTP:X-Forwarded-Proto} !https # not on https
  RewriteCond %{ENV:siteenv} !local # not on local environment
  RewriteRule !/status https://%{SERVER_NAME}%{REQUEST_URI} [L,R]

  ...
</IfModule>

On this example we do not force HTTP when accessing via “local.yoursite.net” (a development environment)

Using the Zend framework with Symfony 1.4

In order to integrate Zend with your Symfony 1.4 project, follow these steps:

Step 1. Add the SplClassLoader class to your project (e.g., in /lib/autoload).

Step 2. Require the SplClassLoader and sfCoreAutoload.class.php (located in Symfony’s lib/autoload directory) in your projectConfiguration.class.php:

1
2
3
4
5
<?php
class projectConfiguration extends sfApplicationConfiguration{
       require_once dirname(__FILE__).'/usr/share/symfony/1.4.20/lib/autoload/sfCoreAutoload.class.php';
       require_once dirname(__FILE__).'/../lib/autoload/SplClassLoader.php';
       //...

Step 3. Then, copy the Zend framework to your project (e.g., to /lib/vendor/Zend) and add the following in your projectConfiguration.class.php:

1
2
3
4
5
6
7
8
9
10
<?php
class projectConfiguration extends sfApplicationConfiguration {
      //...
      public function configure(){
            //...
            sfCoreAutoload::register();
            $classLoader = new SplClassLoader('Zend', dirname(__FILE__).'/../../../lib/vendor');
            $classLoader->register();
      }
      //...

And it’s done! Now you can use namespaces to add Zend’s features to your Symfony project:

1
2
3
4
5
6
7
<?php
use Zend\Validator;
class myActions extends sfActions
{
  public function executeValidate(sfWebRequest $request){
    $validator = new EmailAddress();
    //...

jQuery from ‘live’ to ‘on’

jQuery’s live event handler is deprecated (as of version 1.7) and no longer supported (as of version 1.9). The new event handler is called on. Here’s how to migrate your scripts.

Before:

1
2
3
$(".my_selector").live("click", function(e) {
    // ...
});

Now:

1
2
3
$(document).on("click", ".my_selector", function(e) {
    // ...
});