You can use Set::extract for tidying array
On Thu, 2009-04-09 at 06:56 -0700, rossjha wrote:
> This seems to be working.
>
> $this->Tag->unbindModel(array('hasAndBelongsToMany' => array
> ('Improvements')));
> $this->Tag->bindModel(array('hasOne' => array(
> 'ImprovementsTag',
> 'FilterTag' => array(
> 'className' => 'Tag',
> 'foreignKey' => false,
> 'conditions' => array('FilterTag.id =
> ImprovementsTag.tag_id')
> )
> )));
> $tags = $this->Tag->find('all', array(
> 'conditions'=>null,
> 'fields' => array('Tag.*', 'COUNT(FilterTag.id)
> AS no_tags'),
> 'group' => 'Tag.id'
> ));
>
> Array
> (
> [0] => Array
> (
> [Tag] => Array
> (
> [id] => 1
> [title] => Effective Glucose Control
> )
>
> [0] => Array
> (
> [no_tags] => 2
> )
>
> )
>
> Is there a way of tidying this array so no_tags is part of the Tag
> element?
>
> Cheers
>
> R
>
> On Apr 9, 1:45 pm, "[email protected]"
> <[email protected]> wrote:
> > Hi i have the following associations:
> >
> > var $hasAndBelongsToMany = array('Improvements');
> > var $hasAndBelongsToMany = array('Tags');
> >
> > With a join table improvements_tags
> >
> > I want to be able to count the number of time a tag has been used.
> > The following sql query works, but i'm not sure how to do this in
> > CakePHP?
> >
> > SELECT tags.id, tags.title, COUNT(improvements_tags.tag_id) AS no_tags
> > FROM tags, improvements_tags WHERE tags.id = improvements_tags.tag_id
> > GROUP BY tags.id
> >
> > I've tried to bindModel with a count in the find condition, but it
> > never seems to work. Any help would be really appreciated.
> >
> > Ross
> >
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---