У меня есть следующие schema.ymlНепризнанная таблица получения создана, когда доктрина создает базу данных
Proposition:
actAs: { Timestampable: ~ }
columns:
name: { type: string(255), notnull: true }
slug: { type: string(255), notnull: true, unique: true }
proposition_type_id: { type: integer, notnull: true }
icon: { type: string(255) }
overview: { type: string(4000) }
features: { type: string(4000) }
benefits: { type: string(4000) }
published: { type: boolean, notnull: true, default: 1 }
relations:
PropositionType: { onDelete: CASCADE, local: proposition_type_id, foreign: id }
Products:
class: Product
refClass: PropositionProduct
local: proposition_id
foreign: product_id
foreignAlias: PropositionProducts
PropositionType:
columns:
name: { type: string(255), notnull: true }
Review:
actAs: { Timestampable: ~ }
columns:
proposition_id: { type: integer, notnull: true }
review: { type: string(4000), notnull: true }
name: { type: string(255), notnull: true }
company: { type: string(255) }
published: { type: boolean, notnull: true, default: 1 }
relations:
Proposition: { onDelete: CASCADE, local: proposition_id, foreign: id }
PropositionProduct:
columns:
proposition_id: { type: integer, primary: true }
product_id: { type: integer, primary: true }
relations:
Proposition: { onDelete: CASCADE, local: proposition_id, foreign: id }
Product: { onDelete: CASCADE, local: product_id, foreign: id }
Product:
actAs: { Timestampable: ~ }
columns:
name: { type: string(255), notnull: true }
slug: { type: string(255), notnull: true, unique: true }
icon: { type: string(255) }
ataglance: { type: string(4000) }
idealfor: { type: string(4000) }
details: { type: string(4000) }
specsheet: { type: string(255) }
chart: { type: string(255) }
published: { type: boolean, notnull: true, default: 1 }
relations:
RelatedProducts:
class: Product
refClass: RelatedProduct
local: product_id
foreign: related_product_id
foreignAlias: RelatedProducts
RelatedProduct:
columns:
product_id: { type: integer, primary: true }
related_product_id: { type: integer, primary: true }
relations:
Product: { onDelete: CASCADE, local: product_id, foreign: id }
Product: { onDelete: CASCADE, local: related_product_id, foreign: id }
Segment:
actAs: { Timestampable: ~ }
columns:
name: { type: string(255), notnull: true }
slug: { type: string(255), notnull: true, unique: true }
published: { type: boolean, notnull: true, default: 1 }
relations:
Products:
class: Product
refClass: SegmentProduct
local: segment_id
foreign: product_id
foreignAlias: SegmentProducts
SegmentProduct:
columns:
segment_id: { type: integer, primary: true }
product_id: { type: integer, primary: true }
relations:
Segment: { onDelete: CASCADE, local: segment_id, foreign: id }
Product: { onDelete: CASCADE, local: product_id, foreign: id }
Я Ран:
php symfony doctrine:build --all --and-load --no-confirmation
и база данных успешно построены.
Но почему создана таблица proposition_segment
?
CREATE TABLE `proposition_segment` (
`segment_id` bigint(20) NOT NULL DEFAULT '0',
`product_id` bigint(20) NOT NULL DEFAULT '0',
PRIMARY KEY (`segment_id`,`product_id`),
KEY `proposition_segment_product_id_product_id` (`product_id`),
CONSTRAINT `proposition_segment_product_id_product_id` FOREIGN KEY (`product_id`) REFERENCES `product` (`id`) ON DELETE CASCADE,
CONSTRAINT `proposition_segment_segment_id_segment_id` FOREIGN KEY (`segment_id`) REFERENCES `segment` (`id`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
В моем понимании моя схема должна подробно, что Segment
и Product
имеют отношения многие ко многим через SegmentProduct
таблице.
Аналогичным образом, Proposition
и Product
имеют отношение много к большому количеству через таблицу PropositionProduct
.
Я не могу понять, почему Doctrine создает таблицу proposition_segment
. Кроме этого, база данных выглядит корректно - она создает таблицы proposition_product
и segment_product
, как и ожидалось.
Когда я добавляю данные через встроенный сервер администратора Symfony, таблица proposition_segment
остается пустой, увеличивая мое подозрение, что она создана с ошибкой.
Это, вероятно, не имеет ничего общего с вашей проблемой, но я думаю, что foreignAlias, который вы использовали для отношения «Продукты» вашего класса «Сегмент», следует называть «Сегменты», потому что это создаст метод 'getSegments() который будет возвращать объекты сегмента, а не объект SegmentProducts. Выбирайте свои имена с умом, потому что Doctrine использует их для вывода вещей, которые не отображаются в вашей схеме, и иногда это может вызвать странные вещи. – greg0ire
Другое дело: у вас, похоже, отношение Equal Nest в вашей схеме: relatedProducts Вот как это должно быть объявлено: http://www.doctrine-project.org/projects/orm/1.2/docs/manual/defining-models % 3Arelationships% 3Ajoin-table-association% 3Aself-referencing-nest-relations% 3Aequal-nest-relations/zh (я не знаю, было ли то, что вы сделали, или не имеет серьезных последствий) – greg0ire