Consider that I have a model Foo that has methods doSomething and 
doAnotherThing that do something useful. These methods need data from the 
database, so they use $this->find. But they also need to call another method, 
isOk, to do some verification on the record first:


class Foo extends AppModel {
        function isOk($id) {
                $data = $this->find('first', array('conditions' => 
array('Foo.id' => $id)));
                if (!$data) return false;
                // now use $data and other factors to determine if it's ok and 
return true or false
        }
        function doSomething($id) {
                if (!$this-> isOk($id)) return false;
                $data = $this->find('first', array('conditions' => 
array('Foo.id' => $id)));
                if (!$data) return false;
                // now do something with $data
        }
        function doAnotherThing($id) {
                if (!$this-> isOk($id)) return false;
                $data = $this->find('first', array('conditions' => 
array('Foo.id' => $id)));
                if (!$data) return false;
                // now do something else with $data
        }
}


Regardless whether I call doSomething or doAnotherThing, essentially the same 
find command is run twice; this seems wasteful. How should I structure this so 
that the data is not fetched a second time?

Or does some manner of CakePHP caching make this a non-issue? (Is there query 
results caching?)

I am not yet using Containable, though I believe I probably should be. I wonder 
if that will make it a non-issue as well; the fields necessary to determine if 
the record isOk might be different than those needed to doSomething.

All three of the above methods will potentially be called from controllers and 
shell scripts, so I would dislike to change the signature of isOk so that it 
accepts a $data array; it seems most logical that all three would accept an $id 
as their argument, but I don't know how people typically do this.



-- 
Our newest site for the community: CakePHP Video Tutorials 
http://tv.cakephp.org 
Check out the new CakePHP Questions site http://ask.cakephp.org and help others 
with their CakePHP related questions.


To unsubscribe from this group, send email to
[email protected] For more options, visit this group at 
http://groups.google.com/group/cake-php

Reply via email to