clear()
separates all entities managed by the entity manager, so $role
also disabled, and trying to save a separate object creates a new object.
You should get this role again after cleaning:
$this->em->clear(); $role = $this->em->getRepository('userRole')->find(1);
Or just create a link:
$this->em->clear(); $role = $this->em->getReference('userRole', 1);
source share