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 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
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.
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.
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();
22. Save is easy.
$post->title = "Hello world";
$post->content = file_get_content(...);
if ($post->save()) {
$this->isCool();
}
23. Entity Class
Entity Class is a default class when EntityModel
will 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 Subclass
If class with model’s name + “Entity” is exists, that
class is used instead.
class PostEntity extends Entity {
...
}
Or override entityClass().
25. Custom subclass
depending by data
protected function entityClassForData($data) {
switch ($data[‘type’]) {
case ‘hyper’:
return ‘HyperPostEntity’;
default:
return ‘PlainPostEntity’;
}
}
26. Domain logic can be
placed 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. Future
CakePHP 2.0, of course.
Reverse conversion to array structure.
More tests.
saveAll() support.
dirty property detection.