Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: [Symfony] Problem z relacją Many to Many - Doctrine
Forum PHP.pl > Forum > PHP > Frameworki
Halva
Mam problem z relacją Many to Many w Doctrine w moim projekcie opartym na Symfony.
Mam dwie tabele, placeCompany oraz placeCategories, chcę aby dodawana firma (placeCompany) mogła być przypisana do wielu kategorii (placeCategories) na raz.

Mój schema:
Kod
placeCategories:
  actAs:
    Timestampable: ~
    Sluggable:
      fields: [name]
  columns:
    id:   { type: integer(4), primary: true, autoincrement: true, notnull: true }
    name: { type: string(255), notnull: true, unique: true }
  options:
    collate: utf8_polish_ci
    charset: utf8
    
placeCompany:
  actAs:
    Timestampable: ~
    Sluggable:
      fields: [name]
  columns:
    id:                 { type: integer(4), primary: true, autoincrement: true, notnull: true }
    name:               { type: string(255), notnull: true, unique: false }
    description:        { type: string(255), notnull: true, unique: false }
    phone:              { type: string(12), notnull: true, unique: false }
    fax:                { type: string(12), notnull: true, unique: false }
    email:              { type: string(255), notnull: true, unique: false }
    street:             { type: string(255), notnull: true, unique: false }
    building:           { type: string(255), notnull: true, unique: false }
    city:               { type: string(255), notnull: true, unique: false }
    postcode:           { type: string(6), notnull: true, unique: false }
    is_promoted:        { type: boolean, notnull: true, default: 0 }
    promote_expires_at: { type: timestamp, notnull: false }
    is_public:          { type: boolean, notnull: true, default: 1 }
    is_activated:       { type: boolean, notnull: true, default: 0 }
    token:              { type: string(255), notnull: false, unique: true }
    rating:             { type: decimal, notnull: true, default: 0 }
    views:              { type: integer, notnull: true, default: 0 }
    logo:               { type: string(255) }
    www:                { type: string(255) }
  options:
    collate: utf8_polish_ci
    charset: utf8
  relations:
    placeCategories:
      class: placeCategories
      refClass: placeCompanyCategories
      foreignAlias: placeCompanies
      local: company_id
      foreign: company_id

placeCompanyCategories:
  columns:
    company_id: { type: integer(4), primary: false, notnull: true }
    category_id: { type: integer(4), primary: false, notnull: false }
  relations:
    placeCompany: { class: placeCompany, local: company_id, foreign: id, onDelete: CASCADE }
    placeCategories: { class: placeCategories, local: category_id, foreign: id, onDelete: CASCADE }
  options:
    collate: utf8_polish_ci
    charset: utf8


Kiedy dodaję rekord w placeCompany z zaznaczeniem placeCategories otrzymuję komunikat błędu:

Kod
SQLSTATE[HY000]: General error: 1452 Cannot add or update a child row: a foreign key constraint fails (`baza58809_83`.`place_company_categories`, CONSTRAINT `place_company_categories_ibfk_1` FOREIGN KEY (`company_id`) REFERENCES `place_company` (`id`) ON DELETE CASCADE)


Stack trace:
Kod
at ()
in SF_ROOT_DIR\lib\symfony\plugins\sfDoctrinePlugin\lib\vendor\doctrine\Doctrine\Connection.php line 1086 ...
at Doctrine_Connection->rethrowException(object('PDOException'), object('Doctrine_Connection_Statement'))
in SF_ROOT_DIR\lib\symfony\plugins\sfDoctrinePlugin\lib\vendor\doctrine\Doctrine\Connection\Statement.php line 253 ...
at Doctrine_Connection_Statement->execute(array(2))
in SF_ROOT_DIR\lib\symfony\plugins\sfDoctrinePlugin\lib\vendor\doctrine\Doctrine\Connection.php line 1051 ...
at Doctrine_Connection->exec('INSERT INTO place_company_categories (company_id) VALUES (?)', array(2))
in SF_ROOT_DIR\lib\symfony\plugins\sfDoctrinePlugin\lib\vendor\doctrine\Doctrine\Connection.php line 684 ...
at Doctrine_Connection->insert(object('placeCompanyCategoriesTable'), array('company_id' => 2))
in SF_ROOT_DIR\lib\symfony\plugins\sfDoctrinePlugin\lib\vendor\doctrine\Doctrine\Connection\UnitOfWork.php line 616 ...
at Doctrine_Connection_UnitOfWork->processSingleInsert(object('placeCompanyCategories'))
in SF_ROOT_DIR\lib\symfony\plugins\sfDoctrinePlugin\lib\vendor\doctrine\Doctrine\Connection\UnitOfWork.php line 575 ...


Proszę o pomoc bo męczę się z tym 3 dni i nie mogę sobie poradzić.
empathon
Polecam czytać dokumentację: http://www.doctrine-project.org/projects/o...ns:many-to-many

Kod
User:
# ...
  relations:
    # ...
    Groups:
      class: Group
      local: user_id
      foreign: group_id
      refClass: UserGroup

Group:
  tableName: groups
  columns:
    name: string(30)
  relations:
    Users:
      class: User
      local: group_id
      foreign: user_id
      refClass: UserGroup

UserGroup:
  columns:
    user_id:
      type: integer
      primary: true
    group_id:
      type: integer
      primary: true
Halva
Dzięki za pomoc, pomogło smile.gif Działa biggrin.gif
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.