【问题标题】:How to access from Twig to the underlying object in an embedded form in a Symfony project如何在 Symfony 项目中以嵌入式形式从 Twig 访问底层对象
【发布时间】:2015-12-07 17:41:53
【问题描述】:

在使用 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 %}

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

谢谢

【问题讨论】:

  • 试试supplementaryExamination.vars.value.hasNormalOrAlteredEvaluation
  • 它与supplementaryExamination.vars.value.supplementaryExamination.hasNormalOrAlteredEvaluation 合作。天才!非常感谢!
  • 嗨@gianluca78,不客气!我可以发布作为答案,以便您可以标记为已解决您的问题吗?

标签: forms symfony twig symfony-forms embedding


【解决方案1】:

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

您可以通过form.vars.value访问表单的当前数据:

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

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

而不是这个:

{% if supplementaryExamination.hasNormalOrAlteredEvaluation == true %}

希望有帮助

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-07-23
    • 1970-01-01
    • 2011-09-10
    • 1970-01-01
    • 2015-02-05
    • 2016-12-05
    • 1970-01-01
    相关资源
    最近更新 更多