How to return entity object type from EntityType class in form?

Viewed 549

Every time I try to send data from form and use doctrine manager I have an error: Error

As you can see, this problem is according to user_type. I have another entity UserType in which I have this attribute and I want to use it in register form. I have controller and I want to add all form input into database and then error occurs. I would like to point out that I don't want to get and set each attribute manually via for instance:

$name = $form['name']->getData();
$userDetails->setName($name);

Because I think that by using entity manager it is supposed to do automatically like this: UserController.php:

 public function register(Request $request){
    $user = new User();
    $userType = new UserType();
    $userDetails = new UserDetails();
    $form = $this->createForm(RegisterFormType::class, $user);


    $form->handleRequest($request);

    if($form->isSubmitted()){

        $userDetails = $form->getData();


        $em=$this->getDoctrine()->getManager();
        $em->persist($userDetails);
        $em->flush();
        dd($form->getData());

My form in RegisterFormType.php:

    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            -> add('name',TextType::class,[
                'data_class' => UserDetails::class,
                'attr' => [
                    "placeholder" => 'Name'
                    ],
                ])
            -> add('surname',TextType::class,[
                'data_class' => UserDetails::class,
                'attr' => [
                    "placeholder" => 'Surname'
                ],
            ])
    
            ->add('email',TextType::class,[
                'attr' => [
                "placeholder" => 'Email'
            ]
            ])
            ->add('password',PasswordType::class,[
                'attr' => [
                    "placeholder" => 'Password'
                ]
            ])
            ->add('confirmPassword',PasswordType::class,[
                'attr' => [
                    "placeholder" => 'Confirm Password'
                ]
            ])
    
    
           /* ->add('user_type',ChoiceType::class, [
                'help' => 'Do you want to perform? Choose Artist account type. Do you want to employ artist? Choose Firm.',
                'label' => "Account type",
                'choices' => [
                    'None' => 0,
                    'firm' => 1,
                    'artist' => 2
                ],
                'attr' => [
                    "class" => 'choice'
                ]
    
            ])*/
           ->add('user_type',EntityType::class, [
               'class' => UserType::class,
               'choice_label'=>'type',
    
               /*'choice_label'=>'type',
               'choice_value' => function ($entity) {
                   return $entity ? $entity->getId() : '';
                   },*/
           ])
        ;
    }
    
    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults([
            'data_class' => User::class,
    
        ]);
    }

After submitting input in form I get this data: enter image description here

The problem is user_type sends as a string I suppose, but should as an object. Am I right?

In User.php

<?php
namespace App\Entity;

use App\Repository\UserRepository;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
    

    /**
     * @ORM\Entity(repositoryClass=UserRepository::class)
     * @ORM\Table(name="`user`")
     */
    class User extends UserDetails
    {
        /**
         * @ORM\Id
         * @ORM\GeneratedValue(strategy="IDENTITY")
         * @ORM\Column(type="integer")
         */
        private $id;
    
        /**
         * @ORM\Column(type="string", length=255)
         */
        private $email;
    
        /**
         * @ORM\Column(type="string", length=255)
         */
        private $password;
    
        private $confirmPassword;
    
        /**
         * @ORM\OneToOne(targetEntity=UserType::class, cascade={"persist", "remove"})
         * @ORM\JoinColumn(nullable=false)
         */
        private $user_type;
    
        /**
         * @ORM\OneToOne(targetEntity=UserDetails::class, cascade={"persist", "remove"})
         * @ORM\JoinColumn(nullable=false)
         */
        private $user_details;
    
    
        /**
         * @ORM\OneToMany(targetEntity=UserToOffert::class, mappedBy="user_", orphanRemoval=true)
         */
        private $user_to_offert;
    
        /**
         * @ORM\OneToMany(targetEntity=Skill::class, mappedBy="user_", orphanRemoval=true)
         */
        private $skill;
    
        /**
         * @ORM\OneToMany(targetEntity=Requirement::class, mappedBy="user_id", orphanRemoval=true)
         */
        private $requirement;
    
    
        public function __construct()
        {
    
            $this->user_to_offert = new ArrayCollection();
            $this->skill = new ArrayCollection();
            $this->requirement = new ArrayCollection();
    
        }
    
        public function getId(): ?int
        {
            return $this->id;
        }
    
        public function getEmail(): ?string
        {
            return $this->email;
        }
    
        public function setEmail(string $email): self
        {
            $this->email = $email;
    
            return $this;
        }
    
        public function getPassword(): ?string
        {
            return $this->password;
        }
    
        public function setPassword(string $password): self
        {
            $this->password = $password;
    
            return $this;
        }
    
        public function getUserType(): ?string
        {
            return $this->user_type;
        }
    
        public function setUserType(string $user_type): self
        {
            $this->user_type = $user_type;
    
            return $this;
        }
    
        public function getUserDetails(): ?UserDetails
        {
            return $this->user_details;
        }
    
        public function setUserDetails(UserDetails $user_details): self
        {
            $this->user_details = $user_details;
    
            return $this;
        }
    
        /**
         * @return mixed
         */
        public function getConfirmPassword()
        {
            return $this->confirmPassword;
        }
    
        /**
         * @param mixed $confirmPassword
         */
        public function setConfirmPassword($confirmPassword): void
        {
            $this->confirmPassword = $confirmPassword;
        }
    
    
    
    
    
        /**
         * @return Collection|UserToOffert[]
         */
        public function getUserToOffert(): Collection
        {
            return $this->user_to_offert;
        }
    
        public function addUserToOffert(UserToOffert $userToOffert): self
        {
            if (!$this->user_to_offert->contains($userToOffert)) {
                $this->user_to_offert[] = $userToOffert;
                $userToOffert->setUser($this);
            }
    
            return $this;
        }
    
        public function removeUserToOffert(UserToOffert $userToOffert): self
        {
            if ($this->user_to_offert->removeElement($userToOffert)) {
                // set the owning side to null (unless already changed)
                if ($userToOffert->getUser() === $this) {
                    $userToOffert->setUser(null);
                }
            }
    
            return $this;
        }
    
        /**
         * @return Collection|Skill[]
         */
        public function getSkill(): Collection
        {
            return $this->skill;
        }
    
        public function addSkill(Skill $skill): self
        {
            if (!$this->skill->contains($skill)) {
                $this->skill[] = $skill;
                $skill->setUser($this);
            }
    
            return $this;
        }
    
        public function removeSkill(Skill $skill): self
        {
            if ($this->skill->removeElement($skill)) {
                // set the owning side to null (unless already changed)
                if ($skill->getUser() === $this) {
                    $skill->setUser(null);
                }
            }
    
            return $this;
        }
    
        /**
         * @return Collection|Requirement[]
         */
        public function getRequirement(): Collection
        {
            return $this->requirement;
        }
    
        public function addRequirement(Requirement $requirement): self
        {
            if (!$this->requirement->contains($requirement)) {
                $this->requirement[] = $requirement;
                $requirement->setUser($this);
            }
    
            return $this;
        }
    
        public function removeRequirement(Requirement $requirement): self
        {
            if ($this->requirement->removeElement($requirement)) {
                // set the owning side to null (unless already changed)
                if ($requirement->getUser() === $this) {
                    $requirement->setUser(null);
                }
            }
    
            return $this;
        }
    
    
    }

UserDetails.php:

    <?php
namespace App\Entity;

use App\Repository\UserDetailsRepository;
use Doctrine\ORM\Mapping as ORM;

/**
 * @ORM\Entity(repositoryClass=UserDetailsRepository::class)
 */
class UserDetails
{
    /**
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="IDENTITY")
     * @ORM\Column(type="integer")
     */
    private $id;

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

    /**
     * @ORM\Column(type="string", length=255)
     */
    private $surname;

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

    /**
     * @ORM\Column(type="string", length=255, nullable=true)
     */
    private $photo;

    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 getSurname(): ?string
    {
        return $this->surname;
    }

    public function setSurname(string $surname): self
    {
        $this->surname = $surname;

        return $this;
    }

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

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

        return $this;
    }


    public function getPhoto(): ?string
    {
        return $this->photo;
    }


    public function setPhoto(?string $photo): self
    {
        $this->photo = $photo;

        return $this;
    }

    public function __toString() {
        return $this->name;
    }


}

In UserType.php:

    <?php
namespace App\Entity;

/**
 * @ORM\Entity(repositoryClass=UserTypeRepository::class)
 */
class UserType
{
    /**
     * @ORM\Id
     * @ORM\GeneratedValue
     * @ORM\Column(type="integer")
     */
    private $id;

    /**
     * @ORM\Column(type="string", length=100)
     */
    private $type;

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

    public function getType(): ?string
    {
        return $this->type;
    }

    public function setType(string $type): self
    {
        $this->type = $type;

        return $this;
    }
    public function __toString() {
        return $this->type;
    }
}

If I remove __toString method I get an error: Error 2

Any ideas what am I doing wrong? Thank you for your time in advance. Have a nice day!

0 Answers
Related