Re: What is the best practice for adding persistence to an MVC model?

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



It sounds like you need a combination of MVC and the 3 Tier Architecture. 
They are not the same thing, for reasons described at 
http://www.tonymarston.net/php-mysql/infrastructure-faq.html#faq26

I have combined both these patterns in the Radicore framework - take a look 
at http://www.radicore.org/. If you don't like the idea of downloading a 
large framework there is a small sample application described in 
http://www.tonymarston.net/php-mysql/sample-application.html which is 
available for download.

Basically you need to have code in every business object (every object in 
the Business layer) which communicates with a separate object for all its 
database access. This is known as a Data Access Object as it exists in the 
Data Access layer.

If you do it right it will be possible to have a separate DAO for each DBMS 
so that you can switch between one database engine and another by simply 
changing a single configuration variable. In my own framework I have 
implemented DAOs for MySQL, PostgreSQL and Oracle.

-- 
Tony Marston
http://www.tonymarston.net
http://www.radicore.org

"Eric Bauman" <baumane@xxxxxxxxxxxxxx> wrote in message 
news:B9.45.36627.EB296EA4@xxxxxxxxxxxxxxx
> 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


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux