0
votes

I have this error when I persist Object Invitation .. I make entity Invitation content mapping between invitation and candidat ...ManyToOne in Invitation and OneToMany in Candidate but I have this error:

Expected value of type "AppBundle\Entity\Candidate" for association field "AppBundle\Entity\Invitation#$candidate", got "Doctrine\Common\Collections\ArrayCollection" instead.

code Invitation

<?php

    namespace AppBundle\Entity;

    use Doctrine\ORM\Mapping as ORM;
    use Doctrine\Common\Collections\ArrayCollection;
    use AppBundle\Model\RecruiterInterface;
    use AppBundle\Model\CandidateInterface;
    use AppBundle\Entity\Recruiter;
    use AppBundle\Entity\Candidate;
    use AppBundle\Entity\OfferSkill;

    /**
     * Invitation
     *
     * @ORM\Table(name="invitation")
     * @ORM\Entity(repositoryClass="AppBundle\Repository\InvitationRepository")
     */
    class Invitation
    {
        /**
         * @var int
         *
         * @ORM\Column(name="id", type="integer")
         * @ORM\Id
         * @ORM\GeneratedValue(strategy="AUTO")
         */
        private $id;

        /**
         * @var RecruiterInterface
         *
         * @ORM\ManyToOne(targetEntity="Recruiter", inversedBy="invitations")
         * @ORM\JoinColumns({
         *   @ORM\JoinColumn(name="recruiter_id", referencedColumnName="id")
         * })
         */
        private $recruiter;

        /**
         * @var CandidateInterface
         *
         * @ORM\ManyToOne(targetEntity="Candidate", inversedBy="invitations")
         * @ORM\JoinColumns({
         *   @ORM\JoinColumn(name="candidate_id", referencedColumnName="id")
         * })
         */
        private $candidate;

        /**
         * @var OfferSkill
         *
         * @ORM\ManyToOne(targetEntity="OfferSkill", inversedBy="invitations")
         * @ORM\JoinColumns({
         *   @ORM\JoinColumn(name="offerskill_id", referencedColumnName="id")
         * })
         */
        private $offerSkill;


        /**
         * Get id
         *
         * @return int
         */
        public function getId()
        {
            return $this->id;
        }

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

        /**
         * Set recruiter.
         *
         * @param \AppBundle\Entity\Recruiter $recruiter
         *
         * @return Invitation
         */
        public function setRecruiter(\AppBundle\Entity\Recruiter $recruiter = null)
        {
            $this->recruiter = $recruiter;

            return $this;
        }

        /**
         * Get recruiter.
         *
         * @return \AppBundle\Entity\Recruiter
         */
        public function getRecruiter()
        {
            return $this->recruiter;
        }

        /**
         * Set candidate.
         *
         * @param \AppBundle\Entity\Candidate $candidate
         *
         * @return Invitation
         */
        public function setCandidate(\AppBundle\Entity\Candidate $candidate = null)
        {
            $this->candidate = $candidate;

            return $this;
        }

        /**
         * Get candidate.
         *
         * @return \AppBundle\Entity\Candidate
         */
        public function getCandidate()
        {
            return $this->candidate;
        }

        /**
         * Set offerSkill.
         *
         * @param \AppBundle\Entity\OfferSkill $offerSkill
         *
         * @return Invitation
         */
        public function setOfferSkill(\AppBundle\Entity\OfferSkill $offerSkill = null)
        {
            $this->offerSkill = $offerSkill;

            return $this;
        }

        /**
         * Get offerSkill.
         *
         * @return \AppBundle\Entity\OfferSkill
         */
        public function getOfferSkill()
        {
            return $this->offerSkill;
        }
    }

part mapping in Candidate:

 /**
             * @var Invitation[]
             *
             * @ORM\OneToMany(targetEntity="Invitation", mappedBy="candidate", cascade={"persist","remove"})
             * @Assert\Count(min = 1)
             */
            private $invitations;


            /**
             * Add invitation.
             *
             * @param Invitation $invitation
             *
             * @return RecruiterInterface
             */
            public function addInvitation(Invitation $invitation): CandidateInterface
            {
                $this->invitations[] = $invitation;

                return $this;
            }

            /**
             * Remove invitation.
             *
             * @param Invitation $invitation
             */
            public function removeInvitation(Invitation $invitation)
            {
                $this->invitations->removeElement($invitation);
            }

            /**
             * @return Collection
             */
            public function getInvitation()
            {
                return $this->invitations;
            }

code Form Type:

<?php

    namespace AppBundle\Form\Type;

    use AppBundle\Entity\Invitation;
    use AppBundle\Entity\Candidate;
    use Doctrine\ORM\EntityRepository;
    use Symfony\Bridge\Doctrine\Form\Type\EntityType;
    use Symfony\Component\Form\AbstractType;
    use Symfony\Component\Form\FormBuilderInterface;
    use Symfony\Component\OptionsResolver\OptionsResolver;
    use Symfony\Component\Form\Extension\Core\Type\SubmitType;

    /**
     * Class InvitationType.
     */
    class InvitationType extends AbstractType
    {
        /**
         * {@inheritdoc}
         */
        public function buildForm(FormBuilderInterface $builder, array $options)
        {

            $builder
                ->add('candidate', EntityType::class, [
                    'class' => Candidate::class,
                    'choice_label' => 'Username',
                    'required' => true,
                    'multiple' => true,
                ])
                ->add('Create', SubmitType::class, array('attr' => array('class' => 'experience btn btn-fill btn-rose')))
            ;
        }

        /**
         * {@inheritdoc}
         */
        public function configureOptions(OptionsResolver $resolver)
        {
            $resolver->setDefaults(array(
                'data_class' => Invitation::class,
                'allow_extra_fields' => true,
            ));
        }

        /**
         * {@inheritdoc}
         */
        public function getBlockPrefix()
        {
            return '';
        }
    }

code controller:

public function newAction($id,Request $request) {
            try 
            {
                $offer = $this->getDoctrine()->getRepository(OfferSkill::class)->findOneBy(['id'=>$id]);
                $invitation = $this->getHandler()->invitation($offer, $request->request->all(), $request->isMethod(Request::METHOD_POST));
                if ($invitation instanceOf Invitation) {
                     $url = $this->generateUrl('homepage');

                    return new RedirectResponse($url);
                }
            }

            catch(InvalidFormException $e) {
                return [
                    'form' => $e->getForm()->createView(),
                    'edit' => false,
                ];
            }

            return ['form' => $invitation->createView()];
        }

now I have selected multiple candidate but I have this error

1

1 Answers

1
votes

You can't set multiple => true in InvitationType because the relation is ManyToOne between the Candidate and Invitation
So you need to remove the multiple option or change the relation to ManyToMany in your entities