Symfony - 为什么我的表单得到表中存在的每一个答案? [英] Symfony - Why does my form get every answer that exists in the table?
问题描述
这不是一个问题,而是我注意到并希望更好地理解的东西。
我有一个 code> class,其集合
投票
和 pollOptions
。
我的 PollOption
类有它所属的调查
和标题
。
投票
类也有它所属的 poll
以及自己的 voteChoices
和 user
。
VoteChoice
类有它所属的投票
, code>(
PollOption
),以及优先级
。
我有一个表单包含一组表单(一个投票
与许多 VoteChoice
)
VoteChoiceType
如下
class VoteChoiceType extends AbstractType
{
public function buildForm(FormBuilderInterface $ builder,array $ options)
{
$ builder
- > add('answer',null,array('disabled'=> true))
- > add('priority',null);
}
public function configureOptions(OptionsResolver $ resolver)
{
$ resolver-> setDefaults(array(
'data_class'=> PollBundle\Entity\VoteChoice',
));
}
}
现在在我的 VoteController
我创建并填充一个投票
与许多 voteChoices
,设置根据当前民意调查的可用选择(来自URL)
$ vote =新投票();
$ vote-> setPoll($ poll);
foreach($ vote-> getPoll() - > getPollOptions()as $ op){
$ vc = New VoteChoice();
$ vote-> addVoteChoice($ vc-> setAnswer($ op));
}
所以我的树枝如下
< ul class =voteChoicesdata-prototype ={{form_widget(form.voteChoices.vars.prototype)| e('html_attr')}}> ;
{%for formChoice in form.voteChoices%}
< li> {{voteChoice.vars.value.answer}} {{form_row(voteChoice.priority)}}< / li>
{%endfor%}
< / ul>
< / div>
< p>< button type =submitclass =btn btn-success> Go!< / button>< / p>
{{form_end(form)}}
但是,如果我启用答案
在我的 VotechoiceType
中,在我的枝条包括 {{form_row(voteChoice.answer)}}
我注意到所有我的投票选项与可选列表中的表格相同,即使它们是另一个投票。
VoteChoice 和 PollOption
之间的关系(很多到一个)。如果我在这里犯了一个错误,我可能在其他地方(例如一个投票
可能会得到每个投票
在数据库而不是属于它的那些)
$ builder- > add('answer',null,array('disabled'=> true))
为 PollOption
创建一个ChoiceType字段,默认情况下会使用所有PollOption填充它。没有限制选择字段,因为您的表单不知道通过 $ voteChoice-> getVote() - > getPolloptions()
给出的所需限制。 / p>
看看Symfonys EntityType字段: query_builder
选项可能会为您提供一种方法来解决您的问题,方法是将自定义查询添加到答案字段中,该选项将选择更正PollOptions的子集通过 VoteChoice - >投票 - > Polloptions
关系。
This isn't so much an issue but rather something I noticed and would like to better understand.
I have a Poll
class which has a collection of votes
and pollOptions
.
My PollOption
class has the poll
it belongs to and a title
.
The Vote
class also has the poll
it belongs to, as well as its own collection of voteChoices
and user
.
The VoteChoice
class has the vote
it belongs to, the answer
(PollOption
) that the vote is for, and a priority
.
I have a form that contains a collection of forms (a Vote
with many VoteChoice
)
The VoteChoiceType
is as follows
class VoteChoiceType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('answer', null, array('disabled' => true))
->add('priority', null);
}
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'PollBundle\Entity\VoteChoice',
));
}
}
Now in my VoteController
I create and populate a Vote
with many voteChoices
, setting the answer
according to the available choices for the current poll (derived from the URL)
$vote = new Vote();
$vote->setPoll($poll);
foreach ($vote->getPoll()->getPollOptions() as $op) {
$vc = New VoteChoice();
$vote->addVoteChoice($vc->setAnswer($op));
}
So my twig is as follows
<ul class="voteChoices" data-prototype="{{form_widget(form.voteChoices.vars.prototype)|e('html_attr') }}">
{% for voteChoice in form.voteChoices %}
<li>{{ voteChoice.vars.value.answer }} {{ form_row(voteChoice.priority) }}</li>
{% endfor %}
</ul>
</div>
<p><button type="submit" class="btn btn-success">Go!</button></p>
{{ form_end(form) }}
However, if I enable answer
in my VotechoiceType
and in my twig include {{ form_row(voteChoice.answer) }}
I notice that all my poll options from the table as in the selectable list, even if they are apart of another poll.
Why is this? How would I get it to just display options available form the poll the vote belongs to instead? My suspicion is I've not handled the relationship between the VoteChoice
and PollOption
correctly (many to one). If I've made a mistake here, I've likely done it elsewhere (eg a Poll
might be getting every Vote
in the db rather than the ones that belong to it)
$builder->add('answer', null, array('disabled' => true))
creates a ChoiceType-Field for PollOption
and by default it populates it with all PollOption. There is nothing limiting the Choice-Field because your form doesn't know about the desired limitation given through $voteChoice->getVote()->getPolloptions()
.
Take a look at Symfonys EntityType Field: The query_builder
option might give you a way to solve your issue, by adding a custom query to the answer-field, which selects to correct subset of PollOptions through the VoteChoice -> Vote -> Polloptions
relation.
这篇关于Symfony - 为什么我的表单得到表中存在的每一个答案?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!