Advertisement
Advertisement

More Related Content

Advertisement
Advertisement

Introducing CakeEntity

  1. CakeEntity The ActiveRecord for CakePHP @basuke https://github.com/kanshin/CakeEntity
  2. @basuke • Mac / iPhone / Newton • PHP : 10+ years
  3. http://www.kanshin.com/ 2001 AssistOn http://www.assiston.co.jp/ 2004 https://tabidachi.ana.co.jp/ 2006
  4. CakeEntity • CakePHP • Active Record Model • • GitHub • https://github.com/kanshin/CakeEntity • CakePHP 1.3, PHP 5.2 >
  5. CakeEntity • • CakePHP 1.3.11 1.3.12 up • Smarty Cake • CakeSmarty •
  6. Active Record ActiveRecord is "an object that wraps a row in a database table or view, encapsulates database access and adds domain logic on that data". Fowler, 2003 http://www.martinfowler.com/books.html
  7. Active Record
  8. CakeEntity find $post = $this->Post->entity(); // $post->content = "Hello world!"; $post->save(); //
  9. CakeEntity • array() [ ] • • • •
  10. Prepare
  11. first things first entity app/plugins/entity/ or plugins/entity/
  12. EntityModel extends App::import('Model', 'Entity.EntityModel'); class Post extends EntityModel { ... }
  13. Find
  14. 'entity' => true $this->Post->find('all', array( 'conditions' => ... 'order' => ... 'entity' => true, ));
  15. ������������ $result = [ {id: 1, title:"title1", author_id:"123", ... } {id: 2, title:"title2", content:"...", } ... ]; // paginate()
  16. // Post -> belongsTo -> Author $this->Post->find('all', array( 'conditions' => ... 'contain' => array('Author'), 'entity' => true, ));
  17. $result = [ {title:"title1", author: {id:3, name:"Basuke"}, ... } {title:"title2", author:null, ... } ... ]; //
  18. hasMany // Post -> hasMany -> Image $this->Post->find('all', array( 'conditions' => ... 'contain' => array('Image'), 'entity' => true, ));
  19. $result = [ {title:"title1", images: [ {path:"..."}, {path:"..."}] } {title:"title2", images: [{path:"..."}, ...] } ... ]; // w
  20. load
  21. $post = $this->Post->find('first', array( 'conditions' => array('id'=>$id), 'entity' => true, )); or $post = $this->Post->entityById($id);
  22. instantiation
  23. $post = $this->Post->entity(); $post->title = “Hello”; • $post = new PostEntity();
  24. Save
  25. $post->title = "Hello world"; $post->content = file_get_content(...); if ($post->save()) { $this->isCool(); }
  26. Entity Entity EntityModle Entity
  27. Entity + Entity entityClass()
  28. protected function entityClassForData($data) { switch ($data[‘type’]) { case ‘hyper’: return ‘HyperPostEntity’; default: return ‘PlainPostEntity’; } }
  29. isHidden(), publish()
  30. $post->property_name • $post[‘property_name’] • $post->some_method() • $post[‘some_method’] • • []
  31. {Smarty} • Smarty {$post.property_name} • () • •
  32. public function allows() { return array(); } • allows() • public
  33. public $comments; public function comments() { $Comment = $this->getModel()->Comment; return $Comment->find(array( ... )); } • $post[‘comments’]
  34. CakePHP 2.0 w Many to Many
  35. w README.md
  36. Thanks http://d.hatena.ne.jp/basuke/

Editor's Notes

  1. \n
  2. \n
  3. \n
  4. \n
  5. \n
  6. \n
  7. \n
  8. \n
  9. \n
  10. \n
  11. \n
  12. \n
  13. \n
  14. \n
  15. \n
  16. \n
  17. \n
  18. \n
  19. \n
  20. \n
  21. \n
  22. \n
  23. \n
  24. \n
  25. \n
  26. \n
  27. \n
  28. \n
  29. \n
  30. \n
  31. \n
  32. \n
  33. \n
  34. \n
  35. \n
  36. \n
Advertisement