php - doctrine how to merge incomplete data

37

I'm creating an REST api with Symfony 4.2 and Doctrine as ORM connected to a MySQL database. I receive data in JSON , deserialize them with JMSSerializer and use the doctrine merge function to attach it. How can I merge the entity I got from the JSON with an already existing entity, updating only the given fields in the JSON?

Event entity

<?php

namespace App\Entity;

use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use JMS\Serializer\Annotation\Type;

/**
 * Event
 *
 * @ORM\Table(name="Event", indexes={@ORM\Index(name="Organiser", columns={"Organiser"})})
 * @ORM\Entity
 */
class Event
{
    /**
     * @var int
     *
     * @ORM\Column(name="ID", type="integer", nullable=false)
     * @ORM\Id
     * @ORM\GeneratedValue
     * @Type("int")
     */
    private $id;

    /**
     * @var \Invited
     * 
     * @ORM\OneToMany(targetEntity="Invited",mappedBy="event",cascade={"persist","merge","remove"})
     * @Type("ArrayCollection<App\Entity\Invited>")
     */
    private $guests;

    /**
     * @var string
     *
     * @ORM\Column(name="Title", type="string", length=255, nullable=false)
     * @Type("string")
     */
    private $title;

    /**
     * @var \DateTime
     *
     * @ORM\Column(name="StartDate", type="datetime", nullable=false)
     * @Type("DateTime")
     */
    private $startdate;

    /**
     * @var \DateTime|null
     *
     * @ORM\Column(name="EndDate", type="datetime", nullable=true)
     * @Type("DateTime")
     */
    private $enddate;

    /**
     * @var string
     *
     * @ORM\Column(name="Place", type="string", length=255, nullable=false)
     * @Type("string")
     */
    private $place;

    /**
     * @var string|null
     *
     * @ORM\Column(name="ImagePath", type="string", length=255, nullable=true)
     * @Type("string")
     */
    private $imagepath;

    /**
     * @var \User
     *
     * @ORM\ManyToOne(targetEntity="User",cascade={"persist"})
     * @ORM\JoinColumns({
     *   @ORM\JoinColumn(name="Organiser", referencedColumnName="ID")
     * })
     * @Type("App\Entity\User")
     */
    private $organiser;
?>

Deserialisation

 $json = $request->getContent();
        $serializer = SerializerBuilder::create()->build();
        $event = $serializer->deserialize($json,'App\Entity\Event','json');

Merging

  $feedback = $this->em->merge($newEvent);
  $this->em->flush();

Expected vs Actual results:

If I have an already existing and attached event like:

{
 id:1,
 title: 'oldTitle',
 startDate: oldDate,
 place: 'oldPlace',
...
}

And I receive :

{
 id:1,
 title: 'newTitle'
}

Expected result:

{
 id:1,
 title: 'newTitle',
 startDate: oldDate,
 place: 'oldPlace',
...
}

Actual result when looking to the database:

{
 id:1,
 title: 'newTitle',
 startDate: null,
 place: null,
...
}

How can I achieve that in a efficient way?

People are also looking for solutions to the problem: php - The wordpress Read More doesn't work, it displays the full content

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.