2
votes

I cannot get the associated Model data using hasMany through (Join Model) association. I'm working with cakephp 2.2.3 The same find were ok in cakephp 1.3, so i don't know what's the matter...

Event hasMany ScoresToEvent. Scores hasMany ScoresToEvent. ScoresToEvent belongsTo Event and Score.

ScoresToEvent will have additional info, so i cannot use HATBM.

some code:

event.php    
class Event extends AppModel{
      public $name='Event';
      public $hasMany=array('ScoresToEvent');
      public $belongsTo=array('Entity');
      public $actsAs=array('containable');
} 

score.php
class Score extends AppModel{
      public $name='Score';
      public $hasMany=array('ScoresToEvent');
      public $belongsTo=array('Entity');
}

scores_to_event.php
class ScoresToEvent extends AppModel{
   public $name='ScoresToEvent';
   public $belongsTo=array('Event','Score');
}

When i retrieve data i get these results:

$this->Event->ScoresToEvent->find('all', array('recursive'=>2))
array(
    (int) 0 => array(
        'ScoresToEvent' => array(
            'id' => '8',
            'event_id' => '7',
            'score_id' => '1'
        )
    ),
    (int) 1 => array(
        'ScoresToEvent' => array(
            'id' => '9',
            'event_id' => '7',
            'score_id' => '3'
        )
    )
) 

In this case i have to get Event and Score data.

If I try to use containable it returns that Model "ScoresToEvent" is not associated with model "Score" and this array, consequently it doesn't retrieve the Score data...

$this->Event->find('all', array(
      'contain'=>array(
         'Entity',
         'ScoresToEvent'=>array('Score'=>array('Entity'))
      ),
      'conditions'=>array('Event.id'=>7));

array(
    (int) 0 => array(
        'Event' => array(
            'id' => '7',
            'entity_id' => '17',
            'start_date' => '2012-07-24',
            'status' => null,
            'end_date' => null
        ),
        'Entity' => array(
            'id' => '17',
            'title' => 'y',
            'content' => '',
            'subtitle' => '',
            'type' => 'Evento',
            'avatar' => null,
            'image' => null
        ),
        'ScoresToEvent' => array(
            (int) 0 => array(
                'id' => '8',
                'event_id' => '7',
                'score_id' => '1'
            ),
            (int) 1 => array(
                'id' => '9',
                'event_id' => '7',
                'score_id' => '3'
            )
        )
    )
)

Where is my fault? what part of code is wrong? I tried this on a brand new cakephp 2.2.3 installation

Thanks all

p.s. The same code works correctly in cakephp 1.3 p.p.s. do not consider 'Entity'.

1

1 Answers

3
votes

I think is a problem of name of the model. Try to rename the file scores_to_event.php to ScoresToEvent.php. But in this case I think that the bestname ofr this model is: ScoreEvent.php, is more appropriate. After try to insert more information to your model like this example: into ScoreEvent.php

public $belongsTo = array(
        'Event' => array(
            'className'    => 'Event',
            'foreignKey'   => 'event_id'
        ),
       'Score' => array(
            'className'    => 'Score',
            'foreignKey'   => 'score_id'
        )
);