Models Best Practices (ZF MVC)

  • 299 views
Uploaded on

 

  • 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
299
On Slideshare
0
From Embeds
0
Number of Embeds
0

Actions

Shares
Downloads
6
Comments
0
Likes
1

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

Transcript

  • 1. The M in MVCZend Framework
  • 2. AboutTwitter: @eddiejaoudeGitHub: eddiejaoude (http://eddiejaoude.github.com)Email: eddie@jaoudestudios.com
  • 3. MVC
  • 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. Model● Entity (business object)● DAO (data access object)● Mapper (mapping from external to internal)● Service (NOT web service, business logic)
  • 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. 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. 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. 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. DAO Interfaceinterface ModelDaosUserInterface{ /** * Find all * * @return stdClass */ public function findAll(); /** * Find by ID * * @param int $id * * @return stdClass */ public function findById($id);}
  • 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. 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. 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. Service//$_mapper = new ModelMappersUser;class ModelServicesUser{ /** * Find all * * @return array */ public function findAll() { // business logic // .. return $this->getMapper()->findAll() ; }}
  • 15. Verbose Usage$dao = new ModelDaosUserSoap;$mapper = new ModelMappersUser($dao);$service = new ModelServicesUser($mapper);$users =$service->findAll(); DIC Dependency Injection Container
  • 16. Service / MapperSingle Service Multiple Mappers Service Mapper DAO (User) (User) (Soap) findAll findEnabled findDisabled
  • 17. Service / MapperMultiple Services Single Mapper Service Mapper DAO (User) (User) (Soap) findEnabled findAll findDisabled
  • 18. Model Overview Model Datasource DB Interface Entity / Collection Service Mapper DAO API Entity FILE
  • 19. Next Model Datasource Interface DB Interface Entity / Collection Service Mapper DAO API Entity FILE