1
votes

The Issue

I have two methods I need to call in my getContacts function. These methods are as follows:

Method 1: $authenticated = $this->authenticateUserInternal($inputData['auth']);

Method 2: $result = $this->Consumer->find('first', array( 'conditions' => array('Consumer.id' => 81)));

When I invoke these methods one after the other my resultset in $result does not have data from related tables. It seems like Method 1 is killing the relationship or affecting my results somehow, but it's a completely un-related function in the AppController.

The Code

The the Consumer model has a HABTM relationship to my Users model through a join table consumers_users.

Consumer Model

public $hasAndBelongsToMany = array(
'User' => array( 'className' => 'User', 'joinTable' => 'consumers_users', 'foreignKey'     => 'consumer_id', 'associationForeignKey' => 'user_id', ));

User Model

public $hasAndBelongsToMany = array(
'Consumer' => array(
    'className' => 'Consumer',
    'joinTable' => 'consumers_users',
    'foreignKey' => 'user_id',
    'associationForeignKey' => 'consumer_id',
));

ConsumersUsers Model

public $belongsTo = array(
'User' => array( 'className' => 'User', 'foreignKey' => 'user_id', ), 'Consumer' =>        array( 'className' => 'Consumer', 'foreignKey' => 'consumer_id', ), );

My normal result

"Consumer": {

"id": "81",
"fullname": "consumer2",
"email": "[email protected]",
"password": "111111",
"dateregistered": "2013-08-18",
"audio": "0"
}, "User": [

{
    "id": "179",
    "fullname": "leesa",
    "email": "[email protected]",
    "password": "111111",
    "imageurl": "http://bridgefiles.s3.amazonaws.com/2013-07-18-144613_IMG_0773.jpeg",
    "thumb_url": "http://bridgefiles.s3.amazonaws.com/2013-07-18-144613_IMG_0773.jpeg",
    "messagecount": "2",
    "dateregistered": "2013-07-29",
    "phone1": null,
    "phone2": null,
    "videoaddress": null,
    "ConsumersUser": {
        "id": "17",
        "consumer_id": "81",
        "status": "accepted",
        "user_id": "179"
    }
},

The Actual Result

"Consumer": {

"id": "81",
"fullname": "consumer2",
"email": "[email protected]",
"password": "111111",
"dateregistered": "2013-08-18",
"audio": "0"
}
1

1 Answers

0
votes

You are selecting only the table consumer, I think that the right way is to load the join table and insert recursive 1 to retrieve both table Consumer and User
try this:

$this->loadModel('ConsumersUsers');
 $result = $this->ConsumersUsers->find('first', array( 'conditions' => array('Consumer.id' => 81), 'recursive' => 1));