Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>One possibility is to use the magic __set and __get methods in PHP. I use them like so within my abstract Model class:</p> <pre><code>abstract class Model_Abstract { protected $_data; // Private Data Members assigned to protected $_data public function __construct($data = null) { // Makes it so that I can pass in an associative array as well as // an StdObject. if(!is_object($data)) { $data = (object) $data; } $this-&gt;_data = $data; } public function __get($key) { if (method_exists($this, '_get' . ucfirst($key))) { $method = '_get' . ucfirst($key); return $this-&gt;$method(); } else { return $this-&gt;_data-&gt;$key; } } public function __set($key, $val) { if ( method_exists( $this, '_set' . ucfirst($key) ) ) { $method = '_set' . ucfirst($key); return $this-&gt;$method($val); } else { $this-&gt;_data-&gt;$key = $val; return $this-&gt;_data-&gt;$key; } } } class Model_User extends Model_Abstract { //Example overriding method for the property firstName in the $_data collection. protected function _getFirstName() { // Do some special processing and then output the first name. } } </code></pre> <p>This makes it so that you can specify getters and setters for properties as necessary but makes it so that you don't have to define boilerplate functions for every property, just the ones where you want to do some sort of processing on it before returning the value. For example I use the functionality in a number of places to change ISO compliant dates (as stored in MySQL) into a more compact and readable format for users.</p> <p>As far as what to place in your controller, I would recommend looking at <a href="https://stackoverflow.com/questions/432435/zendframework-where-to-place-get-and-post-http-request-handling">this post</a> for some specific feedback on what handling to place within your controller. </p> <p>Some feel that they would rather have a helper that automatically loads models into the view and skirts the controller altogether. Personally I would say that within the context of Zend Framework and PHP it makes plenty of sense to pass models into the view from the controller because the state of the models in the view frequently depends on what came from the request (which should definitely be handled in the controller).</p> <p><b>Update:</b> As per criticisms in the comments, one thing that I would point out is that your database access layer and domain (or model) layer are really two different things, though with the Active Record they are blended together. I asked <a href="https://stackoverflow.com/questions/373054/how-to-properly-create-domain-using-zend-framework">this question</a> a while back and received some useful feedback on this matter. Whatever you decide to do with the model, you'll want to provide a consistent API for all domain objects regardless of where the data for the model comes from. </p> <p>I suppose that one benefit offered by Saem's answer is that it offers the ability to directly map properties / function return values from one or more domain objects to the view object. Theoretically the usage within the view then looks like this:</p> <pre><code>// Mapped from Model_User::_data-&gt;last_name and Model_User::_data-&gt;first_name $this-&gt;name </code></pre>
    singulars
    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.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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