Maybe a suggestion which does not infects performance;
1. Add a collumn to the photoset table containing the number of photos
in the set. You can use this in your users/view action with no loss of
performance
2. When adding or deleting photos to a set this collumn in the
photoset model needs to be altered. Do this in the Photo model
(afterSave and afterDelete actions). This has a very low decrease of
performance added to the Photos edit, add & delete actions.
Success.
On Jul 20, 1:57 pm, tawm <[EMAIL PROTECTED]> wrote:
> Hey Geoff,
>
> Thanks for your reply. I guess your approach is the best possible one.
> I do not like it tremendously though. Thanks to CakePHP's associations
> all photosets are automatically part of a user data-array. So we
> already have them, and therefore would rather not findAll them again.
> Also, since we are already looping the photosets in the view, I don't
> like to loop it once again in the controller. But I guess I'm asking
> too much.
>
> The ideal solution would be, in my opinion, to have a model instance
> or value object passed to the view, instead of a data array. That
> object then can contain not only data fields, but also methods that
> deliver meta-data such as age (based on birthdate) for user instances
> or number of photos for photoset instances. I think this idea will be
> only be looked at towards version 2.0 though...
>
> Tim
>
> On 20 jul, 13:30, Geoff Ford <[EMAIL PROTECTED]> wrote:
>
> > 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
-~----------~----~----~----~------~----~------~--~---