2016-05-21 6 views
0

私はsymfonyでdoctrineを使用しています。 YaMLを使用してデータベースエンティティを指定しました。私は、生成されたテーブルを検査したときに、私の恐怖に、しかし - - 私は私のYAMLファイルからデータベースを生成することができる午前ない一部エンティティは外部キー参照を失っているなどDoctrine ORMはYaML仕様で生成されたデータベースに外部キーを作成しません

Doctrineは黙って失敗しているように見える、何としてエラーが画面に表示され、doctrine:schema:update --forceタスクが実行され、正常に完了したことが報告されます。ここで

は、そのようなエンティティの例です:

ここ
AppBundle\Entity\User: 
    type: entity 
    table: user 
    repositoryClass: UserRepository 
    id: 
     id: 
      type: integer 
      generator: { strategy: AUTO } 

    manyToOne: 
     user_type: 
      targetEntity: UserType 
      joinColumn: 
       name: user_type_id 
       referencedColumnName: id 
       nullable: false 
       onDelete: RESTRICT 
       onUpdate: CASCADE 

    manyToOne: 
     user_title: 
      targetEntity: UserTitle 
      joinColumn: 
       name: user_type_id 
       referencedColumnName: id 
       nullable: false 
       onDelete: RESTRICT 
       onUpdate: CASCADE 

    oneToMany: 
     actions: 
      targetEntity: UserAction 
      mappedBy: User 

    oneToMany: 
     type_roles: 
      targetEntity: UserTypeRole 
      mappedBy: User 

    uniqueConstraints: 
     idxu_user_lname_eml: 
      columns: [last_name, user_email] 

    indexes: 
     idx_user_name: 
      columns: [last_name, first_name] 
     idx_user_email: 
      columns: user_email    


    fields: 
     first_name: 
      type: string 
      length: 32 
      nullable: false 
      unique: false 

     middle_name: 
      type: string 
      length: 32 
      nullable: true 
      unique: false 

     last_name: 
      type: string 
      length: 64 
      nullable: false 
      unique: false  

     email: 
      type: string 
      length: 32 
      column: user_email 
      unique: true 
      options: 
       fixed: true 
       comment: User's email address 

     address_line1: 
      type: string 
      length: 128 
      nullable: false 

     address_line2: 
      type: string 
      length: 128 
      nullable: true 

     address_line3: 
      type: string 
      length: 128 
      nullable: true 

     town_city: 
      type: string 
      length: 128 
      nullable: true 

     county_district: 
      type: string 
      length: 128 
      nullable: true 

     state_region: 
      type: string 
      length: 128 
      nullable: true 

     post_zipcode: 
      type: string 
      length: 12 
      nullable: true 

     country: 
      type: string 
      length: 128 
      nullable: true 

     login_count: 
      type: integer 
      nullable: false 
      options: 
       unsigned: true 
       default: 0 

     last_login: 
      type: datetime 
      nullable: true 

上、この仕様から生成されたテーブルである:

mysql> describe user; 
+-----------------+------------------+------+-----+---------+----------------+ 
| Field   | Type    | Null | Key | Default | Extra   | 
+-----------------+------------------+------+-----+---------+----------------+ 
| id    | int(11)   | NO | PRI | NULL | auto_increment | 
| user_type_id | int(11)   | NO | MUL | NULL |    | 
| first_name  | varchar(32)  | NO |  | NULL |    | 
| middle_name  | varchar(32)  | YES |  | NULL |    | 
| last_name  | varchar(64)  | NO | MUL | NULL |    | 
| user_email  | char(32)   | NO | UNI | NULL |    | 
| address_line1 | varchar(128)  | NO |  | NULL |    | 
| address_line2 | varchar(128)  | YES |  | NULL |    | 
| address_line3 | varchar(128)  | YES |  | NULL |    | 
| town_city  | varchar(128)  | YES |  | NULL |    | 
| county_district | varchar(128)  | YES |  | NULL |    | 
| state_region | varchar(128)  | YES |  | NULL |    | 
| post_zipcode | varchar(12)  | YES |  | NULL |    | 
| country   | varchar(128)  | YES |  | NULL |    | 
| login_count  | int(10) unsigned | NO |  | 0  |    | 
| last_login  | datetime   | YES |  | NULL |    | 
+-----------------+------------------+------+-----+---------+----------------+ 
16 rows in set (0.07 sec) 

は、誰もが私が間違ってやっているかを見ることができ、それが防止されます外交関係は作られていないのか?

答えて

0

私はYaMLでエンティティを間違って指定していましたが、正しいYaML表現は次のようになります:

この問題に対する解決策は、後で知ることができます(おそらく、良い夜の睡眠)。
AppBundle\Entity\User: 
    type: entity 
    table: user 
    repositoryClass: UserRepository 
    id: 
     id: 
      type: integer 
      generator: { strategy: AUTO } 

    manyToOne: 
     user_type: 
      targetEntity: UserType 
      joinColumn: 
       name: user_type_id 
       referencedColumnName: id 
       nullable: false 
       onDelete: RESTRICT 
       onUpdate: CASCADE 

     user_title: 
      targetEntity: UserTitle 
      joinColumn: 
       name: user_title_id 
       referencedColumnName: id 
       nullable: false 
       onDelete: RESTRICT 
       onUpdate: CASCADE 

    oneToMany: 
     actions: 
      targetEntity: UserAction 
      mappedBy: User 

     roles: 
      targetEntity: UserTypeRole 
      mappedBy: User 

    uniqueConstraints: 
     idxu_user_lname_eml: 
      columns: [user_email] 

    indexes: 
     idx_user_name: 
      columns: [last_name, first_name]   


    fields: 
     first_name: 
      type: string 
      length: 32 
      nullable: false 
      unique: false 

     middle_name: 
      type: string 
      length: 32 
      nullable: true 
      unique: false 

     last_name: 
      type: string 
      length: 64 
      nullable: false 
      unique: false  

     email: 
      type: string 
      length: 32 
      column: user_email 
      unique: true 
      options: 
       fixed: true 
       comment: User's email address 

     address_line1: 
      type: string 
      length: 128 
      nullable: false 

     address_line2: 
      type: string 
      length: 128 
      nullable: true 

     address_line3: 
      type: string 
      length: 128 
      nullable: true 

     town_city: 
      type: string 
      length: 128 
      nullable: true 

     county_district: 
      type: string 
      length: 128 
      nullable: true 

     state_region: 
      type: string 
      length: 128 
      nullable: true 

     post_zipcode: 
      type: string 
      length: 12 
      nullable: true 

     country: 
      type: string 
      length: 128 
      nullable: true 

     login_count: 
      type: integer 
      nullable: false 
      options: 
       unsigned: true 
       default: 0 

     last_login: 
      type: datetime 
      nullable: true 
0

このセクション:

manyToOne: 
    user_type: 
     targetEntity: UserType 
     joinColumn: 
      name: user_type_id 
      referencedColumnName: id 
      nullable: false 
      onDelete: RESTRICT 
      onUpdate: CASCADE 

manyToOne: 
    user_title: 
     targetEntity: UserTitle 
     joinColumn: 
      name: user_type_id 
      referencedColumnName: id 
      nullable: false 
      onDelete: RESTRICT 
      onUpdate: CASCADE 

oneToMany: 
    actions: 
     targetEntity: UserAction 
     mappedBy: User 

oneToMany: 
    type_roles: 
     targetEntity: UserTypeRole 
     mappedBy: User 

はそのようになります。YAMLで

manyToOne: 
    user_type: 
     targetEntity: UserType 
     joinColumn: 
      name: user_type_id 
      referencedColumnName: id 
      nullable: false 
      onDelete: RESTRICT 
      onUpdate: CASCADE 
    user_title: 
     targetEntity: UserTitle 
     joinColumn: 
      name: user_type_id 
      referencedColumnName: id 
      nullable: false 
      onDelete: RESTRICT 
      onUpdate: CASCADE 

oneToMany: 
    actions: 
     targetEntity: UserAction 
     mappedBy: User 
    type_roles: 
     targetEntity: UserTypeRole 
     mappedBy: User 

、複数のキーが同じ名前で定義されている、それだけでそのキーの前の値を上書きします。

+0

あなたの回答には推奨された変更が含まれていません(異なるキーに異なる名前を使用するなど)。いずれにしても、修正提案があっても、FKは正しく生成されません。 –

+0

PHPエンティティで、関係が正しく実装されていることを確認してください。 Doctrineは、所有側(アソシエーションが 'inversedBy'で定義されているエンティティ)でのみアソシエーションをチェックします。リレーションの逆の側からアソシエーションを作成する場合は、アソシエーションが所有側で作成されていることを確認する必要があります。例えば、 'User'クラスの中で、' public function addAction(UserAction $ userAction){$ this-> actions [] = $ userAction; $ userAction-> setUser($ this); } ' – palra

関連する問題