Monday, 3 February 2014

DateHelper (date_format) in symfony

DateHelper (date_format) in symfony


DateHelper has a particular function format_date which is quite good for formatting dates, esp that come straight from the propel object. However looking at the Helper file DateHelper.php you cant really tell what sort of formats does it accept and no it doesnt take the standard PHP date formats.
Digging a little further I found the details in another file sfDateFormat.class.php and as the class states.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
   switch ($pattern)
    {
      case 'd':
        return $this->formatInfo->ShortDatePattern;
        break;
      case 'D':
        return $this->formatInfo->LongDatePattern;
        break;
      case 'p':
        return $this->formatInfo->MediumDatePattern;
        break;
      case 'P':
        return $this->formatInfo->FullDatePattern;
        break;        
      case 't':
        return $this->formatInfo->ShortTimePattern;
        break;
      case 'T':
        return $this->formatInfo->LongTimePattern;
        break;
      case 'q':
        return $this->formatInfo->MediumTimePattern;
        break;
      case 'Q':
        return $this->formatInfo->FullTimePattern;
        break;
      case 'f':
        return $this->formatInfo->formatDateTime($this->formatInfo->LongDatePattern, $this->formatInfo->ShortTimePattern);
        break;
      case 'F':
        return $this->formatInfo->formatDateTime($this->formatInfo->LongDatePattern, $this->formatInfo->LongTimePattern);
        break;
      case 'g':
        return $this->formatInfo->formatDateTime($this->formatInfo->ShortDatePattern, $this->formatInfo->ShortTimePattern);
        break;
      case 'G':
        return $this->formatInfo->formatDateTime($this->formatInfo->ShortDatePattern, $this->formatInfo->LongTimePattern);
        break;
      case 'i':
        return 'yyyy-MM-dd';
        break;
      case 'I':
        return 'yyyy-MM-dd HH:mm:ss';
        break;
      case 'M':
      case 'm':
        return 'MMMM dd';
        break;
      case 'R':
      case 'r':
        return 'EEE, dd MMM yyyy HH:mm:ss';
        break;
      case 's':
        return 'yyyy-MM-ddTHH:mm:ss';
        break;
      case 'u':
        return 'yyyy-MM-dd HH:mm:ss z';
        break;
      case 'U':
        return 'EEEE dd MMMM yyyy HH:mm:ss';
        break;
      case 'Y':
      case 'y':
        return 'yyyy MMMM';
        break;
      default :
        return $pattern;
    }

Symfony Generator Configuration


Remove Filter , New , Edit ,Delete batch action Using generator.yml

    config:
      actions: ~
      fields:
        ip:    { label: IP Address ,is_link: false}
        status: {type: Boolean}
      list:  
        max_per_page: 4
        sort: [status, desc]
        display: [name,ts,country,city,browser,status]
        fields:
          ts: { date_format:'dd-MMM-yyyy hh:mm:ss a' }
        batch_actions: {}
        object_actions: {}
        actions: { }
      filter:
        class: false
      form:   ~
      edit:   ~
      new:    ~

Block some field for admin only
fields:
 ip:      { credentials: [admin] }

Symfony Build All Classes

Symfony 1.4 generate classes

symfony doctrine:build --all-classes


symfony doctrine:generate-admin admin Winner --module=winners

Symfony customise widget from action class


Symfony 1.4 customize widget from action class

$formBasic->setWidget('email', new sfWidgetFormInputHidden()); 

$formBasic->setDefault('email',$userData['email']);

PDO results to Array

PDO Results to Array

PDO result sets return an array of arrays. This is great for most applications except if you need a single dimension array result set from a query that selects only two fields. I was recently populating some dropdown menu's with results from a database, so the query looked like this
SELECT category_id, category_name from phpro_categories
I again needed to populate other drop a products dropdown and several others and found myself writing out code over and over to do the same task, make the array of arrays from PDO into a single dimensional array using the id as key and the name of the second field as the value. So when I fetched my I used this.
$result = $stmt->fetchAll(PDO::FETCH_ASSOC);
This snippet below will take the result set from PDO and create an array like this
Array
(
    [1] => romance
    [2] => Thriller
    [3] => Adventure
    [4] => Sport
    [5] => autobiography
    [6] => non fiction
)

<?php
/**
 *
 * @convert pdo multi-d array to single d
 *
 * @param array $result
 *
 * @return $array
 *
 */
 
function pdo2array($result){
 
$new=array();
 foreach ( 
$result as $val )
 {
    
$keys array_keys($val);
    
$new[$val[$keys[0]]] = $val[$keys[1]];
 }
 return 
$new;
 }
?>