2012-01-17 8 views
1

yamlには2つのテーブルがあります。例えばyamlで制約インデックスを追加する方法Doctrine 2

:USER_IDとchild_id:

 
Entities\User: 
    type: entity 
    table: users 
    id: 
    id: 
     type: integer 
     generator: 
     strategy: AUTO 
    fields: 
    username: 
     type: string 
     length: 64 
    oneToMany: 
    children: 
     targetEntity: UserToUser 
     mappedBy: parent 
    parents: 
    targetEntity: UserToUser 
    mappedBy: child 

Entities\UserToUser: 
    type: entity 
    table: user_to_user 
    id: 
    id: 
     type: integer 
     generator: 
     strategy: AUTO 
    fields: 
    user_id: 
     type: integer 
     nullable: false 
    child_id: 
     type: integer 
     nullable: false 
    manyToOne: 
    parent: 
     targetEntity: User 
     inversedBy: children 
     joinColumn: 
     name: user_id 
     referencedColumnName: id 
    child: 
     targetEntity: User 
     inversedBy: parents 
     joinColumn: 
     name: child_id 
     referencedColumnName: id 

この場合には、すべてがフィールドの無いユニークなインデックスであり、テーブルuser_to_userにデータベースにも実際に生成します。 同じ値を持つ2つのエントリを追加する可能性があります。

私は制約

 
uniqueConstraints: 
    child_user_idx: 
     columns: child_id,user_id 

または2他の方法を追加しようとしていた。これらのオプションを組み合わせるしよう

 
id: 
    user_id: 
     type: integer 
    child_id: 
     type: integer 

または

 
    id: 
    parent: 
     associationKey: true 
    child: 
     associationKey: true 

をが、結果としてそこ教義コンソールの検証を使用して毎回エラーが発生しましたが、生成されたSQLはまさに私が必要としていました。例えばそれらの

ワン:

彼は協会「親」の列に加わるが不足している「child_id」は、ソースエンティティのエンティティ\ UserToUser「すべての識別子列に一致させる必要があります。 *関連 '子'の結合列は、ソースエンティティ 'Entities \ UserToUser'のすべての識別子列に一致する必要がありますが、 'user_id'はありません。

私は本当に私がそう検証を追加しなければならないものを理解していないが正しく

+0

あなたはそのマッピングで何を達成しようとしていますか?各ユーザーにユーザーの集まりが必要ですか? 「親」ユーザーである属性と – jere

+0

yamlを使用して2つの外部キーにユニークなインデックスを追加するソリューションを見つけたい –

答えて

1

を渡す私は、これはあなたがhttps://gist.github.com/1845982

Vendor\BlogBundle\Entity\BlogImage: 
    type: entity 
    table: blog_image 

    # use two fields for our identifier 
    id: 
     blog: 
      # flag as an association key 
      associationKey: true 
     image: 
      associationKey: true 
    fields: 
     caption: 
      type: string 

    # Specify our relationships for above 
    manyToOne: 
     project: 
      targetEntity: Vendor\BlogBundle\Entity\Blog 
      inversedBy: images 

    oneToOne: 
     image: 
      targetEntity: Vendor\ImageBundle\Entity\Image 
1

探しているものをYAMLを使用しているときには、次を使用しようとすることができていると思います表記:

Vendor\CategoryBundle\Entity\Category: 
    type: entity 
    table: category 
    indexes: 
     # the name of the index 
     category_slug_idx: 
      # Columns is an array, specify multiple columns for 
      # a compound index 
      columns: [ slug ] 
    id: 
     id: 
      type: integer 
      generator: { strategy: AUTO } 

    fields: 
     name: 
      type: string 
     slug: 
      type: string 

あなたはインデックスの下にインデックスを宣言することができていることがわかります:ノード

+0

私は自分の答えを別の答えとして直接コピーしたようです(したがって、downvote –

+1

) "yaml doctrine 2でコンストレイントインデックスを追加する方法" – perrohunter

+0

私が正確に投稿したスキーマは、2つの外国語のインデックスを追加する方法を説明していますキー、コメント付き。あなたは、質問に間違ったスキーマをコピーして貼り付け、私のGistから直接取り出しました。 –

関連する問題