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

时间:2015-12-07 17:41:54

标签: forms symfony twig symfony-forms embedding

在使用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实例。

问题在于我想根据医学补充考试的结果类型在模板中显示正确的表格字段。

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

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

我看过这篇文章:How to access an underlying object from a Twig's FormView in a template?但我无法理解如何将这个解决方案应用到我的案例中。

谢谢

1 个答案:

答案 0 :(得分:3)

参考from the doc如何在树枝模板中渲染表单:

  

您可以通过form.vars.value

访问表单的当前数据

所以,在你的情况下,你可以试试这个:

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

而不是:

{% if supplementaryExamination.hasNormalOrAlteredEvaluation == true %}

希望这个帮助