php - 使用属性 yaml 映射连接表 - Doctrine

标签 php mysql doctrine-orm

CREATE TABLE IF NOT EXISTS list_tb (
    list_id               VARCHAR(255) PRIMARY KEY, 
    country               VARCHAR(255) NOT NULL
);

CREATE TABLE IF NOT EXISTS users_tb (           
    id_str          VARCHAR(255) PRIMARY KEY,             
    name            VARCHAR(255) NOT NULL,
    statuses_count  INT(10)      NOT NULL   
);

CREATE TABLE IF NOT EXISTS list_user_tb (
    list_id         VARCHAR(255),
    user_id         VARCHAR(255),
    priority        INT(10)  NOT NULL,
    date_added      DATETIME NOT NULL,
    date_removed    DATETIME NOT NULL
    PRIMARY KEY (list_id, user_id)
);

ALTER TABLE list_user_tb 
    ADD CONSTRAINT fk_list_user_tb_list_tb FOREIGN KEY (list_id) REFERENCES list_tb(list_id);

ALTER TABLE list_user_tb 
    ADD CONSTRAINT fk_list_user_tb_users_tb FOREIGN KEY (user_id) REFERENCES users_tb(id_str);

我有一个列表,可以包含 N 个用户和可以属于 M 个列表的用户。连接表有它自己的属性,如 dateAdded、dateRemoved 等。我将有像 createList(list)、createUser(user)、list.addUserToList(user)、list.removeUserFromList(user) 这样的操作。在删除用户时,我希望他也从 list_user_tb 中删除。在 deleteList 上,我想删除仅属于该列表的所有用户。很明显,list_tb 和 Users_tb 与 list_user_tb 是一对多的关系,

我不确定 list_user_tb 是否应该与用户和列表建立多对一关系?这只是我必须在 php 或 mysql 中添加和删除句柄的问题天气会为我处理。

我的问题是关于 Doctrine yaml 中的正确映射是什么?当我使用 doctrine orm:convert-mapping yaml ./config/yaml --from-database --force 它只创建连接 manyToMany 关系,但正如我所说,我想要连接具有附加属性的表。所以我删除了一个外键并创建了映射,然后将其手动添加到 yaml 中。

这两篇文章我也看了

http://future500.nl/articles/2013/09/doctrine-2-how-to-handle-join-tables-with-extra-columns/

http://doctrine-orm.readthedocs.org/en/latest/reference/association-mapping.html

这是我目前所拥有的。

//用户数

UsersTb:
    type: entity
    table: users_tb
    id:
        idStr:
            type: string
            nullable: false
            length: 255
            fixed: false
            default: ''
            id: true
            column: id_str
            generator:
                strategy: IDENTITY
    fields:
        name:
            type: string
            nullable: false
            length: 255
            fixed: false
        statusesCount:
            type: integer
            nullable: false
            unsigned: false
            column: statuses_count
    lifecycleCallbacks: {  }

//ListTb.yml

ListTb:
    type: entity
    table: list_tb
    id:
        listId:
            type: string
            nullable: false
            length: 255
            fixed: false
            default: ''
            id: true
            column: list_id
            generator:
                strategy: IDENTITY
    fields:
        country:
            type: string
            nullable: false
            length: 255
            fixed: false
    lifecycleCallbacks: {  }

//ListUserTb.yml

ListUserTb:
    type: entity
    table: list_user_tb
    indexes:
        IDX_6D2EC7B13DAE168B:
            columns:
                - list_id
    id:
        userId:
            type: string
            nullable: false
            length: 255
            fixed: false
            id: true
            column: user_id
    id:
        idStr:
            type: string
            nullable: false
            length: 255
            fixed: false
            id: true
            column: id_str
            generator:
                strategy: IDENTITY
    fields:
        priority:
            type: integer
            nullable: false
            unsigned: false
        dateAdded:
            type: datetime
            nullable: false
            column: date_added
        dateRemoved:
            type: datetime
            nullable: false
            column: date_removed
    manyToOne:
        list:
            targetEntity: ListTb
            cascade: {  }
            mappedBy: null
            inversedBy: null
            joinColumns:
                list_id:
                    referencedColumnName: list_id
            orphanRemoval: false
        user:
            targetEntity: UsersTb
            cascade: {  }
            mappedBy: null
            inversedBy: null
            joinColumns:
                id_str:
                    referencedColumnName: id_str
            orphanRemoval: false
    lifecycleCallbacks: {  }

最佳答案

关于php - 使用属性 yaml 映射连接表 - Doctrine,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27675196/

相关文章:

php - 在 RLIKE 正则表达式中使用 PHP 变量

php - 如何用ajax查看表单上传的文件?

php - Mysql 从 1 个查询中选择第二行

orm - 奇怪的错误 : [Semantical Error] line 0, col 75 靠近 'submit' :错误: 'submit' 未定义

php - 在 WHERE 子句中使用 mysql concat()?

php-mysql count() 查询重复结果

mysql - 条目中以逗号分隔的循环子字符串

php - SF2 中的 INNER JOIN 与 Doctrine "createQueryBuilder"

php - Doctrine2,传递 Id 还是对象?

javascript - 如何使用 AJAX 将 DropDown 保存到数据库中