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.
RE: [PHP] What is the best practice for adding persistence to an MVC model?
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
Re: [PHP] What is the best practice for adding persistence to an MVC model?
On Tue, Oct 27, 2009 at 05:27:07PM +1100, Eric Bauman wrote: 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 disagree. There should be database class(es) which know how to deal with the datastore(s). This may be as simple as a thin layer over the PDO classes. The model talks to and uses the database class, and is the only component which deals with the database class. The model serves up whatever data the controller needs to feed to the view. All are welcome to disagree. Paul -- Paul M. Foster -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] What is the best practice for adding persistence to an MVC model?
2009/10/27 Paul M Foster pa...@quillandmouse.com: On Tue, Oct 27, 2009 at 05:27:07PM +1100, Eric Bauman wrote: 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 disagree. There should be database class(es) which know how to deal Paul, you already did this in one of Eric's threads a couple of weeks back, and the best you could come up with when pressed was Probably not a great example, but Is it really necessary to rehash this? Eric, I recommend reading up on how to develop testable code (especially Mock Objects) - it's great discipline because it requires everything to be decoupled. The short version is that it's often useful in unit testing to fake the classes that your target relies on, so that you're testing it in isolation. For example you could provide the class under test with a fake DB class that returns static results. It's much easier to do this when you pass objects /to/ your class: class MyClass { function MyClass($db) { } } than when your class inherits from the object: class MyClass extends Db { } or when the class instantiates the object: class MyClass { function MyClass() { $this-db = new DB_CLASS; } } If you go with the first approach, you're writing code that you and anyone who comes after you can write useful tests for. The others, and you're denying maintenance programmers a useful tool. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] What is the best practice for adding persistence to an MVC model?
On Tue, Oct 27, 2009 at 04:11:32PM +, David Otton wrote: 2009/10/27 Paul M Foster pa...@quillandmouse.com: On Tue, Oct 27, 2009 at 05:27:07PM +1100, Eric Bauman wrote: 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 disagree. There should be database class(es) which know how to deal Paul, you already did this in one of Eric's threads a couple of weeks back, and the best you could come up with when pressed was Probably not a great example, but Is it really necessary to rehash this? I don't track threads closely enough to know if it's the same OP on this thread. Apologies if I'm repeating myself. I don't recall anyone disputing my example, nor any effective rebuttal at all. And by the time I finished writing that email, I thought it was a better example than I originally believed. Paul -- Paul M. Foster -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] What is the best practice for adding persistence to an MVC model?
2009/10/27 David Otton phpm...@jawbone.freeserve.co.uk: If you go with the first approach, you're writing code that you and anyone who comes after you can write useful tests for. The others, and you're denying maintenance programmers a useful tool. I should have lead with this: the wikipedia article on Dependency injection http://en.wikipedia.org/wiki/Dependency_injection -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php