2

Hi i have this setup: House N-1 User N-1 Group N-N Role

Domain\Entity\House:
    manyToOne:
        user:
            targetEntity: Domain\Entity\User
            cascade: ["persist"]
            inversedBy: houses
            joinColumn:
                name: user_id
                referencedColumnName: id
                onDelete: CASCADE
    genre:
        targetEntity: Domain\Entity\Genre
        cascade: ["persist"]
        inversedBy: houses
        joinColumn:
            name: genre_id
            referencedColumnName: id
            onDelete: SET NULL

Domain\Entity\User:
  oneToMany:
    houses:
      targetEntity: Domain\Entity\House
      cascade: ["persist"]
      mappedBy: user

  manyToOne:
    group:
      targetEntity: Domain\Entity\Group
      inversedBy: users

Domain\Entity\Group:
  manyToMany:
    roles:
      targetEntity: Domain\Entity\Role
      joinTable:
        name: groups_roles
        joinColumns:
          group_id:
            referencedColumnName: id
            onDelete: CASCADE
        inverseJoinColumns:
          role_id:
            referencedColumnName: id
            onDelete: CASCADE

I already have many roles, groups and user and when i update an House object with something like:

$userObj = $userRepo->find(3);
$house->setUser($userObj);
$houseRepo->save($house);

Everything explodes and doctrine tell me this:

A new entity was found through the relationship 'User#group' that was not configured to cascade persist operations for entity

So i've edited my mappings to have the cascade=["persist"] option:

Domain\Entity\House:
    manyToOne:
        user:
            targetEntity: Domain\Entity\User
            cascade: ["persist"]
            inversedBy: houses
            joinColumn:
                name: user_id
                referencedColumnName: id
                onDelete: CASCADE
        genre:
            targetEntity: Domain\Entity\Genre
            cascade: ["persist"]
            inversedBy: houses
            joinColumn:
                name: genre_id
                referencedColumnName: id
                onDelete: SET NULL

Domain\Entity\User:
  oneToMany:
    houses:
      targetEntity: Domain\Entity\House
      cascade: ["persist"]
      mappedBy: user

  manyToOne:
    group:
      targetEntity: Domain\Entity\Group
      inversedBy: users
      cascade: ["persist"]

Domain\Entity\Group:
  manyToMany:
    roles:
      cascade: ["persist"]
      targetEntity: Domain\Entity\Role
      joinTable:
        name: groups_roles
        joinColumns:
          group_id:
            referencedColumnName: id
            onDelete: CASCADE
        inverseJoinColumns:
          role_id:
            referencedColumnName: id
            onDelete: CASCADE

But when i try to run the php code above to save an updated House object i got this error

An exception occurred while executing 'INSERT INTO roles (code,description) VALUES (?, ?)' with params ['ROLE_USER', null]: SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry 'ROLE_USER' for key 'UNIQ_B63E2EC777153098'

An exception occurred while executing 'INSERT INTO genres (name, active, parent_id) VALUES (?, ?, ?)' with params [null, 1, null]: SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'name' cannot be null

Why it seems that Doctrine does not know that the Role assigned to the Group, assigned to the User, is already existing in my db? Every data is already present into the DB.

My base repository is use this code to save entities:

abstract class AbstractRepository extends EntityRepository
{
    /**
     * @param $object
     * @throws \Doctrine\ORM\ORMException
     * @throws \Doctrine\ORM\OptimisticLockException
     */
    public function save($object)
    {
        $this->getEM()->persist($object);
        $this->getEM()->flush();
    }

I've used this base repo since Feb-2017 to persist new and updated entities with no problem at all, i really can't understand this behaviour

enter image description here enter image description here enter image description here enter image description here

Snapshot of an House object instance, you can see his User and his User's Group and Group's Roles

enter image description here

Matt
  • 721
  • 9
  • 26
  • Can you view from here and compare ralation and methods that you should have and everything should work -> https://stackoverflow.com/questions/49026436/relationships-in-doctrine/49028183#49028183 – l13 Oct 26 '18 at 19:48
  • no luck.. if i put the code in a Fixture and use the ObjectManager embedded into the Fixture the entity get saved. Can't understand what is the problem. Everything worked so far untile last week – Matt Oct 31 '18 at 15:20

0 Answers0