通过标志显示开关

Display a switch by a flag

如果我的数据库中的标志设置为正值,我需要显示一个开关。

<div class="form-group m-form__group row vertical-padding10">
                            <label class="col-form-label col-lg-2 col-sm-12">Switch</label>
                            <input id="switch" name="switch" data-switch="true" data-size="small" data-on-color="success"  type="checkbox" checked>   
</div>

如何执行查询并在我的视图中显示此开关?这是一个好习惯吗?

您需要添加:

{% if value >= 0 %}
    put your input here
{% endif %}

在您的控制器中,您创建变量 $displaySwitch 来指示是否存在具有真值的布尔值并将其传递给模板:

$em = $this->getDoctrine()->getManager();
$displaySwitch = $em->getRepository(<your entity class>)->findBy(['<your bool field>' => true]);
return $this->render('<your template>',['displaySwitch' => $displaySwitch]);

在模板中,添加条件:如果没有找到符合true的值,则不显示开关:

{% if displaySwitch %}
<div class="form-group m-form__group row vertical-padding10">
    <label class="col-form-label col-lg-2 col-sm-12">Switch</label>
    <input id="switch" name="switch" data-switch="true" data-size="small" data-on-color="success"  type="checkbox" checked>
</div>
{% endif %}