Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Introducing CakeEntity

3,901 views

Published on

CakePHPの検索でオブジェクトが返るようになるCakePHP用のプラグインの紹介

Published in: Technology, Business
  • Be the first to comment

Introducing CakeEntity

  1. 1. CakeEntityThe ActiveRecord for CakePHP @basuke https://github.com/kanshin/CakeEntity
  2. 2. • @basuke• Mac / iPhone / Newton• PHP : 10+ years
  3. 3. http://www.kanshin.com/ 2001AssistOn 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 RecordActiveRecord is "an object that wraps a row in adatabase table or view, encapsulates databaseaccess 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 entityapp/plugins/entity/ orplugins/entity/
  12. 12. EntityModel extendsApp::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. EntityEntityEntityModle 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. wREADME.md
  36. 36. Thankshttp://d.hatena.ne.jp/basuke/

×