Monday, February 2, 2009

Re: BeforeFind

I'd say that beforeFind should be used for thing that you want to run
at all times.

Instead of doing this in beforeFind you might want to create a custom
find which is a good way to enforce custom logic to the find operation
while leaving any associations and other internal queries alone.

As a simple example. To find blocked and not blocked items from a
table (using a simple "blocked" field) you can define this in your
model:

function find($type, $options = array()) {
switch ($type) {
case 'count_not_blocked':
case 'first_not_blocked':
case 'all_not_blocked':
$type = substr($type,0,strpos($type,'_'));
if ( !isset($options['conditions']) ){
$options['conditions'] = array();
}elseif ( !is_array($options['conditions']) ) {
$str = $options['conditions'];
$options['conditions'] = array($str);
}
$options['conditions'][] = array($this-
>alias.'.blocked'=>0);
break;

case 'count_blocked':
case 'first_blocked':
case 'all_blocked':
$type = substr($type,0,strpos($type,'_'));
if ( !isset($options['conditions']) ){
$options['conditions'] = array();
}
$options['conditions'][] = array($this-
>alias.'.blocked'=>1);
break;

case default:
break;
}

return parent::find($type, $options);
}


Then you can call:
$this->Modelname->find('all_blocked');
$this->Modelname->find('count_not_blocked');

There are several articles available detailing variations on this
concept. Among them:
http://cakebaker.42dh.com/2008/03/23/defining-custom-find-types/
http://www.pseudocoder.com/archives/2008/10/24/cakephp-custom-find-types/

I am not sure if something like this might suit your application but
it is a nice tool to use sometimes.

On Feb 2, 2:40 am, Fred <fb...@multiply.it> wrote:
> Thanks...that is very helpful. One thing to note it looks like the
> children method of a tree doesn't support callbacks of false (line 275
> of tree.php has only to recursive on the find, but no callback
> passed). I will just use a find directly instead of using the children
> method.
>
> Thanks.
>
> Fred
>
> On Feb 1, 4:33 pm, Miles J <mileswjohn...@gmail.com> wrote:
>
> > In your find() queries, you can add a parameter: 'callbacks' => false
>
> >http://book.cakephp.org/view/73/Retrieving-Your-Data
>
> > On Feb 1, 11:59 am, Fred <fb...@multiply.it> wrote:
>
> > > I am using BeforeFind in my Model to filter based on criteria for a
> > > logged in user. This works great and I love how easy this is to do...
> > > However, I am running into a problem. There are certain times that I
> > > don't want to add the filter. For example I have a tree of company's
> > > that a user is in and when I get the children of an element in the
> > > tree I want everything not to have the beforeFind filter things. So
> > > how do I determine what is calling beforeFind? If I could find out
> > > what is calling beforeFind it would make it easy and queryData doesn't
> > > contain anything. I think this is probably something easy that I just
> > > cannot find in the manual. I have hacked it by saying something like:
> > >    if ($queryData['order'][0] == 'Company.lft asc') {
> > >         return $queryData;
> > >    }
> > > This works, but it is 1) really ugly and 2) really poor coding ... how
> > > do I do this correctly?
>
> > > Thanks.
> > > Fred
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "CakePHP" group.
To post to this group, send email to cake-php@googlegroups.com
To unsubscribe from this group, send email to cake-php+unsubscribe@googlegroups.com
For more options, visit this group at http://groups.google.com/group/cake-php?hl=en
-~----------~----~----~----~------~----~------~--~---

No comments: