如何从 Twig 访问 Symfony 项目中嵌入形式的底层对象

How to access from Twig to the underlying object in an embedded form in a Symfony project

在使用 Symfony 2.7.7 开发的病历项目中,我必须跟踪补充体检的结果。根据完成的考试类型,这些结果可能有多种类型。特别是,要考虑的参数可以是:

为此,我以这种方式完成了实体 SupplementaryMedicalExamination:

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

MedicalRecord 实体与补充检查具有一对多关系。

/**
 * 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
}

MedicalRecordSupplementaryExamination 应包含每次补充检查的结果。

/**
 * 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;
}

在 MedicalRecordType 中,我将 MedicalRecordExamination 作为一个集合包含在内,在 MedicalRecordController 中,我根据可用的 SupplementaryExamination 在 MedicalRecord 对象中添加了许多 MedicalRecordSupplementaryExamination 实例。

问题是我想根据 MedicalSupplementaryExamination 的结果类型在模板中显示正确的表单字段。

我想做这样的事情,但我不知道怎么做,因为在这个级别我正在使用 FormView 实例并且我无法访问底层对象:

{% for supplementaryExamination in form.supplementaryExaminations %}
    {% if supplementaryExamination.hasNormalOrAlteredEvaluation == true %}
        {{ form_row(supplementaryExamination.isAltered) }}
    {% endif %}
{% endfor %}

我看过这个 post:How to access an underlying object from a Twig's FormView in a template? 但我不明白如何将这个解决方案应用到我的案例中。

谢谢

关于如何在 twig 模板中呈现表单,参考 from the doc

You can access the current data of your form via form.vars.value:

所以,对于你的情况,你可以试试这个:

{% if supplementaryExamination.vars.value.supplementaryExamination.hasNormalOrAltered‌​Evaluation == true %}

而不是这个:

{% if supplementaryExamination.hasNormalOrAlteredEvaluation == true %}

希望对您有所帮助