• Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Be the first to comment
No Downloads

Views

Total Views
2,212
On Slideshare
0
From Embeds
0
Number of Embeds
1

Actions

Shares
Downloads
15
Comments
0
Likes
2

Embeds 0

No embeds

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
    No notes for slide
  • \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

Transcript

  • 1. CakeEntityThe ActiveRecord for CakePHP Basuke Suzuki @basuke https://github.com/kanshin/CakeEntity
  • 2. me• Basuke Suzuki @basuke• Mac / iPhone / Newton• PHP : 10+ years
  • 3. What is CakeEntity?• Plugin for CakePHP• Support ActiveRecord in Model• 100% compatible with the standard Model.• Open source. Available in GitHub• https://github.com/kanshin/CakeEntity• CakePHP 1.3, PHP 5.2 >
  • 4. 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
  • 5. Cons. find() returns array of object. You can manipulate object immediately, directly.$post = $this->Post->entity(); // creation$post->content = "Hello world!";$post->save();
  • 6. Cons. cont.• Less array() and [ brankets ].• Makes code structure simple. • Safe access to domain logic from View. • More code moves from Helper to Model. • More code moves from Controller to Model.• Testable.
  • 7. Prepare
  • 8. first things firstPlace in the plugins directory.Name should be “Entity”.app/plugins/entity/ orplugins/entity/
  • 9. Extends EntityModelApp::import(Model, Entity.EntityModel);class Post extends EntityModel { ...}
  • 10. Find
  • 11. entity => true$this->Post->find(all, array( conditions => ... order => ... entity => true,));
  • 12. Result is array of object $result = [ {id: 1, title:"title1", author_id:"123", ... } {id: 2, title:"title2", content:"...", } ...];// works fine with paginate()
  • 13. Works with “belongsTo”// Post -> belongsTo -> Author$this->Post->find(all, array( conditions => ... contain => array(Author), entity => true,));
  • 14. Property is also Entity $result = [ {title:"title1", author: {id:3, name:"Basuke"}, ... } {title:"title2", author:null, ... } ... ]; // property name is converted to lower case
  • 15. Of course, works with “hasMany”// Post -> hasMany -> Image$this->Post->find(all, array( conditions => ... contain => array(Image), entity => true,));
  • 16. Property holds array of Entity$result = [ {title:"title1", images: [ {path:"..."}, {path:"..."}] } {title:"title2", images: [{path:"..."}, ...] } ...];// property name is pluralized.
  • 17. load
  • 18. Fetch one entity$post = $this->Post->find(first, array( conditions => array(id=>$id), entity => true,)); or$post = $this->Post->entityById($id);
  • 19. instantiation.
  • 20. Instantiation is easy $post = $this->Post->entity(); $post->title = “Hello”;• Don’t use “new”. PostEntity class has no information of model. $post = new PostEntity();
  • 21. Save
  • 22. Save is easy.$post->title = "Hello world";$post->content = file_get_content(...);if ($post->save()) { $this->isCool();}
  • 23. Entity ClassEntity Class is a default class when EntityModelwill instantiate object.Customizable with sub class of Entity.Many features to works with View layer.The result of method call can be cached.Property access can be restricted.
  • 24. Entity SubclassIf class with model’s name + “Entity” is exists, thatclass is used instead.class PostEntity extends Entity { ...}Or override entityClass().
  • 25. Custom subclass depending by dataprotected function entityClassForData($data) { switch ($data[‘type’]) { case ‘hyper’: return ‘HyperPostEntity’; default: return ‘PlainPostEntity’; }}
  • 26. Domain logic can beplaced in Entity subclass Object is the place of domain logic. Method for collection should be placed in Model. Method for record should be placed in Entity. ex) isHidden(), publish() is the candidates.
  • 27. Easy access for property and method• For $post->property_name • $post[‘property_name’] is OK• For $post->some_method() • $post[‘some_method’] is also OK• Array access is cacheable.• Wait! I want to increase [ ] any more! Why?
  • 28. {Smarty} uses “.”• Smarty uses dot to access array. • i.e {$post.property_name}• Also reduce ( ) for method access.• Easier code reading.
  • 29. Method access control public function allows() { return array(); }• Override allows(). Return name of method to allow access.• Or define public method.
  • 30. Result is cached automatically public $comments; public function comments() { $Comment = $this->getModel()->Comment; return $Comment->find(array( ... )); }• $post[‘comments’] is cached.
  • 31. FutureCakePHP 2.0, of course.Reverse conversion to array structure.More tests.saveAll() support.dirty property detection.
  • 32. Document isnot ready. Sorry.Sample is available soon.
  • 33. Thankshttp://d.hatena.ne.jp/basuke/