clear()
detaches all entities managed by the entity manager, so $role
is detached too, and trying to persist a detached entity creates a new entity.
You should fetch the role again after clear:
$this->em->clear();
$role = $this->em->getRepository('userRole')->find(1);
Or just create a reference instead:
$this->em->clear();
$role = $this->em->getReference('userRole', 1);
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…