使用来自外部 table 的数据以 symfony 2 形式显示集合
displaying a collection in symfony 2 form with data from a foreign table
我是 symfony 的新手,我无法找到有关以正确方式显示表单的问题的解决方案。
我有三个表:活动、状态和人员
desc activities;
+----------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+----------+--------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| start | datetime | NO | | NULL | |
| stop | datetime | YES | | NULL | |
| activity | varchar(100) | NO | | NULL | |
| location | varchar(45) | NO | | NULL | |
| event_id | int(11) | YES | MUL | NULL | |
+----------+--------------+------+-----+---------+----------------+
desc presences;
+-------------+----------------------------------------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------------+----------------------------------------------+------+-----+---------+-------+
| activity_id | int(11) | NO | PRI | NULL | |
| person_id | int(11) | NO | PRI | NULL | |
| status | enum('afwezig','aanwezig','verontschuldigd') | NO | PRI | afwezig | |
+-------------+----------------------------------------------+------+-----+---------+-------+
desc persons;
+---------------+----------------+------+-----+-------------------+-----------------------------+
| Field | Type | Null | Key | Default | Extra |
+---------------+----------------+------+-----+-------------------+-----------------------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| firstname | varchar(20) | YES | | NULL | |
| name | varchar(30) | YES | | NULL | |
+----------------+----------------+--------+---- -+--------------------+-------------------------- -+
我创建了 2 个表单类型
一个用于活动
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add(
'start',
'datetime',
array(
'label' => 'Start'
)
)
->add(
'stop',
'datetime',
array(
'label' => 'Stop'
)
)
->add('activity', 'text', array('label' => 'Naam'))
->add('location', 'text', array('label' => 'Locatie'))
->add(
'presences',
'collection',
array(
'type' => new PresencesType()
)
)
->add('save', 'submit', array('label' => 'Aanpassen'));
}
还有一个用于显示某个 activity
的存在
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add(
'persons',
'entity',
array(
'class' => 'KHOSAdminBundle:Persons',
'label' => false,
'property' => 'fullname',
'read_only' => true,
'disabled' =>true
)
)
->add(
'status',
'choice',
array(
'label' => false,
'multiple' => false,
'expanded' => true,
'choices' => array(
'afwezig' => 'afwezig',
'aanwezig' => 'aanwezig',
'verontschuldigd' => 'verontschuldigd'
)
)
);
}
我可以使用 twig 渲染它,以便它显示 activity 的详细信息,并在存在列表下方显示。
{% for presence in form_activity.presences %}
<div class="row">
<div class="col-md-4">
{{ form_row(presence.persons) }}
</div>
<div class="col-md-6">
{{ form_row(presence.status, { 'style': 'inline' }) }}
</div>
</div>
{% endfor %}
但是所有人都是下拉列表,加载需要很长时间。
但我不想要那个
我只想要 activity 的详细信息,下面是所有具有单选按钮状态的人的列表
Start: <<inputfield>> Stop: <<inputfield>>
location: <<inputfield>>
firstname1 name1 : O afwezig O aanwezig O verontschuldigd
firstname2 name2 : O afwezig O aanwezig O verontschuldigd
firstname3 name3 : O afwezig O aanwezig O verontschuldigd
firstname4 name4 : O afwezig O aanwezig O verontschuldigd
有人可以帮助我吗?
正如我在评论中建议的那样,您应该使用不使用 entity
字段的自定义类型来显示状态(无论如何您都不希望能够编辑 Presence 实例中包含的 Person ,这意味着您不需要 firstName 和 lastName 字段)。
这是 php 部分:
<?php
namespace Demo {
class Activity
{
protected $id;
protected $start;
protected $stop;
/** @var Presence[] */
protected $presences = [];
// ...
}
class Presence {
/** @var Activity */
protected $activity;
/** @var Person */
protected $person;
protected $status;
// ...
}
class Person {
protected $firstName;
protected $lastName;
/** @var Presence[] */
protected $presences;
// ...
}
class ActivityType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('start')
->add('stop')
->add('location')
->add('presences', 'collection', [
'type' => new PresenceType(),
])
;
}
public function getName()
{
return 'activity';
}
}
class PresenceType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('status', 'choice', [
'multiple' => true,
'expanded' => false,
'choices' => [
'afwezig' => 'afwezig',
'aanwezig' => 'aanwezig',
'verontschuldigd' => 'verontschuldigd'
]
])
;
}
public function getName()
{
return 'presence';
}
}
}
和 twig 表单主题(它可能需要一些调整才能实际工作,但想法是定义当您调用 form_row(form) 并且表单是 PresenceType 视图实例时呈现表单视图的方式):
{% block presence_row %}
{{ form.vars.data.user.firstName }} {{ form.vars.data.user.lastName }} {{ form_widget(form) }}
# form.vars.data contains the Presence instance
{% endblock %}
如您所见,我使用表单主题来显示人物的名字和姓氏,但我没有将其添加到我的 PresenceType class,那是因为我不需要名字和姓氏输入字段。
我是 symfony 的新手,我无法找到有关以正确方式显示表单的问题的解决方案。
我有三个表:活动、状态和人员
desc activities;
+----------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+----------+--------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| start | datetime | NO | | NULL | |
| stop | datetime | YES | | NULL | |
| activity | varchar(100) | NO | | NULL | |
| location | varchar(45) | NO | | NULL | |
| event_id | int(11) | YES | MUL | NULL | |
+----------+--------------+------+-----+---------+----------------+
desc presences;
+-------------+----------------------------------------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------------+----------------------------------------------+------+-----+---------+-------+
| activity_id | int(11) | NO | PRI | NULL | |
| person_id | int(11) | NO | PRI | NULL | |
| status | enum('afwezig','aanwezig','verontschuldigd') | NO | PRI | afwezig | |
+-------------+----------------------------------------------+------+-----+---------+-------+
desc persons;
+---------------+----------------+------+-----+-------------------+-----------------------------+
| Field | Type | Null | Key | Default | Extra |
+---------------+----------------+------+-----+-------------------+-----------------------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| firstname | varchar(20) | YES | | NULL | |
| name | varchar(30) | YES | | NULL | |
+----------------+----------------+--------+---- -+--------------------+-------------------------- -+
我创建了 2 个表单类型 一个用于活动
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add(
'start',
'datetime',
array(
'label' => 'Start'
)
)
->add(
'stop',
'datetime',
array(
'label' => 'Stop'
)
)
->add('activity', 'text', array('label' => 'Naam'))
->add('location', 'text', array('label' => 'Locatie'))
->add(
'presences',
'collection',
array(
'type' => new PresencesType()
)
)
->add('save', 'submit', array('label' => 'Aanpassen'));
}
还有一个用于显示某个 activity
的存在 public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add(
'persons',
'entity',
array(
'class' => 'KHOSAdminBundle:Persons',
'label' => false,
'property' => 'fullname',
'read_only' => true,
'disabled' =>true
)
)
->add(
'status',
'choice',
array(
'label' => false,
'multiple' => false,
'expanded' => true,
'choices' => array(
'afwezig' => 'afwezig',
'aanwezig' => 'aanwezig',
'verontschuldigd' => 'verontschuldigd'
)
)
);
}
我可以使用 twig 渲染它,以便它显示 activity 的详细信息,并在存在列表下方显示。
{% for presence in form_activity.presences %}
<div class="row">
<div class="col-md-4">
{{ form_row(presence.persons) }}
</div>
<div class="col-md-6">
{{ form_row(presence.status, { 'style': 'inline' }) }}
</div>
</div>
{% endfor %}
但是所有人都是下拉列表,加载需要很长时间。 但我不想要那个
我只想要 activity 的详细信息,下面是所有具有单选按钮状态的人的列表
Start: <<inputfield>> Stop: <<inputfield>>
location: <<inputfield>>
firstname1 name1 : O afwezig O aanwezig O verontschuldigd
firstname2 name2 : O afwezig O aanwezig O verontschuldigd
firstname3 name3 : O afwezig O aanwezig O verontschuldigd
firstname4 name4 : O afwezig O aanwezig O verontschuldigd
有人可以帮助我吗?
正如我在评论中建议的那样,您应该使用不使用 entity
字段的自定义类型来显示状态(无论如何您都不希望能够编辑 Presence 实例中包含的 Person ,这意味着您不需要 firstName 和 lastName 字段)。
这是 php 部分:
<?php
namespace Demo {
class Activity
{
protected $id;
protected $start;
protected $stop;
/** @var Presence[] */
protected $presences = [];
// ...
}
class Presence {
/** @var Activity */
protected $activity;
/** @var Person */
protected $person;
protected $status;
// ...
}
class Person {
protected $firstName;
protected $lastName;
/** @var Presence[] */
protected $presences;
// ...
}
class ActivityType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('start')
->add('stop')
->add('location')
->add('presences', 'collection', [
'type' => new PresenceType(),
])
;
}
public function getName()
{
return 'activity';
}
}
class PresenceType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('status', 'choice', [
'multiple' => true,
'expanded' => false,
'choices' => [
'afwezig' => 'afwezig',
'aanwezig' => 'aanwezig',
'verontschuldigd' => 'verontschuldigd'
]
])
;
}
public function getName()
{
return 'presence';
}
}
}
和 twig 表单主题(它可能需要一些调整才能实际工作,但想法是定义当您调用 form_row(form) 并且表单是 PresenceType 视图实例时呈现表单视图的方式):
{% block presence_row %}
{{ form.vars.data.user.firstName }} {{ form.vars.data.user.lastName }} {{ form_widget(form) }}
# form.vars.data contains the Presence instance
{% endblock %}
如您所见,我使用表单主题来显示人物的名字和姓氏,但我没有将其添加到我的 PresenceType class,那是因为我不需要名字和姓氏输入字段。