I agree with John Andersen, you should use the counterCache in this
situation, but to answer your question -- how do you preform a count
in CakePHP from a hasMany relationship -- you get a count like this:

//in a events_controller.php within
function view($id){
  $attendants_count = $this->Event->EventRegistration->find('count',
array('conditions' => 'EventRegistration.event_id' => $id));
  $this->set(compact('attendants_count'));
}
That's the easiest way to get a count of a hasMany relationship from a
parent. However, I highly suggest using counterCache so you'll have
this count data within your parent table (events).

HTH
Nick


On Apr 15, 4:05 pm, mdb <[email protected]> wrote:
> Hello,
>
> I have Events view.ctp where i display list of Events (Event Name ,
> Location and Date)
>
> In EventRegistration table i have the users registered for each event.
> (Event id , UserId)
>
> Now in my EventView.ctp i need to add no. of registered users for each
> event.
>
> the SQL query will be
>
> Select a.EventName,a.EventLocation,a.Date,
> (Select count(*) from EventRegistraion b where b.EventId = a.EventID)
> as userscount
>  from Events a.
>
> How do i write the equivalent of this query in cakephp?
>
> Please Help

Check out the new CakePHP Questions site http://cakeqs.org and help others with 
their CakePHP related questions.

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