Created
July 30, 2022 16:56
-
-
Save carnage/7b72baab2ffac02634a98d4ff7b8feb3 to your computer and use it in GitHub Desktop.
Custom collection in Doctrine
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* This is a bit messy but seems to work | |
*/ | |
#[Entity] | |
class ParentEntity | |
{ | |
#[Column] | |
#[Id] | |
private string $id; | |
#[OneToOne(mappedBy: 'parentEntity', cascade:['all'])] | |
private CustomCollection $customCollection | |
} | |
#[Entity] | |
class CustomCollection | |
{ | |
#[Id] | |
#[OneToOne] | |
#[JoinColumn('parent_entity_id', referencedColumnName: 'id')] | |
private ParentEntity $parentEntity; | |
#[ManyToMany(targetEntity: CollectionItem::class, cascade: ['all'])] | |
#[JoinTable] | |
#[JoinColumn(name:'parent_entity_id', referencedColumnName: 'parent_entity_id')] | |
#[InverseJoinColumn(name:'id', referencedColumnName: 'id')] | |
#[InverseJoinColumn(name:'f_parent_entity_id', referencedColumnName: 'parent_entity_id')] | |
private Collection $items; | |
} | |
#[Entity] | |
class CollectionItem | |
{ | |
#[Column] | |
#[Id] | |
private string $id; | |
#[Id] | |
#[ManyToOne] | |
#[JoinColumn(name:'parent_entity_id', referencedColumnName: 'id')] | |
private ParentEntity $parentEntity; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment