speedmax (owner)

Revisions

gist: 111008 Download_button fork
public
Public Clone URL: git://gist.github.com/111008.git
Embed All Files: show embed
1. dataobject example for cakephp.php #
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
<?
 
class PagesController extends AppController {
  function index() {
    $pages = DataObject::toCollection($this->paginate('Page'), 'PageObject');
 
    
    //$pages[0]->title;
    //$pages[0]->body;
    //$pages[0]->path();
 
    echo h2o("
{% for page in pages %}
{{ page.title | links_to page.path }}
{{ 'edit' | links_to page.edit_path }}
{{ 'images/delete.png'|image_tag|link_to page.delete_path }}
{% endfor %}
", array('safeClass'=>'DataObject'))->render(compact('pages'));
 
    die;
  }
}
 
 
class PageObject extends DataObject {
}
?>
2. lib/dataobject.php #
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
<?
 
class DataObject {
   protected $data;
   function __construct($data) {
      $this->data = $data;
   }
 
   function __set($attr, $value) {
      return $this->data[$attr] = $value;
   }
 
   function __get($attr, $value) {
      return $this->data[$attr];
   }
 
   function __isset($attr) {
     return isset($this->data[$attr]);
   }
 
   function path() {
      return $this->url_for('view');
   }
 
   function edit_path() {
      return $this->url_for('edit');
   }
 
   function delete_path() {
      return $this->url_for('delete');
   }
 
  private
   function url_for($action, $params = array()) {
     Router::url(array_merge(array(
        'controller' => Inflector::pluralize(get_class($this)),
        'action' => $action,
        'id' => $this->id
     ), $params));
   }
  
   static function toInstance($data, $class = 'DataObject') {
      return new $class($data)
   }
   static function toCollection($data, $class = 'DataObject') {
      foreach ($data as &$item) {
         $item = new $class($item);
      }
      return $data;
   }
}
 
?>