php - How to extend the ZF2 skeleton application - entities with foreign keys -
my zf2 skeleton application works fine per zend user guide. i'm running in circles trying extend application album's artist isn't string anymore foreign key of artist table in db.
i've created necessary models, controllers, views create, edit , view artists, works fine. how combine 2 list of albums shows artist's name?
do use hydrator strategy as suggested here? if so, how implement it? zend\stdlib\hydrator\strategy manual doesn't tell me register hydrator strategy. yes there short implementation code:
$foo = new foo(); $foo->setfoo("bar"); $foo->setbar("foo"); $hydrator = new classmethods(); $hydrator->addstrategy("foo", new rot13strategy());
but go? in albumcontroller? , why keeps saying hydrators "mostly used forms"?
or isn't hydrator i'm looking after all? thing keep bumping in quest solution dependency injection. again found kind of tutorial, in using classes a, b , c , not telling me code fragments go isn't grasping concept.
i think want pretty common, , i'm staggered can't find real world tutorials. after hours of research i'm sure i'm close break through - can't see anymore , need push. :-)
hydrators used when ever need data in or out of object/entity, can when using form, or when saving or loading entity storage.
i wouldn't use hydrator populate objects related objects (e.g. adding antist object album). leave upto services / mappers.
here's example how i'd it:
album.php
<?php class album { // .. properties / getters/setters etc .. public function setartist(artistinterface $artist) { $this->artist = $artist; } public function getartist() { return $this->artist; } }
albumhydrator.php
this can vary, use getters/setters or properties, take @ base hydrators decide route want take..
your hydrator take assembled objects, , give data need either show form, or save persistence etc.
<?php class albumhydrator // extends zend\stdlib\hydrator\classmethods { public function extract($object) { return array( 'artist_id' => $object->getartist()->getid(), 'albumn_name' => $object->getalbumname() ) } // .. etc }
albumservice.php
<?php use zend\servicemanager\servicemanagerawareinterface; use zend\servicemanager\servicemanager; class albumservice implements servicemanagerawareinterface { /** * album, we'll load related objects here * * @param int */ public function find($id) { $album = $this->_getalbummapper()->find($id); $artist = $this->_getartistmapper()->findbyalbum($id); $album->setartist($artist); return $album; } /** * mapper * * @return application\mappers\albummapper */ protected function _getalbummapper() { return $this->getservicemanager() ->get('albummapper') ; } // .. }
you setup service in service manager config this:
'albumservice' => function($sm) { $mapper = $sm->get('albummapper'); $service = new application\service\albumservice(); $service->setalbummapper($mapper); return $service; },
you use service create object graph need. have separate mappers each of entities, , services can use mappers generate objects require.
you have hydrator each of entities, , mappers use populate objects you. depend on implementation, may using tablegateway or instead of mappers, still work ok.
an example of accessing objects when inside controller
somecontroller.php
public function albumaction() { $id = (int) $this->params()->fromroute('id', false); if( ! $id) { // errors etc .. return $this->redirect()->toroute('default_route', array( 'controller' => 'index', 'action' => 'index' )); } $request = $this->getrequest(); $album = $this->getservicelocator()->get('albumservice')->find($id); $artistname = $album->getartist()->getname(); return new viewmdoel( 'album' => $ablum, 'artistname' => $artistname ); }
populating object graphs when using collections little more tricky, tend use lazy loading , virtual proxies kind of setup, defer loading objects need until require them, check these out:
http://phpmaster.com/intro-to-virtual-proxies-1/ http://phpmaster.com/intro-to-virtual-proxies-2/
Comments
Post a Comment