Note that there are some explanatory texts on larger screens.

plurals
  1. POZend Framework Structure for Datamapper, Controller and Model
    primarykey
    data
    text
    <p>I have just started with Zend Framework, so I have lots of questions about the structure. I hope I can explain it properly, it's rather difficult.</p> <p>Ok, I have done the Quickstart Tutorial and decided to use a similar structure for my first project.</p> <p>So I have a Datamapper, a Model and the Database Table File.</p> <p>I have created a Form where I can enter a some Information(Item) and upload Images with it.</p> <p>I have 2 Datamapper (Item and Image) as well as 2 Models for them. I have an Item Controller, that one jumps into the ItemMapper. In the Datamapper I have a save Method. Please see below: </p> <pre><code> public function save(Application_Model_Item $item) { $data = array( 'item_title' =&gt; $item-&gt;getItemTitle(), 'item_description' =&gt; $item-&gt;getItemDescription(), ); $this-&gt;getDbTable()-&gt;insert($data); // Add Image Information ($imageData is Session Data) $table = new Application_Model_DbTable_Image(); foreach ($imageData as $fileData) { $image = new Application_Model_Image($fileData); $data = array( 'image_newname' =&gt; $image-&gt;getNewImageName(), 'image_thumbname' =&gt; $image-&gt;getImageThumbName(), ); $table-&gt;insert($data); } </code></pre> <p>Now the question I have.</p> <ol> <li>I have Getter and Setter in my Model. Should everything what is in the Item Database Table be in 1 Model? Means everything about the Image should go to the Image Model since it is in a different Database Table?</li> <li>Is it correct that I save the Information about the Image in the Item Mapper? Shoudl I not have a save() Method in the ImageMapper and save it in there? If so, how do I jump from the Item Mapper to the Image Mapper? Or would I first finish everything about the Item, return the ID to the Controller and than call the ImageMapper from the Controller?</li> <li><p>I read something about "Fat Model Thin Controller". I had this all the time in my had, but I noticed that my Controller got pretty fat with just putting the Form together. I have about 5 Dropdown Fields which are depending Dropdowns. When I saw that I duplicating Code I decided to add this in a separate Function. So f.e. I have a Dropdown for counties. I wrote a Function which is also in my Controller so it looks like this:</p> <pre><code>public function getCounties () </code></pre> <p>{ </p> <pre><code>// List of Counties does not exist in Cache, read from DB if(!$CountyList = $this-&gt;getFromCache('counties')){ $geolocationMapper = new Application_Model_GeolocationMapper(); $CountyDropdown = $geolocationMapper-&gt;createCountyDropdown(); // Save DB Result in Cache $this-&gt;addToCache ('counties',$CountyDropdown); return $CountyDropdown; } else{ // Return Country List from Cache return $this-&gt;getFromCache('counties'); } </code></pre> <p>} </p></li> </ol> <p>In my Add Function I use</p> <p>// Assign Geo Info to Form</p> <pre><code>$CountyList = $this-&gt;getCounties(); $form-&gt;getElement('county')-&gt;setMultiOptions($CountyList); </code></pre> <p>The Edit Function than</p> <pre><code>$CountyList = $this-&gt;getCounties(); $form-&gt;getElement('county')-&gt;setMultiOptions($CountyList)-&gt;setValue($activeCounty) </code></pre> <p>all the Functions like getCounties () stay in the Controller or should it be moved to the GeolocationMapper? And if so, how would that be called up?</p> <ol> <li>Should the Form be created in some Function so I would only call up something like createForm() ? I really have a lot of duplication (Add and Edit Function) and than Stuff comes from Database or Form was not Valid and it comes from Cache with a setValue. It just adds up when using dependable Dropdowns. </li> </ol> <p>I know this are lots of questions, but I have the feeling it gets very messy, as a learner you are happy when it works, but I would also like to structure it in a proper way. I hope it all makes sense.</p> <p>Maybe some of you have a few Tipps I could use. Thanks a lot for your help in advance.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload