在symfony中插入ManyToMany关系时无法识别的字段

时间:2018-12-13 17:36:58

标签: symfony many-to-many

我尝试与许多人建立关系,但是我遇到一个错误,告诉我“无法识别的字段:保险”。

我想注册医生选择的一两种保险,但是在同花顺期间,由于医生与保险之间存在许多关系,我会出错

实体医学

  /**
 * @ORM\ManyToMany(targetEntity="Doctix\MedecinBundle\Entity\Assurance", cascade={"persist", "remove"}, inversedBy="medecin")
 * @ORM\JoinColumn(nullable=true)
 */
private $assurance;

 /**
 * Add assurance
 *
 * @param \Doctix\MedecinBundle\Entity\Assurance $assurance
 *
 * @return Medecin
 */
public function addAssurance(\Doctix\MedecinBundle\Entity\Assurance $assurance)
{

     $assurance->addMedecin($this);   
    $this->assurances[] = $assurance;


}

/**
 * Remove assurance
 *
 * @param \Doctix\MedecinBundle\Entity\Assurance $assurance
 */
public function removeAssurance(\Doctix\MedecinBundle\Entity\Assurance $assurance)
{
    $this->assurance->removeElement($assurance);
}

实体保险

  /**
 * 
 * @ORM\ManyToMany(targetEntity="Doctix\MedecinBundle\Entity\Medecin", mappedBy="assurance")
 */
private $medecin;

  /**
 * Add medecin
 *
 * @param \Doctix\MedecinBundle\Entity\Medecin $medecin
 *
 * @return Assurance
 */
public function addMedecin(\Doctix\MedecinBundle\Entity\Medecin $medecin)
{

    $this->medecins[] = $medecin;
}

/**
 * Remove medecin
 *
 * @param \Doctix\MedecinBundle\Entity\Medecin $medecin
 */
public function removeMedecin(\Doctix\MedecinBundle\Entity\Medecin $medecin)
{
    $this->medecin->removeElement($medecin);
}

树枝

    <div class="col-md-6">
                        <select id="assurance" class="form-control" placeholder="Assurance" name="assurance" multiple required>
                            <option value="">Assurance *</option>

                               {% for assur in assurances %}

                                <option value="{{ assur.nom }}" >{{ assur.nom|upper }}</option>


                            {% endfor %}
                        </select>
                    </div>

控制器,我尝试在其中检索选择值

    public function editAction(Request $request)
{
    $user = $this->getUser();
    if ($user === null) {
        throw new NotFoundHttpException('Utilisateur Inexistant');
    }

    $em = $this->getDoctrine()->getManager();
    $repo = $em->getRepository('DoctixMedecinBundle:Medecin');
    $specialiteRepo = $em->getRepository('DoctixAdminBundle:Specialite');

    $assuranceRepo = $em->getRepository('DoctixMedecinBundle:Assurance');

    $medecin = $repo->findOneBy(array(
        'user' => $user,
    ));

    if ($request->isMethod('POST')) {

        if ( ($pass = $request->get('pass')) != ''){
            $medecin->getUser()->setSalt('');
            $factory = $this->get('security.encoder_factory');
            $encoder = $factory->getEncoder($medecin->getUser());
            $password_encoder = $encoder->encodePassword($pass, $medecin->getUser()->getSalt());
            $medecin->getUser()->setPassword($password_encoder);
        }

        $medecin->setSpecialite($specialiteRepo->find($request->get('specialite')));
        $medecin->getUser()->setAdresse($request->get('adresse'));
        $medecin->getUser()->setNumTel($request->get('telephone'));
        $medecin->setQuartier($request->get('quartier'));
        $medecin->addAssurance($assuranceRepo->findOneBy(array(

            'assurance' => $request->get('assurance'))));

        // Save
        $em->flush();

        // redirection avec le status http 301 ::)))))
        $url = $this->generateUrl('medecin_parametre');
        return $this->redirect($url, 301);

    } else {
        return $this->render('DoctixMedecinBundle:Medecin:editProfile.html.twig', array(
            'medecin' => $medecin,
            'specialites' => $specialiteRepo->findAll(),
            'assurances' => $assuranceRepo->findAll()
        ));
    }
}

但是当我尝试冲洗时,出现错误“无法识别的场:保证”

capture of my ormexception

谢谢

2 个答案:

答案 0 :(得分:0)

我看到了一些东西:

1-使用多对多时,需要定义两个实体之间的联接表。 many-to-many

2-您的收款字段是保证书和医学信息(最后带有's')。

$ this-> assurance s -> removeElement($ assurance);

$ this-> medecin s -> removeElement($ medecin);

3-您的错误是因为您在guaranteeRepo一个Assurance实体上搜索(findOneBy),而该实体没有保证字段。医学有保证领域。

$medecin->addAssurance(
          $assuranceRepo->findOneBy(array(
            'assurance' => $request->get('assurance'))));

$medecin->addAssurance(
              $assuranceRepo->findOneBy(array(
                'name' => $request->get('assurance'))));

答案 1 :(得分:0)

最后,我在我的关系ManyToMany中找到了解决备份问题的解决方案,我在两个实体的实体集合级别进行了更改。

实体医学

   /**
 * Add assurance
 *
 * @param \Doctix\MedecinBundle\Entity\Assurance $assurance
 *
 * @return Medecin
 */
public function addAssurance(\Doctix\MedecinBundle\Entity\Assurance $assurance)
{
             if (!$this->assurance->contains($assurance))
    {
        $this->assurance->add($assurance);
        $assurance->addMedecin($this);
    }
 }

  /**
 * Remove assurance
 *
 * @param \Doctix\MedecinBundle\Entity\Assurance $assurance
 */
public function removeAssurance(\Doctix\MedecinBundle\Entity\Assurance $assurance)
{
         if ($this->assurance->contains($assurance))
    {
        $this->assurance->removeElement($assurance);
        $assurance->removeMedecin($this);
    } 
 }

实体保证

   /**
 * Add medecin
 *
 * @param \Doctix\MedecinBundle\Entity\Medecin $medecin
 *
 * @return Assurance
 */
public function addMedecin(\Doctix\MedecinBundle\Entity\Medecin $medecin)
{
         if (!$this->medecin->contains($medecin))
    {
        $this->medecin->add($medecin);
        $medecin->addAssurance($this);
    }
  }

  /**
 * Remove medecin
 *
 * @param \Doctix\MedecinBundle\Entity\Medecin $medecin
 */
public function removeMedecin(\Doctix\MedecinBundle\Entity\Medecin $medecin)
{ 
        if ($this->medecin->contains($medecin))
    {
        $this->medecin->removeElement($medecin);
        $medecin->removeAssurance($this);
    }

 }

非常感谢您的帮助