Zend Framework Structure for Datamapper, Controller and Model -
i have started zend framework, have lots of questions structure. hope can explain properly, it's rather difficult.
ok, have done quickstart tutorial , decided use similar structure first project.
so have datamapper, model , database table file.
i have created form can enter information(item) , upload images it.
i have 2 datamapper (item , image) 2 models them. have item controller, 1 jumps itemmapper. in datamapper have save method. please see below:
public function save(application_model_item $item) { $data = array( 'item_title' => $item->getitemtitle(), 'item_description' => $item->getitemdescription(), ); $this->getdbtable()->insert($data); // add image information ($imagedata session data) $table = new application_model_dbtable_image(); foreach ($imagedata $filedata) { $image = new application_model_image($filedata); $data = array( 'image_newname' => $image->getnewimagename(), 'image_thumbname' => $image->getimagethumbname(), ); $table->insert($data); }
now question have.
- i have getter , setter in model. should in item database table in 1 model? means image should go image model since in different database table?
- is correct save information image in item mapper? shoudl not have save() method in imagemapper , save in there? if so, how jump item mapper image mapper? or first finish item, return id controller , call imagemapper controller?
i read "fat model thin controller". had time in had, noticed controller got pretty fat putting form together. have 5 dropdown fields depending dropdowns. when saw duplicating code decided add in separate function. f.e. have dropdown counties. wrote function in controller looks this:
public function getcounties ()
{
// list of counties not exist in cache, read db if(!$countylist = $this->getfromcache('counties')){ $geolocationmapper = new application_model_geolocationmapper(); $countydropdown = $geolocationmapper->createcountydropdown(); // save db result in cache $this->addtocache ('counties',$countydropdown); return $countydropdown; } else{ // return country list cache return $this->getfromcache('counties'); }
}
in add function use
// assign geo info form
$countylist = $this->getcounties(); $form->getelement('county')->setmultioptions($countylist);
the edit function than
$countylist = $this->getcounties(); $form->getelement('county')->setmultioptions($countylist)->setvalue($activecounty)
all functions getcounties () stay in controller or should moved geolocationmapper? , if so, how called up?
- should form created in function call createform() ? have lot of duplication (add , edit function) , stuff comes database or form not valid , comes cache setvalue. adds when using dependable dropdowns.
i know lots of questions, have feeling gets messy, learner happy when works, structure in proper way. hope makes sense.
maybe of have few tipps use. lot in advance.
there quite few questions here , of answers down largely personal preference. caveat out of way:
should in item database table in 1 model?
i yes, although in general, try , think perspective of models rather database structure. of 'item' data goes in item model - fact stored in 1 database table irrelevant, since mapper handles translation 1 other.
is correct save information image in item mapper?
it's not clear $imagedata comes in example, i'd item mapper should call image mapper if there image data save, e.g.:
public function save(application_model_item $item) { $data = array( 'item_title' => $item->getitemtitle(), 'item_description' => $item->getitemdescription(), ); $this->getdbtable()->insert($data); // save image data if present if (isset($item->image)) { $imagemapper = new yourapp_mapper_image(); $imagemapper->save($item->image); } return true; }
[should] functions getcounties () stay in controller or should moved geolocationmapper? , if so, how called up?
i don't see reason these functions in controller. depending on how comfortable zend_form
component, 1 approach might write custom yourapp_form_element_counties
class extends zend_form_element_select
. move logic getcounties function class, form element responsible populating options presents. e.g.:
class yourapp_form_element_counties extends zend_form_element_select { public function getmultioptions() { // load counties here , return array in format key -> value } }
another approach, if have lot of location-related form elements, might create geolocation service class, has function counties, cities etc. returns options.
should form created in function call createform()
it's not clear how form stuff doing in controller apart populating select options, it's hard answer one. here principles follow when using zend_form:
- each form has own class, extending zend_form (or zend_dojo_form), exists in application/forms , named myapp_form_*
- each form class sets elements in init() method (which called automatically zend_form's constructor purpose).
- if find need slight variations same form in different actions (e.g. in add , edit action), create abstract base class form (e.g.
myapp_form_post_base
) defines elements, , action-specific classes extend it:myapp_form_post_add
,myapp_form_post_edit
, on. these classes make changes need base form in own init() method.
my actions this:
public function editaction() { $form = new myapp_form_post_edit(); if ($this->_request->ispost()) { if ($form->isvalid($this->_request->getpost()) { // save data, set flash messge , redirect } } $this->view->form = $form; }
my other piece of advice try , follow approach seems logical you. might find difficult find definitive 'best practices' lot of stuff since there many different ways all.
Comments
Post a Comment