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列表的用户。连接表有自己的属性,如添加日期、删除日期等。我将有像createList(list),createUser(user),list.addUserToList(user),list.removeUserFromList(user)这样的操作。在删除用户时,我也希望他删除表单list_user_tb。在删除列表中,我希望删除仅属于该列表的所有用户。很明显,list_tb和Users_tb有一对多的关系要list_user_tb,
我不确定list_user_tb是否应该与用户和列表建立多对一的关系?这只是天气问题,我必须在 php 中添加和删除句柄,否则 mysql 将为我处理这个问题。
我的问题是关于教义yaml中正确的映射是什么?当我使用原则时orm:convert-mapping yaml ./config/yaml --from-database --force
它只创建连接许多ToMany关系,但正如我所说,我想要具有附加属性的连接表。所以我删除了一个外键并创建了映射,然后将其手动添加到 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
这是我到目前为止所拥有的。
//用户Tb
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: { }
这是答案,在此链接上得到了很好的解释 http://codemonkeys.be/2013/02/example-of-a-doctrine-2-x-many-to-many-association-class/#respond