I take a  different  approach :

// In the  MainHandler

Define('StorageClassName',"_MySQL");

Class UserController {
        function __construct($objDataStore=false){
                if(!$objDataStore)
                        $this->DataStore = new 
instanceof($this).STORAGECLASSNAME ;
        }
        Function update(){
                $data = $this->DataStore->Load("foo");
                $data->Set("foo","bar");
                $data->save;
        }


This assumes your using DBO  so the loaded foo object inherited the 
settings/methods from the DB Datastore.
Thus letting you  passing a different datastore object  or  it builds one based 
on the model name and the storage type you set as a constant.

However it still links the two together so you can use  $this calls to 
reference the DataStore of the Model layer.

David
> -----Original Message-----
> From: Mert Oztekin [mailto:mozte...@anadolusigorta.com.tr]
> Sent: Tuesday, October 27, 2009 6:14 AM
> To: 'Eric Bauman'; php-general@lists.php.net
> Subject: RE: [PHP] What is the best practice for adding persistence to an MVC
> model?
> 
> Hi Eric,
> 
> IMO, controllers shouldnt be responsible for interacting models and
> datastoreres. Controllers might only change the datastore class of a model.
> 
> You may use your models in lots of controller functions. Defining datastore in
> all controllers seems not a good practice. (too much unneccessary codes
> written)
> 
> A simple example
> 
> //controller
> Public function doItController()
> {
>         $user = new user();
>         // $user->setDataStorer(new anotherDataStorer());   // this is 
> optional if
> you want to change models datastorer
>         $user->loadUserFromId(1);
> }
> 
> // model
> Class user
> {
>         Protected $_dataStorer = null;
> 
>         Public function loadUserFromId($id = 0)
>         {
>                 // codes codes codes
>                 $result =       $this->getDataStorer()->query("select .....");
>                 // codes codes codes
>         }
> 
>         // codes codes codes
>         Public function getDataStorer()
>         {
>                 if(null == $this->_dataStorer)
>                         $this->_dataStorer = new myVeryBestDataStorer();
>                 return $this->_dataStorer;
>         }
> 
>         Public function setDataStorer($newStorer)
>         {
>                 $this->_dataStorer = $newStorer;
>         }
> }
> 
> 
> Hope it will be usefull and understandable
> 
> -----Original Message-----
> From: Eric Bauman [mailto:baum...@livejournal.dk]
> Sent: Tuesday, October 27, 2009 8:27 AM
> To: php-general@lists.php.net
> Subject: [PHP] What is the best practice for adding persistence to an MVC
> model?
> 
> I'm in the process of implementing an ultra-light MVC framework in PHP.
> It seems to be a common opinion that the loading of data from a database,
> file etc. should be independent of the Model, and I agree.
> What I'm unsure of is the best way to link this "data layer" into MVC.
> 
> I've considered a few options:
> 
> *Datastore interacts with Model*
> 
> //controller
> public function update()
> {
> 
>   $model = $this->loadModel('foo');
>   $data = $this->loadDataStore('foo', $model);
> 
>   $data->loadBar(9); //loads data and populates Model
>   $model->setBar('bar');
>   $data->save(); //reads data from Model and saves
> 
> }
> 
> *Controller mediates between Model and Datastore*
> 
> Seems a bit verbose and requires the model to know that a datastore exists.
> 
> //controller
> public function update()
> {
> 
>   $model = $this->loadModel('foo');
>   $data = $this->loadDataStore('foo');
> 
>   $model->setDataStore($data);
> 
>   $model->getDataStore->loadBar(9); //loads data and populates Model
>   $model->setBar('bar');
>   $model->getDataStore->save(); //reads data from Model and saves
> 
> }
> 
> *Datastore extends Model*
> 
> What happens if we want to save a Model extending a database datastore to
> a flatfile datastore?
> 
> //controller
> public function update()
> {
> 
>   $model = $this->loadHybrid('foo'); //get_class == Datastore_Database
> 
>   $model->loadBar(9); //loads data and populates
>   $model->setBar('bar');
>   $model->save(); //saves
> 
> }
> 
> *Model extends datastore*
> 
> This allows for Model portability, but it seems wrong to extend like this.
> Further, the datastore cannot make use of any of the Model's methods.
> 
> //controller extends model
> public function update()
> {
> 
>   $model = $this->loadHybrid('foo');  //get_class == Model
> 
>   $model->loadBar(9); //loads data and populates
>   $model->setBar('bar');
>   $model->save(); //saves
> 
> }
> 
> 
> 
> Any input on the "best" option - or alternative - would be most appreciated.
> 
> --
> PHP General Mailing List (http://www.php.net/) To unsubscribe, visit:
> http://www.php.net/unsub.php
> 
> 
> Bu mesaj ve ekleri, mesajda gönderildiği belirtilen kişi/kişilere özeldir ve
> gizlidir. Size yanlışlıkla ulaşmışsa lütfen gönderen kisiyi bilgilendiriniz 
> ve mesajı
> sisteminizden siliniz. Mesaj ve eklerinin içeriği ile ilgili olarak 
> şirketimizin
> herhangi bir hukuki sorumluluğu bulunmamaktadır. Şirketimiz mesajın ve
> bilgilerinin size değişikliğe uğrayarak veya geç ulaşmasından, bütünlüğünün
> ve gizliliğinin korunamamasından, virüs içermesinden ve bilgisayar sisteminize
> verebileceği herhangi bir zarardan sorumlu tutulamaz.
> 
> This message and attachments are confidential and intended for the
> individual(s) stated in this message. If you received this message in error,
> please immediately notify the sender and delete it from your system. Our
> company has no legal responsibility for the contents of the message and its
> attachments. Our company shall have no liability for any changes or late
> receiving, loss of integrity and confidentiality, viruses and any damages
> caused in anyway to your computer system.
No virus found in this outgoing message.
Checked by AVG - www.avg.com
Version: 8.5.423 / Virus Database: 270.14.34/2462 - Release Date: 10/27/09 
07:38:00
-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to