嘿伙计们,我试图在我相同模型的对象之间建立关系。
我有一张桌子Qcm,一张桌子Equivalent,最后一张桌子是EquivalentRelation。这个想法是有一个可以与许多其他 QCM 相关的 QCM。
我的Equivalent模型的设置方式有一个变量,该变量$qcmId是 qcm 的 id (int) 和我的Equivalent模型的主键。
/**
* @ORM\Entity(repositoryClass=EquivalentRepository::class)
*/
class Equivalent
{
/**
* @ORM\Id()
* @ORM\Column(type="integer")
*/
public $qcmId;
/**
* @ORM\ManyToOne(targetEntity=EquivalentRelation::class, inversedBy="equivalent")
*/
private $equivalentRelation;
/**
* @return mixed
*/
public function getQcmId()
{
return $this->qcmId;
}
/**
* @param mixed $qcmId
*/
public function setQcmId($qcmId): void
{
$this->qcmId = $qcmId;
}
public function getEquivalentRelation(): ?EquivalentRelation
{
return $this->equivalentRelation;
}
public function setEquivalentRelation(?EquivalentRelation $equivalentRelation): self
{
$this->equivalentRelation = $equivalentRelation;
return $this;
}
}
我的EquivalentRelation模型设置为$equivalent与我的Equivalent模型相关,并且$relateId与所选的 QCM 相关联。
/**
* @ORM\Entity(repositoryClass=EquivalentRelationRepository::class)
*/
class EquivalentRelation
{
/**
* @ORM\Id
* @ORM\GeneratedValue
* @ORM\Column(type="integer")
*/
private $id;
/**
* @ORM\OneToMany(targetEntity=Equivalent::class, mappedBy="equivalentRelation")
*/
private $equivalent;
/**
* @ORM\ManyToMany(targetEntity=Qcm::class, inversedBy="equivalentRelations")
*/
private $relatedId;
public function __construct()
{
$this->equivalent = new ArrayCollection();
$this->relatedId = new ArrayCollection();
}
public function getId(): ?int
{
return $this->id;
}
/**
* @return Collection|Equivalent[]
*/
public function getEquivalent(): Collection
{
return $this->equivalent;
}
public function addEquivalent(Equivalent $equivalent): self
{
if (!$this->equivalent->contains($equivalent)) {
$this->equivalent[] = $equivalent;
$equivalent->setEquivalentRelation($this);
}
return $this;
}
public function removeEquivalent(Equivalent $equivalent): self
{
if ($this->equivalent->removeElement($equivalent)) {
// set the owning side to null (unless already changed)
if ($equivalent->getEquivalentRelation() === $this) {
$equivalent->setEquivalentRelation(null);
}
}
return $this;
}
/**
* @return Collection|Qcm[]
*/
public function getRelatedId(): Collection
{
return $this->relatedId;
}
public function addRelatedId(Qcm $relatedId): self
{
if (!$this->relatedId->contains($relatedId)) {
$this->relatedId[] = $relatedId;
}
return $this;
}
public function removeRelatedId(Qcm $relatedId): self
{
$this->relatedId->removeElement($relatedId);
return $this;
}
}
下一步是制作一个EquivalentType表格,该表格将采用所选的 QCM(可以是多个)并轻松验证我的模型。到目前为止,这部分似乎没有问题。
class EquivalentType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('relatedId', EntityType::class, [
'class' => Qcm::class,
'choice_label' => 'id',
'multiple' => true,
'label' => 'QCM Ref Master',
]);
}
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults([
'data_class' => EquivalentRelation::class
]);
}
}
发送我的表格后一切正常,
通过以下方式处理请求 QcmController:
由于我有两个相关的不同模型,我首先创建一个新的Equivalent.
$equivalent = new Equivalent();
$equivalent->setQcmId($qcm->getId());
其次是 EquivalentRelation 的相同内容
$equivalentrelation = new EquivalentRelation();
$equivalentrelation->addEquivalent($equivalent);
当尝试访问EquivalentRelation与查询参数等效的以显示与其相关的所有 QCM ( relatedId) 对象时。
$tester = $equivalentRepository->findBy(['qcmId' => $qcm->getId()]);
$data = $equivalentRelationRepository->findBy(['equivalent' => $tester]);
这是它的控制器端:
$equivalent = new Equivalent();
$equivalent->setQcmId($qcm->getId());
$equivalentrelation = new EquivalentRelation();
$equivalentrelation->addEquivalent($equivalent);
$equival = $this->createForm(EquivalentType::class, $equivalentrelation);
$equival->handleRequest($request);
if ($equival->isSubmitted() && $equival->isValid()){
$entityManager->persist($equivalent);
$entityManager->persist($equivalentrelation);
$entityManager->flush();
return $this->redirectToRoute('qcm_show', ['id' => $qcm->getId()]);
}
我收到此错误消息:
You cannot search for the association field 'App\Entity\EquivalentRelation#equivalent', because it is the inverse side of an association. Find methods only work on owning side associations.
我尝试了所有方法,我不知道我的设计或映射系统是否有问题。