Successfully reported this slideshow.

Introducing CakeEntity

3

Share

Upcoming SlideShare
Introducing CakeEntity
Introducing CakeEntity
Loading in …3
×
1 of 36
1 of 36

More Related Content

Related Books

Free with a 14 day trial from Scribd

See all

Introducing CakeEntity

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

Editor's Notes

  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • ×