In a medical record project developed with Symfony 2.7.7 I have to track the outcome of supplementary medical examinations. These can have many typology of outcomes according to the type of exam done. In particular, the parameters to take into consideration can be the following:
For this reason I have done an entity SupplementaryMedicalExamination in this way:
class SupplementaryMedicalExamination extends MedicalExamination
{
/**
* @var string $examination
*
* @ORM\Column(type="string", length=255, nullable=false)
*
*/
private $examination;
/**
* @var bool $hasNormalOrAlteredEvaluation;
*
* @ORM\Column(name="has_normal_or_altered_evaluation", type="boolean", nullable=false)
*
*/
private $hasNormalOrAlteredEvaluation;
/**
* @var bool $hasNegativeOrPositiveEvaluation
*
* @ORM\Column(name="has_negative_or_positive_evaluation", type="boolean", nullable=false)
*
*/
private $hasNegativeOrPositiveEvaluation;
/**
* @var bool $hasValue
*
* @ORM\Column(name="has_value", type="boolean", nullable=false)
*
*/
private $hasValue;
}
A MedicalRecord entity has a one to many relationship with supplementary examinations.
/**
* AppBundle\Entity\HealthData\MedicalRecord
*
* @ORM\Table(name="medical_record")
* @ORM\Entity(repositoryClass="MedicalRecordRepository")
* @ORM\HasLifecycleCallbacks
*/
class MedicalRecord
{
//other stuff
/**
* @var ArrayCollection $supplementaryExaminations
*
* @ORM\OneToMany(targetEntity="\AppBundle\Entity\HealthData\MedicalRecordSupplementaryExamination", mappedBy = "medicalRecord")
*/
protected $supplementaryExaminations;
//other stuff
}
And the MedicalRecordSupplementaryExamination should contain the outcomes of each supplementary examination.
/**
* AppBundle\Entity\HealthData\MedicalRecordSupplementaryExamination
*
* @ORM\Table(name="medical_record_supplementary_examination")
* @ORM\Entity()
* @ORM\HasLifecycleCallbacks
*
*/
class MedicalRecordSupplementaryExamination
{
/**
* @var int $id
*
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue(strategy="AUTO")
*
*/
private $id;
/**
* @var bool $isAltered
*
* @ORM\Column(name="is_altered", type="boolean")
*/
protected $isAltered;
/**
* @var bool $isPositive
*
* @ORM\Column(name="is_positive", type="boolean")
*/
protected $isPositive;
/**
* @var string $alterations
*
* @ORM\Column(type="string", length=255)
*/
protected $alterations;
/**
* @var float $value
*
* @ORM\Column(type = "decimal", precision = 10, scale = 2)
*/
protected $value;
/**
* @var MedicalRecord $medicalRecord
*
* @ORM\ManyToOne(targetEntity="medicalRecord", inversedBy = "supplementaryExaminations")
* @ORM\JoinColumn(name="medical_record_id", referencedColumnName="id")
*/
protected $medicalRecord;
/**
* @var SupplementaryMedicalExamination $supplementaryExamination
*
* @ORM\ManyToOne(targetEntity="AppBundle\Entity\HealthData\Core\SupplementaryMedicalExamination")
* @ORM\JoinColumn(name="supplementary_examination_id", referencedColumnName="id")
*/
protected $supplementaryExamination;
}
In the MedicalRecordType, I include the MedicalRecordExamination as a collection and in the MedicalRecordController I add in the MedicalRecord object many instance of MedicalRecordSupplementaryExamination according to the SupplementaryExamination available.
The problem is that I would like to show in the template the right form fields according to the type of outcome of the MedicalSupplementaryExamination.
I would like to do something like this but I don't know how because at this level I'm working with a FormView instance and I can't access the underlying object:
{% for supplementaryExamination in form.supplementaryExaminations %}
{% if supplementaryExamination.hasNormalOrAlteredEvaluation == true %}
{{ form_row(supplementaryExamination.isAltered) }}
{% endif %}
{% endfor %}
I have seen this post: How to access an underlying object from a Twig's FormView in a template? but I can't understand how to apply this solution to my case.
Thank you
Referring from the doc on how rendering form in the twig template:
You can access the current data of your form via
form.vars.value
:
So, in your case, you can try this:
{% if supplementaryExamination.vars.value.supplementaryExamination.hasNormalOrAlteredEvaluation == true %}
instead of this:
{% if supplementaryExamination.hasNormalOrAlteredEvaluation == true %}
Hope this help