使用cakephp约定的两个表之间的两个关系

使用cakephp约定的两个表之间的两个关系,cakephp,Cakephp,按照约定,如何在同一个表中创建包含两个外键的表。以博客教程和教程简单身份验证和授权应用程序为例,其中表中有一个user_id post,表示引用用户表的post的创建者。 如何设计存储表,除了开发人员之外,其他用户可能是修改者 CREATE TABLE users ( id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY, username VARCHAR(50), password VARCHAR(50), role VARCHA

按照约定,如何在同一个表中创建包含两个外键的表。以博客教程和教程简单身份验证和授权应用程序为例,其中表中有一个user_id post,表示引用用户表的post的创建者。 如何设计存储表,除了开发人员之外,其他用户可能是修改者

CREATE TABLE users (
    id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
    username VARCHAR(50),
    password VARCHAR(50),
    role VARCHAR(20),
    created DATETIME DEFAULT NULL,
    modified DATETIME DEFAULT NULL,
    user_id INT(11);// cakephp here knows the relationship itself
);
显然,我不能添加另一列user_id来表示修改的用户。 我怎么能做到,可能吗?
对不起,我的英语

CakePHP约定的创建有几个原因;保持一致性并减少使应用程序工作所需的配置量

但是,CakePHP并不禁止您使用不符合约定的命名。在某些情况下,您将不得不忽略这些约定—想想将用于CakePHP项目的第三方数据库,该项目不遵循CakePHP约定,您的情况就是其中之一

“同一”模型的多个关系 Cake允许您为每个关系的模型设置不同的别名。通过这种方式,您可以实现您在问题中描述的内容,而不会产生冲突。但是,由于thd second relation的外键不会遵循CakePHP约定,CakePHP不会自动选择正确的列,因此您必须自己指定

比如说

class Post extends AppModel {

    public $belongsTo = array(
        // CakePHP will automatically use 'user_id' as foreign key
        'User',

        // Here we create an 'alias' for the User Model
        // also, we need to specify the foreign key to use
        // for the relation
        'Editor' => array(
            'className'   => 'User',       // name of the Model
            'foreignKey'  => 'editor_id',  // foreign key for this relation
        ),
    );
}
class Post extends AppModel {
    public $belongsTo = array(
       'Editor' => array(
            'className'   => 'User',
            'foreignKey'  => 'editor_id',
        ),
    );
}

class Comment extends AppModel {
    public $belongsTo = array(
       'Editor' => array(
            'className'   => 'Visitor',    // WRONG!! Editor alias is already 
                                           // used as alias for the User model!
            'foreignKey'  => 'editor_id',
        ),
    );
}
Post模型现在与用户模型有两种关系。一个有别名编辑器的 别名的工作方式与“常规”模型相同;i、 就像有一个编辑模型附在帖子上

在控制器中,两个查找都将返回所有用户,并实际使用“用户”模型:

$this->Post->User->find('all');
$this->Post->Editor->find('all');
重要的 CakePHP使用别名来缓存模型信息。如果对一个模型使用别名,则决不能对另一个模型使用相同的别名。CakePHP可能会在这些情况下使用其他模型的缓存信息,从而导致不可预测的结果

比如说

class Post extends AppModel {

    public $belongsTo = array(
        // CakePHP will automatically use 'user_id' as foreign key
        'User',

        // Here we create an 'alias' for the User Model
        // also, we need to specify the foreign key to use
        // for the relation
        'Editor' => array(
            'className'   => 'User',       // name of the Model
            'foreignKey'  => 'editor_id',  // foreign key for this relation
        ),
    );
}
class Post extends AppModel {
    public $belongsTo = array(
       'Editor' => array(
            'className'   => 'User',
            'foreignKey'  => 'editor_id',
        ),
    );
}

class Comment extends AppModel {
    public $belongsTo = array(
       'Editor' => array(
            'className'   => 'Visitor',    // WRONG!! Editor alias is already 
                                           // used as alias for the User model!
            'foreignKey'  => 'editor_id',
        ),
    );
}

创建CakePHP约定有几个原因;保持一致性并减少使应用程序工作所需的配置量

但是,CakePHP并不禁止您使用不符合约定的命名。在某些情况下,您将不得不忽略这些约定—想想将用于CakePHP项目的第三方数据库,该项目不遵循CakePHP约定,您的情况就是其中之一

“同一”模型的多个关系 Cake允许您为每个关系的模型设置不同的别名。通过这种方式,您可以实现您在问题中描述的内容,而不会产生冲突。但是,由于thd second relation的外键不会遵循CakePHP约定,CakePHP不会自动选择正确的列,因此您必须自己指定

比如说

class Post extends AppModel {

    public $belongsTo = array(
        // CakePHP will automatically use 'user_id' as foreign key
        'User',

        // Here we create an 'alias' for the User Model
        // also, we need to specify the foreign key to use
        // for the relation
        'Editor' => array(
            'className'   => 'User',       // name of the Model
            'foreignKey'  => 'editor_id',  // foreign key for this relation
        ),
    );
}
class Post extends AppModel {
    public $belongsTo = array(
       'Editor' => array(
            'className'   => 'User',
            'foreignKey'  => 'editor_id',
        ),
    );
}

class Comment extends AppModel {
    public $belongsTo = array(
       'Editor' => array(
            'className'   => 'Visitor',    // WRONG!! Editor alias is already 
                                           // used as alias for the User model!
            'foreignKey'  => 'editor_id',
        ),
    );
}
Post模型现在与用户模型有两种关系。一个有别名编辑器的 别名的工作方式与“常规”模型相同;i、 就像有一个编辑模型附在帖子上

在控制器中,两个查找都将返回所有用户,并实际使用“用户”模型:

$this->Post->User->find('all');
$this->Post->Editor->find('all');
重要的 CakePHP使用别名来缓存模型信息。如果对一个模型使用别名,则决不能对另一个模型使用相同的别名。CakePHP可能会在这些情况下使用其他模型的缓存信息,从而导致不可预测的结果

比如说

class Post extends AppModel {

    public $belongsTo = array(
        // CakePHP will automatically use 'user_id' as foreign key
        'User',

        // Here we create an 'alias' for the User Model
        // also, we need to specify the foreign key to use
        // for the relation
        'Editor' => array(
            'className'   => 'User',       // name of the Model
            'foreignKey'  => 'editor_id',  // foreign key for this relation
        ),
    );
}
class Post extends AppModel {
    public $belongsTo = array(
       'Editor' => array(
            'className'   => 'User',
            'foreignKey'  => 'editor_id',
        ),
    );
}

class Comment extends AppModel {
    public $belongsTo = array(
       'Editor' => array(
            'className'   => 'Visitor',    // WRONG!! Editor alias is already 
                                           // used as alias for the User model!
            'foreignKey'  => 'editor_id',
        ),
    );
}

很高兴我能帮忙。如果这解决了你的问题,请接受我的回答,标记你的问题“已回答”,很高兴我能帮助你。如果这解决了您的问题,请接受我的回答,将您的问题标记为“已回答”