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\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;
}
}
```
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
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