Support to revert inversedBy/mappedBy in JoinColumn to add foreign key to "mapped" table (OneToOne association) #7505

Open
opened 2026-01-22 15:52:32 +01:00 by admin · 0 comments
Owner

Originally created by @WinterSilence on GitHub (May 21, 2025).

Feature Request

What

Support to revert inversedBy/mappedBy in JoinColumn to add foreign key to "mapped" table (OneToOne association).

Why

Real case: I have meta_data table related to products, categories and etc. with FK like as

FOREIGN KEY (`id`) REFERENCES `products` (`meta_data_id`) ON DELETE CASCADE

In this case, if I remove product/category, then also remove attached record in meta_data.
But I can't create same FK by Doctrine, because FK adds to products and I can't change this behavior.

How

Switch inversedBy/mappedBy to add foreign key to meta_data table, instead products:

#[ORM\Entity]
#[ORM\Table(name: 'products')]
class Product
{
    // now support only set inversedBy
    #[ORM\OneToOne(targetEntity: MetaData::class, mappedBy: 'product')]
    #[ORM\JoinColumn(name: 'meta_data_id')]
    private MetaData $metaData;
}

#[ORM\Entity]
#[ORM\Table(name: 'meta_data')]
class MetaData
{
    // now support only set mappedBy
    #[ORM\OneToOne(targetEntity: Product::class, inversedBy: 'metaData')]
    private Product $product;
}
Originally created by @WinterSilence on GitHub (May 21, 2025). ### Feature Request #### What Support to revert inversedBy/mappedBy in JoinColumn to add foreign key to "mapped" table (OneToOne association). #### Why Real case: I have `meta_data` table related to `products`, `categories` and etc. with FK like as ```sql FOREIGN KEY (`id`) REFERENCES `products` (`meta_data_id`) ON DELETE CASCADE ``` In this case, if I remove product/category, then also remove attached record in `meta_data`. But I can't create same FK by Doctrine, because FK adds to `products` and I can't change this behavior. #### How Switch inversedBy/mappedBy to add foreign key to `meta_data` table, instead `products`: ```php #[ORM\Entity] #[ORM\Table(name: 'products')] class Product { // now support only set inversedBy #[ORM\OneToOne(targetEntity: MetaData::class, mappedBy: 'product')] #[ORM\JoinColumn(name: 'meta_data_id')] private MetaData $metaData; } #[ORM\Entity] #[ORM\Table(name: 'meta_data')] class MetaData { // now support only set mappedBy #[ORM\OneToOne(targetEntity: Product::class, inversedBy: 'metaData')] private Product $product; } ```
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: doctrine/archived-orm#7505