Try this:

$this->Plate->bindModel(array('belongsTo' => array(
  'CustomerX' => array(
    'className' => 'Customer',
    'foreignKey' => false,
    'conditions' => 'CustomerX.id=User.customer_id',
    'fields' => 'CustomerX.id'
  ),
  'DealerX' => array(
    'className' => 'Dealer',
    'foreignKey' => false,
    'conditions' => 'DealerX.id=CustomerX.dealer_id',
    'fields' => 'DealerX.id'
  )
)));

$this->Plate->find('all', array(
  'recursive' => 0,
  'conditions' => "DealerX.type <> 'drug_dealer'" // or whatever
));

The 'CustomerX' and 'DealerX' aliases just have to be different from
the actual name to avoid collisions.

I've been thinking about trying to put this in a behavior, but it's
difficult to extract model/field names from conditions arrays and
strings. If I find a good solution I'll post it.

hth
grigri

On Jul 24, 2:35 pm, Jon Hinson <[EMAIL PROTECTED]> wrote:
> I tried doing something along the lines of this:
>
> $this->Plate->find('all',
> array('contain'=>array('User'=>array('contain'=>array('Customer'=>array('Dealer'=>array('conditions'=>array('Dealer.id
> ='=>$id))))))));
>
> But, I'm afraid that did not work. It gets all of the Plates for that
> particular Dealer, and then it goes on to fetch ALL plates. It ran
> something like 140 queries (I had a lot of Plates) Surely there is a
> easy way to produce a SIMPLE query like this:
>
> SELECT *
> FROM plates p
> JOIN users u ON u.id = p.user_id
> JOIN customers c on c.id = u.customer_id
> JOIN dealers d on d.id = c.dealer_id
> WHERE d.id = $id;
>
> I'm wondering if they could build in to cake a way to just follow each
> belongsTo relationship, and for each one a join statement is added
> until x levels deep.
>
> something like this:
>
> $this->Plate->find('join', array('depth'=>3,
> 'conditions'=>array('Dealer.id ='=>$id)));
>
> So what this would do is look for a belongsTo relationship in Plate,
> and it would find User ( depth of 1) and add that join to the query.
> Then, it searches User for belongsTo and finds Customer (depth of 2)
> and adds the join, and finally it would look for the belongsTo in
> Customer and find Dealer (depth of 3) and add the join. It would then
> add the conditions to the where clause. There you have it, you'd be
> able to paginate the plates results. I would absolutely, positively be
> madly in love with this feature.
>
> Jon Hinson
>
> On Jul 23, 8:29 am, Marc <[EMAIL PROTECTED]> wrote:
>
> > This still doesnt make any difference, also saw i had put not working
> > code:
>
> > $this->paginate = array(
> >                         'contain' => array(
> >                                         'User.Customer.Dealer' =>
> > array('id'=>$id)
> >                         )
> >                 );
>
> > had to be
> > $this->paginate = array(
> >                         'contain' => array(
> >                                         'User.Customer.Dealer' =>
> > array('conditions' => array('id' => $id))
> >                         )
> >                 );
>
> > But reset doesnt make any difference, still get themall.
>
> > On 23 jul, 14:48, "Amit Badkas" <[EMAIL PROTECTED]> wrote:
>
> > > 2008/7/23 Marc <[EMAIL PROTECTED]>:
>
> > > > Well, tried several options, im now working with RC2 and tried
> > > > Containable behaviour.
>
> > > > i now have the following code in my DealersController:
>
> > > >        function listPlates($id = null) {
> > > >        $this->Dealer->Behaviors->attach('Containable');
> > > >                $this->Dealer->recursive = -1;
> > > >                $this->paginate = array(
> > > >                        'contain' => array('Customer.User.Plate'),
> > > >                        'conditions' => array('Dealer.id' => $id)
> > > >                );
> > > >                $plates = $this->paginate('Dealer');
> > > >                pr($plates);
> > > >        }
>
> > > > I also tried in my PlatesController:
>
> > > >        function listPlatesByDealer($id = null) {
> > > >        $this->Plate->Behaviors->attach('Containable');
> > > >                $this->Plate->recursive = -1;
> > > >                $this->paginate = array(
> > > >                        'contain' => array(
> > > >                                        'User.Customer.Dealer' =>
> > > > array('id'=>$id)
> > > >                        )
> > > >                );
> > > >                $plates = $this->paginate('Plate');
> > > >                pr($plates);
> > > >        }
>
> > > > Still i dont know how i can get just the plates related to one dealer.
>
> > > - You also have to provide 'reset' => false in paginate array like
>
> > >                $this->paginate = array(
> > >                        'contain' => array(
> > >                                        'User.Customer.Dealer' =>
> > > array('id'=>$id)
> > >                        ),
> > >                        'reset' => false
> > >                );
>
> > > --
> > > Amit
>
> > >http://amitrb.wordpress.com/http://coppermine-gallery.net/http://chee...
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"CakePHP" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/cake-php?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to