I'd be interested to hear what other people say about this suggestion, but
since PHP is a loosely typed language, you could use that to your advantage.
To reduce confusion, I would suggest changing the name of the variable from $id
at this point, but what about doing something like:
is_numeric($id);
If that returns true, you've got an id, and you need to do the find query, if
is_numeric() returns false, you've passed in an array which you would consider
to have the structure of what the find result would have called.
This way, you could easily pass around the results to a find query, or have
your functions act appropriately if they are called with a mere id#.
On Feb 9, 2011, at 7:41 PM, Ryan Schmidt wrote:
> 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
--
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