Option 3. From the model via the controller e.g.

Users controller.
$this->set('photosets', $this->User->PhotoSet-
>findWithCount($conditions));

PhotSet Model

function findWithCount($conditions){
  $this->recursive = -1; //stops all associations
  $photosets = $this->findAll($conditions);  // if you need sort,
fields etc you will have to pass them through as well

  //loop the $photosets and add a field called count
  foreach ($photosets as $key => $set) {
    $photosets[$key']['PhotoSet']['count'] = $this->Photo-
>findCount(array('photoset_id' => $set['PhotoSet']['id']));
  }
}

Geoff
--
http://lemoncake.wordpress.com


On Jul 20, 8:24 pm, tawm <[EMAIL PROTECTED]> wrote:
> Hello,
>
> I'm dealing with the following dilemma, simply cannot decide what's
> the best approach. Your $0.02 is highly appreciated!
>
> Consider the following structure: User hasMany Photoset, which in turn
> hasMany Photo.
>
> The UserController has a view (/users/view/{id}) action, which shows
> the user's data and its photosets. Each photoset is represented as its
> title and the total number of photos within the set. Since in this
> action we do not need to have the data of all a photoset's photos at
> our disposal, we unbinded the Photoset hasMany Photo association. As a
> result, in order to know how many photos a photoset contains, we need
> to query this additionally.
>
> Obviously, the method that will perform this photo-count-query is to
> be implemented in the Photoset Model, as it is meta-data of a
> Photoset.
>
> Now the question is:>From where do we call this method? -OR- How do we move 
> the result of
>
> this method from model to view?
>
> Option 1: from the Controller
> Your first reaction might be; easy, that's what a Controller is for!
> It has access to the Model and can hand data over the View. True, but
> in our situation this approach has a huge disadvantage as well.
> Remember that we are talking about the UserController here, while the
> values we want to obtain are the total number of photos for *every*
> photoset that belongs to this user. I.e. if we want to call the photo-
> count-method from the Controller, it means we will have to loop all
> photosets belonging to this user, call the method and store its result
> for every one of them and pass this information forward to the View.
> Then of course the View will have to loop all of them again. That's a
> lot of code and redundant loops!
>
> Option 2: from the View
> Eventually, we need to have the photo-count-value at our disposal in
> the appropriate View. So it would be convenient to send out the
> request for this value in the View as well. But, from a View we can
> reach Controller nor Model directly, so this is a problem. Our only
> option would be to call requestAction, but considered that we just
> need a single integer value (the photo count) that feels like
> overkill. Furthermore, we just need some basic data from the database
> -- no controlling involved -- so a call to the Model seems more
> appropriate than a call to the Controller (???). The only type of call
> that we can make to the Model is a static one though, which is
> insufficient for our needs, since we will be using the Model's find*
> methods which are instance methods.
>
> That were our options. None of them satisfies me. What would be your
> approach?
>
> Tim


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Cake 
PHP" 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