Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>What you are trying to do is mix two design patterns: <a href="http://www.martinfowler.com/eaaCatalog/activeRecord.html">Active Record</a> and <a href="http://www.martinfowler.com/eaaCatalog/dataMapper.html">Data Mapper</a>.</p> <p>If you take a look at the Data Mapper pattern, you have the Mapper that accesses both the Model and the database. The Model is passive - usually does not call external resources (it's a POPO - Plain Old PHP Object).</p> <p>A solution for your issue is to inject the related information into the Model, thus keeping the Model only as a data structure.</p> <p>Here is a working scenario for an MVC application:</p> <p><strong>Controller</strong> - used for input validation &amp; retrieving data from services</p> <pre><code>&lt;?php ... public function viewAction() { $id = (int) $this-&gt;params()-&gt;fromQuery('id'); $service = $this-&gt;getServiceLocator()-&gt;get('your-user-service-name'); $user = $service-&gt;getUser($id); ... } </code></pre> <p><strong>Service</strong> - used for executing the business logic; calls multiple Data Mappers</p> <pre><code>&lt;?php ... public function getUser($id) { // get user $mapper = $this-&gt;getServiceLocator()-&gt;get('your-user-mapper'); $user = $mapper-&gt;getUserById($id); // get articles $article_mapper = $this-&gt;getServiceLocator()-&gt;get('your-article-mapper'); $user-&gt;articles = $article_mapper-&gt;getArticlesByUser($id); return $user; } </code></pre> <p><strong>Data Mapper</strong> - used to manipulate one type of Domain entity - it should be composed with a tableGateway if you are accessing the database</p> <pre><code>&lt;?php ... public function getUserById($id) { $select = $this-&gt;tableGateway-&gt;getSql()-&gt;select(); $select = $select-&gt;where(array('id' =&gt; $value)); $row = $this-&gt;tableGateway-&gt;selectWith($select)-&gt;current(); return $row; } </code></pre> <p><strong>Domain Model</strong> - used for data representation</p> <pre><code>&lt;?php ... class User { public $name; // user name ... public $articles; // holds the user articles } </code></pre> <p>Advantages</p> <ol> <li>Passive Models are easy to read - understand the data structure and it's relations.</li> <li>Passive Models are easy to test - you don't need external dependencies.</li> <li>You separate the persistence layer from the Domain layer.</li> </ol> <p>Hope this helps!</p>
 

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