如何在symfony中使用与另一个实体有许多关系的实体的属性



我在医生和保险这两个实体之间有一个ManyToMany关系。我在实体Doctor中配置了注释ManyToMany,在我的表中,Doctrine创建了另一个单独的Doctor_insurance表。现在我想在我的小树枝中使用医生所属保险的图像属性,但我不知道如何使用这个属性

实体医生

/**
* @ORMManyToMany(targetEntity="DoctixMedecinBundleEntityAssurance", cascade={"persist", "remove"})
* @ORMJoinColumn(nullable=true)
*/
private $assurance;
public function __construct()
{
$this->assurance = new ArrayCollection();
}
/**
* Add assurance
*
* @param DoctixMedecinBundleEntityAssurance $assurance
*
* @return Medecin
*/
public function addAssurance(DoctixMedecinBundleEntityAssurance $assurance)
{
$this->assurance[] = $assurance;
return $this;
}
/**
* Remove assurance
*
* @param DoctixMedecinBundleEntityAssurance $assurance
*/
public function removeAssurance(DoctixMedecinBundleEntityAssurance $assurance)
{
$this->assurance->removeElement($assurance);
}
/**
* Get assurance
*
* @return DoctrineCommonCollectionsCollection
*/
public function getAssurance()
{
return $this->assurance;
}

实体保证

<?php
namespace DoctixMedecinBundleEntity;
use DoctrineORMMapping as ORM;
/**
* Assurance
*
* @ORMTable(name="assurance")
* 
*/
class Assurance
{
/**
* @var int
*
* @ORMColumn(name="id", type="integer")
* @ORMId
* @ORMGeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @var string
*
* @ORMColumn(name="nom", type="string", length=40)
*/
public $nom;
/**
* @ORMOneToOne(targetEntity="DoctixMedecinBundleEntityLogo", cascade={"persist","remove","refresh"})
* @ORMJoinColumn(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 DoctixMedecinBundleEntityMedia $logo
*
* @return Assurance
*/
public function setLogo(DoctixMedecinBundleEntityMedia $logo = null)
{
$this->logo = $logo;
return $this;
}
/**
* Get logo
*
* @return DoctixMedecinBundleEntityMedia
*/
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
));
}

Twig

<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>

感谢

保证意味着保证/可以肯定的是,所以你更有可能追求保险,这是对可能发生的事件的保护

您还将第一个实体称为"医生",但将其用作"医学",因此如果医生是医学,请更改此项。

你需要修改你的实体,因为它们是完全错误的:

医生

use DoctixMedecinBundleEntityInsurance;
use DoctrineCommonCollectionsArrayCollection;
use DoctrineCommonCollectionsCollection;
use DoctrineORMMapping as ORM;
class Doctor
{
// ...
/**
* @ORMManyToMany(targetEntity="Insurance", mappedBy="doctors" cascade={"persist", "remove"})
* @ORMJoinColumn(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 DoctixMedecinBundleEntityDoctor;
use DoctrineCommonCollectionsArrayCollection;
use DoctrineCommonCollectionsCollection;
use DoctrineORMMapping as ORM;
class Insurance
{
// ...
/**
* @ORMManyToMany(targetEntity="Doctor", inversedBy="insurances")
* @ORMJoinColumn(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
));
}

Twig

<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>
<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>

最新更新