Note: these examples put the binding in the controller. It makes much
more sense to put them in the model.

// Get all users with documents:
$this->User->bindModel(array(
  'hasOne' => array(
    'CheckDocument' => array(
      'className' => 'Document',
      'fields' => 'CheckDocument.id',
      'type' => 'inner'
    )
  )
), false);
$data = $this->User->find('all', array(
  'group' => 'User.id',
  'recursive' => 0 // At least 0 for joins to work
));

(No conditions needed since you're using an inner join. If you use a
left join (the default) you'll have to add a condition)

// Get all users with no documents:
$this->User->bindModel(array(
  'hasOne' => array(
    'CheckDocument' => array(
      'className' => 'Document',
      'fields' => '(COUNT(CheckDocument.id)) AS num_documents',
      'type' => 'left'
    )
  )
), false);
$data = $this->User->find('all', array(
  'group' => 'User.id',
  'recursive' => 0, // At least 0 for joins to work
  'conditions' => array(
    'CheckDocument.id IS NULL'
  )
));

// Get all users with 3 or more documents, ordered by number of
documents:
$this->User->bindModel(array(
  'hasOne' => array(
    'CheckDocument' => array(
      'className' => 'Document',
      'fields' => '(COUNT(CheckDocument.id)) AS num_documents',
      'type' => 'inner'
    )
  )
), false);
$data = $this->User->find('all', array(
  'group' => 'User.id',
  'recursive' => 0, // At least 0 for joins to work
  'conditions' => array(
    'num_documents >=' => 3
  ),
  'order' => 'num_documents DESC'
));

hth
grigri

On Oct 27, 3:20 pm, Mathew <[EMAIL PROTECTED]> wrote:
> The only thing I can think of is to have a field in User that I update
> when adding Documents, and then filter on that field.
>
> This seems kind of like a hack?
--~--~---------~--~----~------------~-------~--~----~
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 [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/cake-php?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to