当我坚持对象邀请时,我遇到了这个错误。我在邀请和候选人之间制作实体邀请内容映射...邀请中的多对一和候选人中的多对多,但我有这个错误:
关联字段"AppBundle\Entity\Invitation#$candidate"的预期值为"AppBundle\Entity\Candidate ",改为"Doctrine\Common\Collections\ArrayCollection"。
代码邀请
<?php
namespace AppBundleEntity;
use DoctrineORMMapping as ORM;
use DoctrineCommonCollectionsArrayCollection;
use AppBundleModelRecruiterInterface;
use AppBundleModelCandidateInterface;
use AppBundleEntityRecruiter;
use AppBundleEntityCandidate;
use AppBundleEntityOfferSkill;
/**
* Invitation
*
* @ORMTable(name="invitation")
* @ORMEntity(repositoryClass="AppBundleRepositoryInvitationRepository")
*/
class Invitation
{
/**
* @var int
*
* @ORMColumn(name="id", type="integer")
* @ORMId
* @ORMGeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @var RecruiterInterface
*
* @ORMManyToOne(targetEntity="Recruiter", inversedBy="invitations")
* @ORMJoinColumns({
* @ORMJoinColumn(name="recruiter_id", referencedColumnName="id")
* })
*/
private $recruiter;
/**
* @var CandidateInterface
*
* @ORMManyToOne(targetEntity="Candidate", inversedBy="invitations")
* @ORMJoinColumns({
* @ORMJoinColumn(name="candidate_id", referencedColumnName="id")
* })
*/
private $candidate;
/**
* @var OfferSkill
*
* @ORMManyToOne(targetEntity="OfferSkill", inversedBy="invitations")
* @ORMJoinColumns({
* @ORMJoinColumn(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 AppBundleEntityRecruiter $recruiter
*
* @return Invitation
*/
public function setRecruiter(AppBundleEntityRecruiter $recruiter = null)
{
$this->recruiter = $recruiter;
return $this;
}
/**
* Get recruiter.
*
* @return AppBundleEntityRecruiter
*/
public function getRecruiter()
{
return $this->recruiter;
}
/**
* Set candidate.
*
* @param AppBundleEntityCandidate $candidate
*
* @return Invitation
*/
public function setCandidate(AppBundleEntityCandidate $candidate = null)
{
$this->candidate = $candidate;
return $this;
}
/**
* Get candidate.
*
* @return AppBundleEntityCandidate
*/
public function getCandidate()
{
return $this->candidate;
}
/**
* Set offerSkill.
*
* @param AppBundleEntityOfferSkill $offerSkill
*
* @return Invitation
*/
public function setOfferSkill(AppBundleEntityOfferSkill $offerSkill = null)
{
$this->offerSkill = $offerSkill;
return $this;
}
/**
* Get offerSkill.
*
* @return AppBundleEntityOfferSkill
*/
public function getOfferSkill()
{
return $this->offerSkill;
}
}
候选中的零件映射:
/**
* @var Invitation[]
*
* @ORMOneToMany(targetEntity="Invitation", mappedBy="candidate", cascade={"persist","remove"})
* @AssertCount(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;
}
代码表单类型:
<?php
namespace AppBundleFormType;
use AppBundleEntityInvitation;
use AppBundleEntityCandidate;
use DoctrineORMEntityRepository;
use SymfonyBridgeDoctrineFormTypeEntityType;
use SymfonyComponentFormAbstractType;
use SymfonyComponentFormFormBuilderInterface;
use SymfonyComponentOptionsResolverOptionsResolver;
use SymfonyComponentFormExtensionCoreTypeSubmitType;
/**
* 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 '';
}
}
代码控制器:
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()];
}
现在我选择了多个候选人,但我有这个错误
不能在InvitationType
中设置multiple => true
,因为Candidate
和Invitation
之间的关系ManyToOne
因此,您需要删除multiple
选项或更改与实体中ManyToMany
的关系