Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: [Symfony] doctriny many-to-many
Forum PHP.pl > Forum > PHP > Frameworki
janek9
hej

Mam problem z relacja wiele do wielu i nie mam pojecia gdzie mam blad.

Oto moj schema.yml

  1.  
  2. pp_content:
  3. actAs:
  4. Timestampable: ~
  5. columns:
  6. id:
  7. type: integer(4)
  8. primary: true
  9. autoincrement: true
  10. notnull: true
  11. category_id:
  12. type: integer(4)
  13. notnull: true
  14. primary: false
  15. autoincrement: false
  16. title:
  17. type: string(255)
  18. description:
  19. type: string()
  20. type: string(255)
  21. is_visible:
  22. type: boolean
  23. default: 1
  24. notnull: true
  25. relations:
  26. images:
  27. class: pp_image
  28. local: image_id
  29. foreign: content_id
  30. refClass: pp_content_image
  31. foreignAlias: Contents
  32.  
  33. pp_image:
  34. actAs: [Timestampable]
  35. columns:
  36. id:
  37. type: integer(4)
  38. primary: true
  39. autoincrement: true
  40. notnull: true
  41. type: string(255)
  42. is_active:
  43. type: boolean
  44. default: 1
  45. notnull: true
  46. indexes:
  47. is_active_idx:
  48. fields: [is_active]
  49. relations:
  50. contents:
  51. class: pp_content
  52. refClass: pp_content_image
  53. local: content_id
  54. foreign: image_id
  55. foreignAlias: Images
  56.  
  57. pp_content_image:
  58. options:
  59. symfony:
  60. form: true
  61. filter: true
  62. actAs: [Timestampable]
  63. columns:
  64. content_id:
  65. type: integer(4)
  66. notnull: true
  67. primary: true
  68. image_id:
  69. type: integer(4)
  70. notnull: true
  71. primary: true
  72. relations:
  73. pp_content:
  74. local: content_id
  75. onDelete: CASCADE
  76. pp_image:
  77. local: image_id
  78. onDelete: CASCADE
  79.  



problem tego rodzaju, ze jezeli chce dodac w trzeciej tabeli inny obrazek o innym id do tego samego klucza z tabeli pp_content to wyrzuca blad ze powtarzaja sie klucze...

  1.  
  2. SQLSTATE[HY000]: General error: 1452 Cannot add or update a child row: a foreign key constraint fails (`pp`.`pp_content_image`, CONSTRAINT `pp_content_image_content_id_pp_content_id` FOREIGN KEY (`content_id`) REFERENCES `pp_content` (`id`) ON DELETE CASCADE)
  3.  



Pytanie do Was: jak to naprawic?
Co chce osiagnac?: zeby do takich samych kluczy z pp_content mozna bylo przypisywac inne klucze z pp_image..
Gribo
wydaje mi się że Propel wymaga kolumny id nawet jeśli jej nie potrzebujesz spróbuj dodać w tabeli pp_content_image kolumne id z wartościami unikatowymi. Być możne Doctrine ma to samo
Crozin
Cytat
wydaje mi się że Propel wymaga kolumny id nawet jeśli jej nie potrzebujesz spróbuj dodać w tabeli pp_content_image kolumne id z wartościami unikatowymi.
O ile pamiętam Propel wymagał po prostu klucza głównego - ale nie musiał to być klasyczny ID. Podobnie jest z Doctrine.

Co do błędów:
1) Do tworzenia prefiksów skorzystaj z atrybutu TABLE_NAME_FORMAT (czy jakoś tak) - pp_%s ustawianego dla menadżera Doctrine (Doctrine_Manager)
2) Jeżeli masz relację n-n (korzystająca z dodatkowej tabeli) to local oznacza nazwę kolumny z ID obecnie przetwarzanej tabeli, a foreign oznacza kolumnę z ID dołączanej tabeli
3) Nie ma potrzeby definiowania relacji w dwóch miejscach - skorzystaj z foreignAlias
4)
Kod
Content:
  columns:
    id:            { type: integer(4), primary: true, autoincrement: true, notnull: true }
    category_id:   { type: integer(4), notnull: true }
    title:         { type: string(255) }
    description:   { type: string(255) }
    file:          { type: string(255) }
    is_visible:    { type: boolean, default: true, notnull: true }
  actAs:
    Timestampable: ~
  relations:
    Images:        { class: Image, refClass: ContentImageRef, foreignAlias: Contents, local: content_id, foreign: image_id }

Image:
  columns:
    id:            { type: integer(4), primary: true, autoincrement: true, notnull: true }
    file:          { type: string(255) }
    is_active:     { type: boolean, default: true, notnull: true }
  indexes:
    is_active:     { fields: [ is_active ] }
    
ContentImageRef:
  columns:
    content_id:    { type: integer(4), notnull: true }
    image_id:      { type: integer(4), notnull: true }
  relations:
    Content:       { class: Content, local: content_id, onDelete: CASCADE }
    Image:         { class: Image, local: image_id, onDelete: CASCADE }
To jest wersja lo-fi głównej zawartości. Aby zobaczyć pełną wersję z większą zawartością, obrazkami i formatowaniem proszę kliknij tutaj.
Invision Power Board © 2001-2025 Invision Power Services, Inc.