Question

I have a problem with doctrine entity mapping. Have a basic user entity and I want to create a Friend entity, what made up from two foreign key to the user table, the first owner of the friendship and the other. I tried to do it in this way ( yaml ), the entities are generated the correct way, but when I create the DB with doctrine scheme update, it has a totally different result.

The user entity:

..\..\Entity\User:
type:  entity
uniqueConstraints:
    usernameCanonical:
        columns: usernameCanonical
    emailCanonical:
        columns: emailCanonical
oneToMany:
    owners:
        targetEntity: Friend
        mappedBy: owner
    friends:
        targetEntity: Friend
        mappedBy: friend

And the friend entity:

..\..\Entity\Friend:
type: entity
table: friend
lifecycleCallbacks:
    prePersist: [setCreated, setModified]
    preUpdate: [setModified]
manyToOne:
    owner:
        targetEntity: User
        inversedBy: friends
        joinColumn:
            name: user
            referencedColumnName: id
            onDelete: cascade
    friend:
        targetEntity: User
        inversedBy: owners
        joinColumn:
            name: user
            referencedColumnName: id
            onDelete: cascade

This should create two foreign to from friend to user, but instead it's create one like:

ALTER TABLE friend ADD CONSTRAINT FK_55EEAC618D93D649 FOREIGN KEY (user) REFERENCES users (id) ON DELETE CASCADEe
Was it helpful?

Solution

Common "Friends" relationship is ManyToMany SelfReferencing doctrine relationship and it's used as an example in the doctrine documentation

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top