forms - 如何在 Symfony 项目中以嵌入形式从 Twig 访问底层对象

标签 forms symfony twig symfony-forms embedding

在使用 Symfony 2.7.7 开发的医疗记录项目中,我必须跟踪补充医疗检查的结果。根据所进行的考试类型,这些结果可以有多种类型。特别是,要考虑的参数如下:

  • 正常/改变;
  • 正/负;
  • 值(value)。

出于这个原因,我以这种方式做了一个实体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
}

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

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

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

谢谢

最佳答案

引用from the doc关于如何在 twig 模板中渲染表单:

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

希望这有帮助

关于forms - 如何在 Symfony 项目中以嵌入形式从 Twig 访问底层对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34139834/

相关文章:

html - 表单选择列表 - 最初选择的选项在 IE 中无法正常工作

php - 在 symfony 中获取容器

symfony - serviceContainer 在 symfony 2.8 中返回一个 IdentityTranslator

symfony - 无法 Autowiring 服务 FOSUserBundle,Symfony 3.4

twig - Twig 和液体有什么区别?

javascript - 通过 Javascript 验证限制表单字符数

jquery - 使用 jQuery 表单插件和 Spring MVC 提交表单后的成功消息

java - Wicket:从表单设置 JodaDateTime

symfony - Twig:包含另一个模板的 block

php - Twig 根据条件扩展模板