Fosrestbundle: Symfony 4 - mapping data to entity with relations

Created on 15 Sep 2018  路  2Comments  路  Source: FriendsOfSymfony/FOSRestBundle

Hi,
In request i send data to create new entity which has relations to another tables. But after this i got error:

Invalid data 1(integer), expected App\Entity\Priority.

How can i automatically map value from data to entity and assign it to entity which i wanna create?
postProjectAction:

/**
     * @ParamConverter("project", converter="fos_rest.request_body")
     * @FOSRest\Post("/project")
     */
    public function postProjectAction(Project $project, ConstraintViolationList $violations)
    {
        if($violations->count()) {
            var_dump($violations);
        }

        $em = $this->getDoctrine()->getManager();
        $em->persist($project);
        $em->flush();
    }

Project.php - entity:
```

namespace App\Entity;

use DoctrineCommonCollections\ArrayCollection;
use DoctrineCommonCollectionsCollection;
use Symfony\Bridge\Doctrine\ValidatorConstraints\UniqueEntity;
use SymfonyComponent\ValidatorConstraints as Assert;
use Doctrine\ORM\Mapping as ORM;

/**

  • @ORM\Entity(repositoryClass="App\Repository\ProjectRepository")
  • @UniqueEntity(
  • fields={"name", "nameKey"}
  • )
    /
    class Project
    {
    /
    *

    • @ORM\Id()

    • @ORM\GeneratedValue()

    • @ORMColumn(type="integer")

    • @Assert\NotBlank()

      */

      private $id;

/**
 * @ORM\Column(type="string", length=140)
 * @Assert\NotBlank()
 */
private $name;

/**
 * @ORM\Column(type="string", length=5)
 * @Assert\NotBlank()
 */
private $nameKey;

/**
 * @ORM\Column(type="text", nullable=true)
 */
private $description;

/**
 * @ORM\ManyToOne(targetEntity="App\Entity\User", inversedBy="assignedProjects")
 * @ORM\JoinColumn(nullable=false)
 */
private $createdBy;

/**
 * @ORM\ManyToOne(targetEntity="App\Entity\Priority")
 * @ORM\JoinColumn(nullable=false)
 */
private $priority;

/**
 * @ORM\ManyToMany(targetEntity="App\Entity\User", inversedBy="projects")
 */
private $assignedUsers;

/**
 * @ORM\Column(type="datetime", nullable=true)
 */
private $deadline;

/**
 * @ORM\ManyToMany(targetEntity="App\Entity\Customer", inversedBy="projects")
 */
private $assignedCustomers;

/**
 * @ORM\Column(type="boolean")
 */
private $active;

/**
 * @ORM\ManyToOne(targetEntity="App\Entity\Task", inversedBy="assignedProject")
 */
private $tasks;

/**
 * @ORM\Column(type="datetime")
 */
private $createdAt;

public function __construct()
{
    $this->assignedUsers = new ArrayCollection();
    $this->assignedCustomers = new ArrayCollection();
}

public function getId(): ?int
{
    return $this->id;
}

public function getName(): ?string
{
    return $this->name;
}

public function setName(string $name): self
{
    $this->name = $name;

    return $this;
}

public function getNameKey(): ?string
{
    return $this->nameKey;
}

public function setNameKey(string $nameKey): self
{
    $this->nameKey = $nameKey;

    return $this;
}

public function getDescription(): ?string
{
    return $this->description;
}

public function setDescription(?string $description): self
{
    $this->description = $description;

    return $this;
}

public function getCreatedBy(): ?User
{
    return $this->createdBy;
}

public function setCreatedBy(?User $createdBy): self
{
    $this->createdBy = $createdBy;

    return $this;
}

public function getPriority()
{
    return $this->priority;
}

public function setPriority($priority): self
{
    $this->priority = $priority;

    return $this;
}

/**
 * @return Collection|User[]
 */
public function getAssignedUsers()
{
    return $this->assignedUsers;
}

public function addAssignedUser(User $assignedUser): self
{
    if (!$this->assignedUsers->contains($assignedUser)) {
        $this->assignedUsers[] = $assignedUser;
    }

    return $this;
}

public function removeAssignedUser(User $assignedUser): self
{
    if ($this->assignedUsers->contains($assignedUser)) {
        $this->assignedUsers->removeElement($assignedUser);
    }

    return $this;
}

public function getDeadline(): ?\DateTimeInterface
{
    return $this->deadline;
}

public function setDeadline(?\DateTimeInterface $deadline): self
{
    $this->deadline = $deadline;

    return $this;
}

/**
 * @return Collection|Customer[]
 */
public function getAssignedCustomers()
{
    return $this->assignedCustomers;
}

public function addAssignedCustomer(Customer $assignedCustomer): self
{
    if (!$this->assignedCustomers->contains($assignedCustomer)) {
        $this->assignedCustomers[] = $assignedCustomer;
    }

    return $this;
}

public function removeAssignedCustomer(Customer $assignedCustomer): self
{
    if ($this->assignedCustomers->contains($assignedCustomer)) {
        $this->assignedCustomers->removeElement($assignedCustomer);
    }

    return $this;
}

public function getActive(): ?bool
{
    return $this->active;
}

public function setActive(bool $active): self
{
    $this->active = $active;

    return $this;
}

public function getTasks(): ?Task
{
    return $this->tasks;
}

public function setTasks(?Task $tasks): self
{
    $this->tasks = $tasks;

    return $this;
}

public function getCreatedAt(): ?\DateTimeInterface
{
    return $this->createdAt;
}

public function setCreatedAt(\DateTimeInterface $createdAt): self
{
    $this->createdAt = $createdAt;

    return $this;
}

}
```

Most helpful comment

Hello,

I just got a question but how did you implement it without using a form ?

Thanks in advance for your reply

All 2 comments

Are you trying to map entity Priority whose ID is 1, for this project? If that is the case you need a transformer on the inbound to translate ID 1 into entity of ID 1.

Here is an example: https://symfony.com/doc/current/form/data_transformers.html#harder-example-transforming-an-issue-number-into-an-issue-entity

Hello,

I just got a question but how did you implement it without using a form ?

Thanks in advance for your reply

Was this page helpful?
0 / 5 - 0 ratings

Related issues

marcoshoya picture marcoshoya  路  7Comments

ioleo picture ioleo  路  3Comments

kbkk picture kbkk  路  7Comments

matrix818181 picture matrix818181  路  7Comments

MaksSlesarenko picture MaksSlesarenko  路  4Comments