Skip to content

Instantly share code, notes, and snippets.

@carnage
Created July 30, 2022 16:56
Show Gist options
  • Save carnage/7b72baab2ffac02634a98d4ff7b8feb3 to your computer and use it in GitHub Desktop.
Save carnage/7b72baab2ffac02634a98d4ff7b8feb3 to your computer and use it in GitHub Desktop.
Custom collection in Doctrine
<?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