2012-06-18 28 views
2

我的岗位模型的schema.yml(Symfony的1.4)学说/ MySQL的关系是这样的:与多列PK台

Post: 
    columns: 
    id: { type: bigint, notnull: true, primary: true } 
    blog_id: { type: bigint, notnull: true, primary: true } 
    user_id: { type: bigint, notnull: true } 
    subject: { type: string(255), notnull: true } 
    short_body: { type: text, notnull: true } 
    long_body: { type: text } 

,你可以看到张贴有一个多列PK。我的问题是“我如何与这个模型创建n:1关系?”

举个例子,我想是这样的:

PostComment: 
    columns: 
    post_id: { type: bigint, notnull: true } 
    blog_id: { type: bigint, notnull: true } 
    name: { type: string(255), notnull: true } 
    email: { type: string(255) } 
    text: { type: text, notnull: true } 
    relations: 
    Post: 
     #Here is my problem. What should I write here? 
     local: ???? 
     foreign: ???? 
     foreignAlias: Comments 
     onDelete: cascade 
     onUpdate: cascade 

我如何处理这种关系?

+0

我不熟悉这种表示法,但您需要一对多的表示法。另外postcomment应该有它的ID我猜! – GorillaApe

回答

1

你不能。 您可以将另一个主键添加到Post模型,并将另外几个字段保持为唯一。如果您这样做,我强烈建议您将“id”作为主键,并使用另一个字段名称作为唯一字符以及“blog_id”。 然后,像通常一样使用PostComment中的关系。

+0

感谢您的回复。但是另一个问题是,“我如何在yml语法中创建多列唯一约束?” –

+1

参见http://www.symfony-project.org/gentle-introduction/1_4/en/08-Inside-the-Model-Layer-Doctrine#chapter_08_sub_indexes –