Note that there are some explanatory texts on larger screens.

plurals
  1. POConnection Sharing and Structuring of Model Objects
    primarykey
    data
    text
    <p>I'm writing my own OOP framework, partially as a learning exercise, but knowing this codebase isn't going to disappear - only evolve as I learn. Below is some psuedocode of what my setup looks like. In this setup, how would I share DB connections? Do a ctrl+f for "connection" to find the places in the code I'm specifically unsure of how to structure.</p> <p>A routing file would look like this: <pre><code>switch($urlParameters['action']){ case 'lading': //etc etc break; case 'userCP': //etc etc break; case 'dashboard': default: $page = new dashboard(); $page-&gt;route($urlParameters); break; } </code></pre> <p>A page would look like </p> <pre><code>&lt;?php abstract class page{ // Page is a mixture of controller and view. Most view logic is frontend, so I do all my controller-view logic here public var $models; public var $session; public var $user; function __construct(){ $this-&gt;models = new models(); $this-&gt;session = $this-&gt;models-&gt;session(new MongoID($_COOKIE['session'])); // Plus some code to prevent session hijacking $this-&gt;user = $this-&gt;models-&gt;users($this-&gt;session-&gt;userID); } function outputMongoData(){} // Wrapper function used by pages to output Mongo datatypes as regular json (Dates to RFC, IDs to string, rounding floats, etc) function template($file, $data){} // This uses output buffering and extract() to use php its self as the templating language. } class dashboard extends page{ public var $dashboardView; function __construct(){ parent::__construct(); $this-&gt;dashboardView = $this-&gt;models-&gt;dashboardView($this-&gt;user-&gt;dashboardViewID); } function route($urlParams){ $this-&gt;session-&gt;updatePageViewCountOrSomething(); echo $this-&gt;template('dashboard.php', [ 'importantData' =&gt; $this-&gt;dashboardView-&gt;someTypeOfDataFromDashboardView, 'viewCount' =&gt; $this-&gt;session-&gt;pageViewCount ]); } } </code></pre> <p>And the models look like</p> <pre><code>&lt;?php class models{ private var $cache = [ 'users' =&gt; [], 'dashboardViews' =&gt; [], 'sessions' =&gt; [], ]; // Cache to prevent collisions since we're doing save() vs individual updates, plus saves overhead of querying multiple times (potentially hundreds of times for certain models) function __construct(){ // Potential connection sharing stuff here } private function modelFactory($collection, $modelName, $identifier){ $stringID = (string) $identifier; if(!isset($this-&gt;cache[$collection][$stringID])){ $this-&gt;cache[$collection][$stringID] = new $modelName($identifier); } return $this-&gt;cache[$collection][$stringID]; } public function session(MongoID $sessionID){ return $this-&gt;modelFactory('sessions', 'session', $sessionID); } public function user(MongoID $userID){ return $this-&gt;modelFactory('users', 'user', $userID); } public function dashboardView(MongoID $dashboardViewID){ return $this-&gt;modelFactory('dashboardViews', 'dashboardView', $dashboardViewID); } } abstract class model{ public var $db; public var $collection; private var $data = []; function __construct($collectionName){ $this-&gt;db = // ??? Not sure how to connect so that I don't have a new connection for every single model.. $this-&gt;collection = $this-&gt;db-&gt;{$collectionName}; } function __destruct(){ $this-&gt;collection-&gt;save($this-&gt;data); } function __get($fieldName){ return $this-&gt;data[$fieldName]; // if isset, etc etc } } class session extends model{ // Explanation of schema here function __construct(MongoID $sessionID){ parent::__construct('sessions'); $this-&gt;data = $this-&gt;collection-&gt;findOne(['_id' =&gt; $sessionID]); // or if not found, create.. } function updatePageViewCountOrSomething(){ $this-&gt;data['pageViewCount'] += 1; $this-&gt;data['orSomething'] = ['something' =&gt; 'or another']; } } class dashboardView extends model{ // Explanation of schema here function __construct(MongoID $dashboardViewID){ parent::__construct('dashboardViews'); $this-&gt;data = $this-&gt;collection-&gt;findOne(['_id' =&gt; $sessionID]); // or if not found, create.. } function addColumns(){} function reorderColumns($newOrder){} } </code></pre>
    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.
 

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