0

Here I have a relationship ManyToMany between my entity doctor and insurance.Car a doctor can be affiliated with several insurance for his clinic. And insurance can insure several doctors. I have already registered insurance and I want to allow a doctor of selection the insurance or to which he is affiliated. But when I want to display this list, nothing appears when I have insurance in my base.

Entity Doctor

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

public function __construct()
{
    $this->assurance = new \Doctrine\Common\Collections\ArrayCollection();
}

Entity Insurance

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

public function __construct() {
    $this->medecin = new \Doctrine\Common\Collections\ArrayCollection();
}

Twig

<select id="assurance" class="form-control" placeholder="Assurance" name="assurance" required>
    <option value="">Assurance *</option>
        {% for m in medecin %}
            {% for assur in m.assurance %}             
                <option value="{{ assur.nom }}" {{ m.assur.id == assur.id ? 'selected' : '' }}>{{ assur.nom|upper }}</option>           
            {% endfor %}
        {% endfor %}
</select>

With this twig, I have no insurance in my select to display.

Controller

  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($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()
        ));
    }
}

Thanks

7
  • is medecin a collection of doctors? I'd like to see some parts of the controller, where you are getting doctors Commented Dec 13, 2018 at 12:26
  • @Cid, i've add the controller. No medecin isn't a collection of doctors.A medecin is a doctor. As I speak French, I use medecin who is equivalent to doctor in English, sorry for the language. Commented Dec 13, 2018 at 12:32
  • 1
    {% for m in medecin %} you are here iterating over a collection. Remove that loop Commented Dec 13, 2018 at 12:34
  • ok @Cid, I've do it. Commented Dec 13, 2018 at 12:40
  • @Cid, that's work. I have my list of insurance. But when i want to save my choice, i have an error because i retrieve the value of this select with $medecin->addAssurance($request->get('assurance')); because a string is given. Commented Dec 13, 2018 at 13:01

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.