How to insert or update the same Doctrine2 object correctly

I have this piece of code:

$entity = $em->getRepository('AppBundle:Representative')->find($soqlObj1['records'][0]['Id']); if ($entity === null) { $entity = new Representative(); $em->persist($entity); } // we set the values from veeva $entity->setVeevaRepId($soqlObj1['records'][0]['Id']); $entity->setEmail($soqlObj1['records'][0]['Email']); ... $em->flush(); 

And this is part of the Representative entity:

 class Representative { /** * @ORM\Id() * @ORM\Column(type="string", length=45, nullable=false, unique=true) * @ORM\GeneratedValue() * @Expose() */ protected $veeva_rep_id; /** * @var string * @ORM\Column(type="string", length=45) * @Expose() */ protected $display_name; /** * @var string * @ORM\Column(type="string", length=255) * @Expose() */ protected $avatar_url = 'https://pdone.s3.amazonaws.com/avatar/default_avatar.png'; /** * @var string * @ORM\Column(type="string", length=45) * @Expose() */ protected $rep_type = "VEEVA"; /** * @var string * @ORM\Column(type="string", length=45) * @Expose() */ protected $username; /** * @var string * @ORM\Column(type="string", length=45) * @Expose() */ protected $first; /** * @var string * @ORM\Column(type="string", length=45) * @Expose() */ protected $last; /** * @var string * @ORM\Column(type="string", length=45, nullable=true) * @Expose() */ protected $title; /** * @var string * @ORM\Column(type="text", nullable=true) */ protected $bio; /** * @var string * @ORM\Column(type="string", length=45, nullable=true) * @Expose() */ protected $phone; /** * @var string * @ORM\Column(type="string", length=45) * @Expose() */ protected $email; /** * @var bool * @ORM\Column(type="boolean") * @Expose() */ protected $inactive = false; /** * @var \DateTime * @ORM\Column(type="datetime", nullable=true) * @Expose() */ protected $lastLoginAt; /** * @var \DateTime * @ORM\Column(type="datetime") * @Expose() */ protected $lastSyncAt; /** * @var Territory * @ORM\ManyToOne(targetEntity="Territory") * @ORM\JoinColumn(name="territories_id", referencedColumnName="veeva_territory_id") * @Expose() */ protected $territory; /** * @var string * @ORM\Column(type="string", nullable=true, length=150) */ protected $repTokenId; ... } 

There are several other columns that are required at the DB level (nullable = false at the entity level), my question is: if the object does not exist in the DB, it will be created or updated based on the code I wrote, or do I need to move each required field to conditional? What is the right way to achieve this? I am trying to do INSERT|UPDATE based on only one query result

-2
source share
1 answer

If you extracted an object from db, the doctrine itself knows if the next operation will be an update or an insert: you have nothing to worry about

Your answer is valid, but I would change it as follows

 $entity = $em->getRepository('AppBundle:Representative')->find($soqlObj1['records'][0]['Id']); if ($entity === null) { $entity = new Representative(); } // we set the values from veeva $entity->setVeevaRepId($soqlObj1['records'][0]['Id']); $entity->setEmail($soqlObj1['records'][0]['Email']); $em->persist($entity); $em->flush(); 

As I said, you donโ€™t need to worry about inserting or updating, the doctrine will do it for you. However, if you need to set certain values โ€‹โ€‹only if the object is new or if it was extracted from db, just add the correct code in $entity === null control

+3
source

Source: https://habr.com/ru/post/989945/


All Articles