I have an Account entity which contains a collection of Group entities. In addition, each Account must have a default Group. How can one persist the entities? When attempting to do so, $manager->flush(); complains with a Not null violation for the entity that was persisted first.
Account:
namespace App\Entity; use Doctrine\Common\Collections\ArrayCollection; use Doctrine\Common\Collections\Collection; use Symfony\Bridge\Doctrine\IdGenerator\UuidV4Generator; use Doctrine\ORM\Mapping as ORM; /** * @ORM\Entity() */ class Account { /** * @ORM\Id * @ORM\Column(type="uuid", unique=true) * @ORM\GeneratedValue(strategy="CUSTOM") * @ORM\CustomIdGenerator(class=UuidV4Generator::class) */ private $id; /** * @ORM\OneToOne(targetEntity=Group::class, cascade={"persist", "remove"}) * @ORM\JoinColumn(nullable=false, unique=true) */ private $defaultGroup; /** * @ORM\OneToMany(targetEntity=Group::class, mappedBy="account") */ private $groups; public function __construct() { $this->groups = new ArrayCollection(); $this->defaultGroup = new Group(); } // Typical getters and setters } Group:
namespace App\Entity; use Doctrine\ORM\Mapping as ORM; /** * @ORM\Entity() */ class Group { /** * @ORM\Id * @ORM\GeneratedValue * @ORM\Column(type="integer") */ private $id; /** * @ORM\ManyToOne(targetEntity=Account::class, inversedBy="groups") * @ORM\JoinColumn(nullable=false, onDelete="CASCADE") */ private $account; // Typical getters and setters }