Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/241.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Cakephp有一个模型和hasAndBelongsToMany模型_Php_Cakephp_Relationship_Cakephp Model - Fatal编程技术网

Cakephp有一个模型和hasAndBelongsToMany模型

Cakephp有一个模型和hasAndBelongsToMany模型,php,cakephp,relationship,cakephp-model,Php,Cakephp,Relationship,Cakephp Model,我对CakePHP 2.x中的CakePHP belongsTo和HasandBlongstomy关系有问题 示例情况 表用户 餐桌组织 表用户\组织\权限 用户模型 hasAndBelongsToMany(Organisation); belongsTo(Organisation) 用户属于一个组织,但拥有多个组织的权限,导致以下冲突: $aUser = $this->User->findById(1); print_r($aUser); // Output # With t

我对CakePHP 2.x中的CakePHP belongsTo和HasandBlongstomy关系有问题

示例情况

表用户

餐桌组织

表用户\组织\权限

用户模型

hasAndBelongsToMany(Organisation);
belongsTo(Organisation)
用户属于一个组织,但拥有多个组织的权限,导致以下冲突:

$aUser = $this->User->findById(1);
print_r($aUser);

// Output

# With the belongsTo relation
array(
    'User' => array(
        'id' => 1,
        'organisation_id' => 1
        'name' => 'Test User'
    ),
    'Organisation' => array(
        'id' => 1,
        'name' => 'Test organisation'
    )
);

# With the hasAndBelongsToMany relation
array(
    'User' => array(
        'id' => 1,
        'organisation_id' => 1
        'name' => 'Test User'
    ),
    'Organisation' => array(
        1 => array(
            'id' => 1,
            'name' => 'Test organisation'
        ),
        2 => array(
            'id' => 1,
            'name' => 'Test organisation'
        )
    )
);

# When both relations are enabled it doesn't work
有人能解决这场冲突吗


对于这一冲突,是否有一个本地CakePHP解决方案?

答案实际上在CakePHP 2.x食谱中

同一模型的多个关系

在某些情况下,一个模型与另一个模型有多个关系。例如,您可能有一个与用户模型有两种关系的消息模型:一种是与发送消息的用户的关系,另一种是与接收消息的用户的关系。messages表将有一个field user_id,但也有一个field recipient_id。现在,您的邮件模型可以如下所示:


来源:

您指的是什么冲突?ps,请始终提及您的确切CakePHP版本!好的,我补充了一些解释。我明白了,我已经怀疑:@ndm所以在第一个示例中,他们显示的发送者和接收者就像虚拟模型一样?好吧,根据你所说的虚拟,你可能会这样称呼它,尽管实际上没有虚拟化,它就像在两个不同名称的变量中存储对对象的引用一样虚拟。发送方和接收方只是别名,将使用别名代替实际的模型名。
id
user_id
organisation_id
hasAndBelongsToMany(Organisation);
belongsTo(Organisation)
$aUser = $this->User->findById(1);
print_r($aUser);

// Output

# With the belongsTo relation
array(
    'User' => array(
        'id' => 1,
        'organisation_id' => 1
        'name' => 'Test User'
    ),
    'Organisation' => array(
        'id' => 1,
        'name' => 'Test organisation'
    )
);

# With the hasAndBelongsToMany relation
array(
    'User' => array(
        'id' => 1,
        'organisation_id' => 1
        'name' => 'Test User'
    ),
    'Organisation' => array(
        1 => array(
            'id' => 1,
            'name' => 'Test organisation'
        ),
        2 => array(
            'id' => 1,
            'name' => 'Test organisation'
        )
    )
);

# When both relations are enabled it doesn't work
class Message extends AppModel {
    public $belongsTo = array(
        'Sender' => array(
            'className' => 'User',
            'foreignKey' => 'user_id'
        ),
        'Recipient' => array(
            'className' => 'User',
            'foreignKey' => 'recipient_id'
        )
    );
}