Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>My vote is for going down the route of Model extends datastore. That seems most natural because in effect your model <strong>is</strong> an extension of the underlying datastore. However, you may want to use a Service design pattern to decouple the creation of the model object with the consumption of the resulting object.</p> <pre><code>class Model { protected $_tablename == __CLASSNAME__; // or some derivative of the classname protected $_datastore; protected $_typeOfDataStore; function __construct( $type == 'mysql' ) { $this-&gt;_typeOfDataStore = $type; $this-&gt;bindToDataStore(); } function bindToDataStore( $ds ) { $this-&gt;_datastore = DataStoreFactory::buildDataStore( $this-&gt;_typeOfDataStore ); } } class DataStoreFactory { function buildDataStore( $type ) { switch ( $type ) { case 'flatfile' : return new FlatFileDataStore(); case 'mysql': return new MySQLDataStore(); } } } class DataStoreBase { function connect() { } function disconnect() { } function getData() { } // ... } class FlatFileDataStore extends DataStoreBase { } class MySQLDataStore extends DataStoreBase { function runQuery() { } } </code></pre> <p>That's the construction part. When you need to create/implement a new model, you can simply extend the model:</p> <pre><code>class Users extends Model { protected $_tablename = 'users'; protected $_typeOfDataStore = 'flatfile'; function getAllUsers() { return $this-&gt;runQuery( "SELECT * FROM " . $this-&gt;_tablename ); } } </code></pre> <p>And in your controller:</p> <pre><code>$usersModel = new Users(); $usersModel-&gt;getAllUsers(); </code></pre> <p>The objection that you pointed out about the datastore being unable to use any of the Model's methods is a good constraint on the system. The model is a consumer of the datastore and thus the datastore should not be able to directly call the model object's functions. The model should maintain the control and make use of the datastore service whenever it needs to. </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.
    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