Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You are going in the wrong way about solving this.</p> <p>Instead of each time manually making a new "Model" and then configuring it, you should create a structure that does it for you ( extremely simplified version ):</p> <pre><code>class ModelFactory { protected $connection = null; // --- snip -- public function __construct( $connection ) { $this-&gt;connection = $connection; } // --- snip -- public function buildMapper( $name ) { $instance = new {$name}( $this-&gt;connection ); return $instance; } // --- snip -- } </code></pre> <p>This class you would be using in index.php or bootstrap.php , or any other file you use as entry point for your application:</p> <pre><code>// at the bootstrap level $modelFactory = new ModelFactory( new PDO(...) ); // i am assuming that you could get $controllerName // and $action from routing mechanism $controller = new {$controllerName}( $modelFactory ); $controller-&gt;{$action}(); </code></pre> <p>The main problem you have is actually cause by misunderstanding what Model is. In a proper MVC the Model is a layer, and not a specific class. Model layer is composed from multitude of class/instances with two major responsibilities:</p> <ul> <li>domain business logic</li> <li>data access and storage</li> </ul> <p>The instances in first group are usually called <a href="http://c2.com/cgi/wiki?DomainObject">Domain Objects</a> or <a href="https://en.wikipedia.org/wiki/Business_object">Business Objects</a> (kinda like situation with <a href="http://xkcd.com/747/">geeks and nerds</a>). They deal with validations, computation, different conditions, but have no clue how and where information is stored. It does not change how you make an <em>Invoice</em> , whether data comes from SQL, remote REST API or a screenshot of MS Word document.</p> <p>Other group consists mostly of <a href="http://martinfowler.com/eaaCatalog/dataMapper.html">Data Mappers</a>. They store and retrieve information from Domain Objects. This is usually where your SQL would be. But mappers do not always map directly to DB schema. In a classic many-to-many structure you might have either 1 or 2 or 3 mappers servicing the storage. Mappers usually one per each Domain Object .. but even that is not mandatory.</p> <p>In a controller it would all look something like this.</p> <pre><code>public function actionFooBar() { $mapper = $this-&gt;modelFactory-&gt;buildMapper( 'Book' ); $book = $this-&gt;modelFactory-&gt;buildObject( 'Book' ); $patron = $this-&gt;modelFactory-&gt;buildObject( 'Patron' ); $book-&gt;setTitle('Neuromancer'); $mapper-&gt;fetch( $book ); $patron-&gt;setId( $_GET['uid']); if ( $book-&gt;isAvailable() ) { $book-&gt;lendTo( $user ); } $mapper-&gt;store( $book ); } </code></pre> <p>Maybe this will give you some indications for the direction in which to take it.</p> <p>Some additional video materials:</p> <ul> <li><a href="https://vimeo.com/21173483">Advanced OO Patterns</a> (<a href="http://qafoo.com/talks/11_11_osidays_advanced_oo_patters.pdf">slides</a>)</li> <li><a href="http://www.youtube.com/watch?v=-FRm3VPhseI">Global State and Singletons</a></li> <li><a href="http://www.youtube.com/watch?v=RlfLCWKxHJ0">Don't Look For Things!</a></li> </ul>
 

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