The M in MVCZend Framework
AboutTwitter: @eddiejaoudeGitHub: eddiejaoude (http://eddiejaoude.github.com)Email: eddie@jaoudestudios.com
MVC
MVC●   Model - This is the part of your application that defines its basic    functionality behind a set of abstractions. ...
Model● Entity   (business object)● DAO   (data access object)● Mapper        (mapping from external to internal)● Service ...
InterfaceObject interfaces allow you to create code which specifies whichmethods a class must implement, without having to...
Interface// Declare the interface iTemplateinterface iTemplate{    public function setVariable($name, $var);    public fun...
Interface// Declare the interface iTemplateinterface iTemplate{    /**      * Set variable      *      * @param string $na...
Entityclass ModelEntitiesUser{     /**      * @var string      */    private $_name = ;    /**      * Set variable      * ...
DAO Interfaceinterface ModelDaosUserInterface{    /**      * Find all      *      * @return stdClass      */    public fun...
DAO Database//$_datasource = new ZendDb;class ModelDaosUserDb implements ModelDaosUserInterface{    /**      * Find all   ...
DAO Soap Client//$_datasource = new ZendSoapClient;class ModelDaosUserSoap implements ModelDaosUserInterface{    /**      ...
Mapper//$_dao = new ModelDaosUserSoap;//$_dao = new ModelDaosUserDb;class ModelMappersUserDb{    /**      * Find all      ...
Service//$_mapper = new ModelMappersUser;class ModelServicesUser{    /**      * Find all      *      * @return array      ...
Verbose Usage$dao = new ModelDaosUserSoap;$mapper = new ModelMappersUser($dao);$service = new ModelServicesUser($mapper);$...
Service / MapperSingle Service Multiple Mappers      Service             Mapper                   DAO      (User)         ...
Service / MapperMultiple Services Single Mapper      Service                  Mapper              DAO      (User)         ...
Model Overview    Model                                              Datasource                                           ...
Next       Model                                               Datasource                                   Interface     ...
Upcoming SlideShare
Loading in...5
×

Models Best Practices (ZF MVC)

384

Published on

0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
384
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
9
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

Models Best Practices (ZF MVC)

  1. 1. The M in MVCZend Framework
  2. 2. AboutTwitter: @eddiejaoudeGitHub: eddiejaoude (http://eddiejaoude.github.com)Email: eddie@jaoudestudios.com
  3. 3. MVC
  4. 4. MVC● Model - This is the part of your application that defines its basic functionality behind a set of abstractions. Data access routines and some business logic can be defined in the model.● View - Views define exactly what is presented to the user. Usually controllers pass data to each view to render in some format. Views will often collect data from the user, as well. This is where youre likely to find HTML markup in your MVC applications.● Controller - Controllers bind the whole pattern together. They manipulate models, decide which view to display based on the users request and other factors, pass along the data that each view will need, or hand off control to another controller entirely. Most MVC experts recommend » keeping controllers as skinny as possible.
  5. 5. Model● Entity (business object)● DAO (data access object)● Mapper (mapping from external to internal)● Service (NOT web service, business logic)
  6. 6. InterfaceObject interfaces allow you to create code which specifies whichmethods a class must implement, without having to define how thesemethods are handled.Interfaces are defined using the interface keyword, in the same wayas a standard class, but without any of the methods having theircontents defined.All methods declared in an interface must be public, this is the natureof an interface.
  7. 7. Interface// Declare the interface iTemplateinterface iTemplate{ public function setVariable($name, $var); public function getHtml($template);}// Implement the interfaceclass Template implements iTemplate{ private $_vars = array(); public function setVariable($name, $var) { $this->_vars[$name] = $var; return $this; } public function getHtml($template) { foreach($this->_vars as $name => $value) { $template = str_replace({ . $name . }, $value, $template); } return $template; }}
  8. 8. Interface// Declare the interface iTemplateinterface iTemplate{ /** * Set variable * * @param string $name * @param int $var * * @return iTemplate */ public function setVariable($name, $var); /** * Get html * * @param string $template * * @return string $template */ public function getHtml($template);}
  9. 9. Entityclass ModelEntitiesUser{ /** * @var string */ private $_name = ; /** * Set variable * * @param string $name * @return ModelEntitiesUser */ public function setName($name) { $this->_name = (string) $name; return $this; } /** * Get name * * @return string */ public function getName() { return $this->_name; }}
  10. 10. DAO Interfaceinterface ModelDaosUserInterface{ /** * Find all * * @return stdClass */ public function findAll(); /** * Find by ID * * @param int $id * * @return stdClass */ public function findById($id);}
  11. 11. DAO Database//$_datasource = new ZendDb;class ModelDaosUserDb implements ModelDaosUserInterface{ /** * Find all * * @return stdClass */ public function findAll() { return $this->getDatasource()->findAll(); } /** * Find by ID * * @param int $id * * @return stdClass */ public function findById($id) { return $this->getDatasource()->findById($id); }}
  12. 12. DAO Soap Client//$_datasource = new ZendSoapClient;class ModelDaosUserSoap implements ModelDaosUserInterface{ /** * Find all * * @return stdClass */ public function findAll() { $data = $this>getDatasource()->findAll(); return $data; } /** * Find by ID * * @param int $id * * @return stdClass */ public function findById($id) { $data = $this->getDatasource()->findById(array(id => 1)); return $data; }}
  13. 13. Mapper//$_dao = new ModelDaosUserSoap;//$_dao = new ModelDaosUserDb;class ModelMappersUserDb{ /** * Find all * * @return array */ public function findAll() { $users = array(); foreach($this->getDao()->findAll() as $user) { $userEntity = new ModelEntitiesUser; $userEntity->setName($user->name); $users[] = $userEntity; } return $users; }}
  14. 14. Service//$_mapper = new ModelMappersUser;class ModelServicesUser{ /** * Find all * * @return array */ public function findAll() { // business logic // .. return $this->getMapper()->findAll() ; }}
  15. 15. Verbose Usage$dao = new ModelDaosUserSoap;$mapper = new ModelMappersUser($dao);$service = new ModelServicesUser($mapper);$users =$service->findAll(); DIC Dependency Injection Container
  16. 16. Service / MapperSingle Service Multiple Mappers Service Mapper DAO (User) (User) (Soap) findAll findEnabled findDisabled
  17. 17. Service / MapperMultiple Services Single Mapper Service Mapper DAO (User) (User) (Soap) findEnabled findAll findDisabled
  18. 18. Model Overview Model Datasource DB Interface Entity / Collection Service Mapper DAO API Entity FILE
  19. 19. Next Model Datasource Interface DB Interface Entity / Collection Service Mapper DAO API Entity FILE
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×