1

问题

我需要在 getContacts 函数中调用两种方法。这些方法如下:

方法一: $authenticated = $this->authenticateUserInternal($inputData['auth']);

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

当我一个接一个地调用这些方法时,我在 $result 中的结果集没有来自相关表的数据。似乎方法 1 正在以某种方式破坏关系或影响我的结果,但它是 AppController 中完全不相关的功能。

编码

Consumer 模型通过连接表 consumer_users 与我的 Users 模型具有 HABTM 关系。

消费模式

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

用户模型

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

消费者用户模型

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

我的正常结果

"Consumer": {

"id": "81",
"fullname": "consumer2",
"email": "consumer2@cici.mailgun.org",
"password": "111111",
"dateregistered": "2013-08-18",
"audio": "0"
}, "User": [

{
    "id": "179",
    "fullname": "leesa",
    "email": "lda@hotmail.com",
    "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"
    }
},

实际结果

"Consumer": {

"id": "81",
"fullname": "consumer2",
"email": "consumer2@cici.mailgun.org",
"password": "111111",
"dateregistered": "2013-08-18",
"audio": "0"
}
4

1 回答 1

0

您只选择表消费者,我认为正确的方法是加载连接表并插入递归 1 以检索表消费者和用户
试试这个:

$this->loadModel('ConsumersUsers');
 $result = $this->ConsumersUsers->find('first', array( 'conditions' => array('Consumer.id' => 81), 'recursive' => 1));
于 2013-08-22T20:21:45.073 回答