techie_28
techie_28

Reputation: 2133

How to associate models correctly cakephp

I have a scenario where i have templates which has many themes. Clear enough that my relation will be template hasmany themes I wanna show the template with number of themes and i am applying this code:

$this->Template->recursive = 2;
     $this->Template->bindModel(
                         array(
                          'hasMany' =>array(
                            'TemplateTheme'=>array(
                               'className'=>'TemplateTheme',
                               'fields'  => 'count(TemplateTheme.id) AS themes'
                           )
                          )
                      ),false
   );
   $this->paginate = array(
                      'order' => array('Template.modified DESC'),
                      'limit' =>$limit
                    );   
     $template = $this->paginate('Template');
     pr($template);die();

but i am getting is

Array
(
    [0] => Array
        (
            [Template] => Array
                (
                    [id] => 1
                    [name] => churchDesign
                    [status] => Active
                    [created] => 2011-10-24 10:37:23
                    [modified] => 2011-10-25 15:16:46
                )

            [TemplateTheme] => Array
                (
                    [0] => Array
                        (
                            [template_id] => 1
                            [TemplateTheme] => Array
                                (
                                    [0] => Array
                                        (
                                            [themes] => 3
                                        )

                                )

                        )

                )

        )

    [1] => Array
        (
            [Template] => Array
                (
                    [id] => 2
                    [name] => blossoms
                    [status] => Active
                    [created] => 2011-10-19 00:00:00
                    [modified] => 2011-10-24 14:05:27
                )

            [TemplateTheme] => Array
                (
                )

        )

) 

The problem here is it is counting all the themes in first array(1st template).see [TemplateTheme] => Array ( [0] => Array ( [themes] => 3 ) the third theme actually belongs to the 2nd template.The relation is

template_id is in themes table to represent each theme for a template.

Template 1 has 2 themes and template 2nd has one theme and currently its showing all the three themes in the first template.

i want it like this way

templatename1
count of themes 1st template

template name 2
count of themes of 2nd template

Please tell me the right way to do this.

Upvotes: 0

Views: 124

Answers (3)

Vanja D.
Vanja D.

Reputation: 854

If you wanted to get the count of the Themes per each Template, the most elegant way is to use counterCache.

Another way is to use Containable behavior, which fetches only the related data which you wish, and then count the fetched data array in PHP. After you attach the containable behavior to the Template model, some code like this should work:

$allTemplates = $this->Templates->find('all', array(
                      'contain' => 'TemplateTheme.name'
                 ));
foreach($allTemplates as $template){
    $currentThemeCount = count($template['TemplateTheme']);
    foreach($template['TemplateTheme'] as $theme){
         echo $theme['name'];
    }
}

Hope this helps someone along the way.

Upvotes: 1

Oldskool
Oldskool

Reputation: 34877

You should be using the find('count') method instead of using a virtual 'count' field like you're doing now. For example:

$allTemplates = $this->Template->find('all');
foreach($allTemplates as $data) {
    $templateCount = $this->Template->TemplateTheme->find('count', array(
        'conditions' => array(
            'TemplateTheme.template_id' => $data['Template']['id']
        )
    ));
    // $templateCount should now contain the count of this specific template. Foreach will continue recursion for all others.
}

Upvotes: 0

mentes
mentes

Reputation: 118

you can count the themes array like

foreach($template as $t){
    count($t['TemplateTheme'])
}

hope this helps

Upvotes: 0

Related Questions