Re: How to filter a model that is using a hasMany?

2008-10-27 Thread Mathew

wow, thanks so much for the help!

That will work perfectly. :)
--~--~-~--~~~---~--~~
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
-~--~~~~--~~--~--~---



Re: How to filter a model that is using a hasMany?

2008-10-27 Thread grigri

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
-~--~~~~--~~--~--~---



Re: How to filter a model that is using a hasMany?

2008-10-27 Thread Mathew

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
-~--~~~~--~~--~--~---



How to filter a model that is using a hasMany?

2008-10-27 Thread Mathew

Hi,

Let's say I have the following models.

class User extends AppModel {
   var $name="User";
   var $hasMany=array('Document');
}

class Document extends AppModel {
   var $name="Document";
}

Now some Users have published documents, and some Users have not. So I
want to create a SQL condition that will only find all the Users who
have some Documents, but ignore Users who have none.

I can't figure out the condition for this. Everything I try produces a
SQL error.

$data = $this->User->find('all',array('conditions'=>array()));
--~--~-~--~~~---~--~~
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
-~--~~~~--~~--~--~---