Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Well my theory is that this should be done in the model itself. But sometimes when dealing with a complex model, I like to create a separate class that handles any special "presentation" of that model. It takes the model as an argument and encapsulates the presentation logic. </p> <p>So using your example, perhaps something like this:</p> <pre><code>class Model_Product { public static function getAllTypes() { return array(/* key-value pairs */); } //returns non human readable value public function getType() { return $this-&gt;_type; } } class Model_Product_Presenter { protected $_model; public function __construct(Model_Product $model) { $this-&gt;_model = $model; } //returns human readable value public function getType() { $types = $this-&gt;_model-&gt;getAllTypes(); if (!array_key_exists($this-&gt;_model-&gt;type, $types)) { return null; } return $types[$this-&gt;_model-&gt;type]; } public function getDateCreated($format = "Y-m-d") { return date($format,$this-&gt;_model-&gt;timestamp); } } </code></pre> <p>You can go further and create a base presenter class to define any common tasks, i.e. converting timestamps to dates, formatting numbers, etc.</p> <p><strong>Update:</strong> For anonymous access to a list of product types, I don't see any harm in making it the responsibility of the product model via a static method. Not all static methods are evil. In my opinion, the use of static methods for this purpose is fine, because it declares a global constant.</p> <p>In a more complex scenario, I would delegate this responsibility to a separate class like Model_ProductType. Here is an example of such a complex model in production:</p> <p><a href="https://github.com/magento/magento2/blob/master/app/code/core/Mage/Catalog/Model/Product/Type.php" rel="nofollow">https://github.com/magento/magento2/blob/master/app/code/core/Mage/Catalog/Model/Product/Type.php</a></p>
    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. 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