我在两个实体Doctor和insurance之间有一个ManyToMany关系。 我在实体Doctor中配置了注释ManyToMany,在我的表中,Doctrine创建了另一个单独的doctor_insurance表。 现在我想使用我的树枝中附属有医生的保险的图像属性,但是我不知道如何使用该属性
实体医生
/**
* @ORM\ManyToMany(targetEntity="Doctix\MedecinBundle\Entity\Assurance", cascade={"persist", "remove"})
* @ORM\JoinColumn(nullable=true)
*/
private $assurance;
public function __construct()
{
$this->assurance = new ArrayCollection();
}
/**
* Add assurance
*
* @param \Doctix\MedecinBundle\Entity\Assurance $assurance
*
* @return Medecin
*/
public function addAssurance(\Doctix\MedecinBundle\Entity\Assurance $assurance)
{
$this->assurance[] = $assurance;
return $this;
}
/**
* Remove assurance
*
* @param \Doctix\MedecinBundle\Entity\Assurance $assurance
*/
public function removeAssurance(\Doctix\MedecinBundle\Entity\Assurance $assurance)
{
$this->assurance->removeElement($assurance);
}
/**
* Get assurance
*
* @return \Doctrine\Common\Collections\Collection
*/
public function getAssurance()
{
return $this->assurance;
}
实体保证
<?php
namespace Doctix\MedecinBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* Assurance
*
* @ORM\Table(name="assurance")
*
*/
class Assurance
{
/**
* @var int
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @var string
*
* @ORM\Column(name="nom", type="string", length=40)
*/
public $nom;
/**
* @ORM\OneToOne(targetEntity="Doctix\MedecinBundle\Entity\Logo", cascade={"persist","remove","refresh"})
* @ORM\JoinColumn(nullable=true)
*/
private $logo;
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set nom
*
* @param string $nom
*
* @return Assurance
*/
public function setNom($nom)
{
$this->nom = $nom;
return $this;
}
/**
* Get nom
*
* @return string
*/
public function getNom()
{
return $this->nom;
}
/**
* Set logo
*
* @param \Doctix\MedecinBundle\Entity\Media $logo
*
* @return Assurance
*/
public function setLogo(\Doctix\MedecinBundle\Entity\Media $logo = null)
{
$this->logo = $logo;
return $this;
}
/**
* Get logo
*
* @return \Doctix\MedecinBundle\Entity\Media
*/
public function getLogo()
{
return $this->logo;
}
}
控制器
public function parametreAction(Request $request)
{
$em = $this->getDoctrine()->getManager();
$repo = $em->getRepository('DoctixMedecinBundle:Medecin');
$medecin = $repo->findOneBy(array(
'user' => $this->getUser(),
));
$medecin->getAssurance();
return $this->render('DoctixMedecinBundle:Medecin:parametre.html.twig', array(
'medecin' => $medecin
));
}
树枝
<div class = "col-md-2">
<div class="box_list photo-medecin">
<figure>
<img src="{{ vich_uploader_asset(medecin.assurance, 'logoFile')
}}" class="img-fluid" alt="">
</figure>
</div>
谢谢
答案 0 :(得分:2)
保证表示 Guarantee / 可以肯定,因此您更有可能追求 Insurance 防止可能发生的事情。
您还呼叫了第一个实体医生,但将其用作医学,因此,如果医生是药物,请进行更改。
您需要修改实体,因为它们完全错误:
医生
use Doctix\MedecinBundle\Entity\Insurance;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
class Doctor
{
// ...
/**
* @ORM\ManyToMany(targetEntity="Insurance", mappedBy="doctors" cascade={"persist", "remove"})
* @ORM\JoinColumn(nullable=true)
*/
private $insurances;
public function __construct()
{
$this->insurances = new ArrayCollection();
}
public function addInsurance(Insurance $insurance)
{
if (!$this->insurances->contains($insurance))
{
$this->insurances->add($insurance);
$insurance->addDoctor($this);
}
}
public function removeInsurance(Insurance $insurance)
{
if ($this->insurances->contains($insurance))
{
$this->insurances->removeElement($insurance);
$insurance->removeDoctor($this);
}
}
/**
* @return Collection
*/
public function getInsurances()
{
return $this->insurances;
}
// ...
}
保险
use Doctix\MedecinBundle\Entity\Doctor;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
class Insurance
{
// ...
/**
* @ORM\ManyToMany(targetEntity="Doctor", inversedBy="insurances")
* @ORM\JoinColumn(nullable=true)
*/
private $doctors;
public function __construct()
{
$this->doctors = new ArrayCollection();
}
public function addDoctor(Doctor $doctor)
{
if (!$this->doctors->contains($doctor))
{
$this->doctors->add($doctor);
$doctor->addInsurance($this);
}
}
public function removeDoctor(Doctor $doctor)
{
if ($this->doctors->contains($doctor))
{
$this->doctors->removeElement($doctor);
$doctor->removeInsurance($this);
}
}
/**
* @return Collection
*/
public function getDoctors()
{
return $this->doctors;
}
// ...
}
控制器
public function parametreAction(Request $request)
{
$em = $this->getDoctrine()->getManager();
$doctor = $em->getRepository('DoctixMedecinBundle:Medecin')->findOneBy(array(
'user' => $this->getUser(),
));
return $this->render('DoctixMedecinBundle:Medecin:parametre.html.twig', array(
'doctor' => $doctor
));
}
树枝
<div class="col-md-2">
<div class="box_list photo-medecin">
{% for insurance in doctor.insurances %}
<figure>
<img src="{{ vich_uploader_asset(insurance.logo, 'logoFile')
}}" class="img-fluid" alt="">
</figure>
{% endfor %}
</div>
</div>
答案 1 :(得分:1)
<div class = "col-md-2">
{% for item in medecin.assurance %}
<div class="box_list photo-medecin">
<figure>
<img src="{{ vich_uploader_asset(item.logo , 'logoFile')
}}" class="img-fluid" alt="">
</figure>
{% endfor %}
</div>