diff --git a/src/Entity/Project/Project.php b/src/Entity/Project/Project.php index d91b71a..2487053 100644 --- a/src/Entity/Project/Project.php +++ b/src/Entity/Project/Project.php @@ -9,11 +9,14 @@ use App\Entity\Trait\TimestampedCreationEntity; use App\Entity\Trait\TimestampedUpdationEntity; use App\Entity\User\User; +use App\Mapping\Provider\EntityMapProvider; use App\Repository\Project\ProjectRepository; +use AutoMapper\Attribute\MapProvider; use Doctrine\Common\Collections\ArrayCollection; use Doctrine\Common\Collections\Collection; use Doctrine\ORM\Mapping as ORM; +#[MapProvider(EntityMapProvider::class)] #[ORM\Entity(repositoryClass: ProjectRepository::class)] class Project implements UserOwnedInterface, AccountingOwnerInterface { diff --git a/src/Entity/Project/Reward.php b/src/Entity/Project/Reward.php index 7d9e4e1..eca3970 100644 --- a/src/Entity/Project/Reward.php +++ b/src/Entity/Project/Reward.php @@ -3,7 +3,9 @@ namespace App\Entity\Project; use App\Entity\Money; +use App\Mapping\Provider\EntityMapProvider; use App\Repository\Project\RewardRepository; +use AutoMapper\Attribute\MapProvider; use Doctrine\Common\Collections\ArrayCollection; use Doctrine\Common\Collections\Collection; use Doctrine\DBAL\Types\Types; @@ -12,6 +14,7 @@ /** * A ProjectReward is something the Project owner wishes to give in exchange for contributions to their Project. */ +#[MapProvider(EntityMapProvider::class)] #[ORM\Entity(repositoryClass: RewardRepository::class)] class Reward { diff --git a/src/Entity/User/User.php b/src/Entity/User/User.php index d913e88..a29fba4 100644 --- a/src/Entity/User/User.php +++ b/src/Entity/User/User.php @@ -8,7 +8,9 @@ use App\Entity\Trait\MigratedEntity; use App\Entity\Trait\TimestampedCreationEntity; use App\Entity\Trait\TimestampedUpdationEntity; +use App\Mapping\Provider\EntityMapProvider; use App\Repository\User\UserRepository; +use AutoMapper\Attribute\MapProvider; use Doctrine\Common\Collections\ArrayCollection; use Doctrine\Common\Collections\Collection; use Doctrine\ORM\Mapping as ORM; @@ -24,10 +26,11 @@ * This allows to keep an User's "wallet", withholding their non-raised fundings into their Accounting. */ #[Gedmo\Loggable()] -#[UniqueEntity(fields: ['username'], message: 'This usernames already exists.')] -#[UniqueEntity(fields: ['email'], message: 'This email address is already registered.')] +#[MapProvider(EntityMapProvider::class)] #[ORM\Entity(repositoryClass: UserRepository::class)] #[ORM\Index(fields: ['migratedId'])] +#[UniqueEntity(fields: ['username'], message: 'This usernames already exists.')] +#[UniqueEntity(fields: ['email'], message: 'This email address is already registered.')] class User implements UserInterface, PasswordAuthenticatedUserInterface, AccountingOwnerInterface { use MigratedEntity; @@ -68,7 +71,7 @@ class User implements UserInterface, PasswordAuthenticatedUserInterface, Account /** * The projects owned by this User. */ - #[ORM\OneToMany(mappedBy: 'owner', targetEntity: Project::class)] + #[ORM\OneToMany(mappedBy: 'owner', targetEntity: Project::class, cascade: ['persist'])] private Collection $projects; /** diff --git a/src/Mapping/AutoMapper.php b/src/Mapping/AutoMapper.php index 9e871b2..94053ca 100644 --- a/src/Mapping/AutoMapper.php +++ b/src/Mapping/AutoMapper.php @@ -15,7 +15,7 @@ public function __construct( iterable $mapProviders, ) { $this->innerMapper = InnerMapper::create( - providers: $mapProviders + providers: \iterator_to_array($mapProviders) ); } diff --git a/src/Mapping/Provider/EntityMapProvider.php b/src/Mapping/Provider/EntityMapProvider.php new file mode 100644 index 0000000..c2bce41 --- /dev/null +++ b/src/Mapping/Provider/EntityMapProvider.php @@ -0,0 +1,28 @@ +id)) { + return null; + } + + $repository = $this->entityManager->getRepository($targetType); + + if (!$repository) { + throw new \Exception(\sprintf("No repository found for '' class. Is it an Entity?", $targetType)); + } + + return $repository->find($source->id); + } +}