我正在处理一个带有非实体字段的Symfony表单(在集合中)。
(编辑:下面的所有代码现在都运行良好!)
我们在说什么
为了便于理解,下面是一些代码片段:表单定义(在PieceType类中):
// Form'Type'PieceType.php
$builder
->add('name',
'text',
array('max_length' => 70,
'label' => "piece.name"))
->add('compositors',
'collection',
array('type' => 'entity',
'options' => array('class' => 'kalisonStdArchiveBundle:Person',
'property_path' => false),
'allow_add' => true,
'allow_delete' => true,
'property_path' => false,
'by_reference' => false,
'label' => 'piece.compositor'
));
compositors字段是使用javascript添加的,使用的是数据原型(此处解释)。
渲染只需使用以下视图即可完成:
// Resources'views'Piece'piece.new.html.twig
{% block javascripts %}
{{ parent() }}
<script type="text/javascript">var nbCompositors = {{ form.compositors|length }}</script>
<script type="text/javascript" src="{{ asset('bundles/kalisonstdarchive/js/piece.new.js') }}"></script>
{% endblock javascripts %}
{% block main %}
<form novalidate action="{{ path("kalisonStdArchiveBundle_piece_new") }}" method="post" {{ form_enctype(form) }}>
{{ form_widget(form) }}
<button id="add-compositor">{{ "compositor.add"|trans }}</button>
<input type="submit" id="submit" value="{{ "piece.create"|trans }}" name="submit" />
</form>
{% endblock %}
关联的javascript是:
// Resources'public'js'piece.new.js
$(document).ready(function() {
jQuery('#add-compositor').click(function() {
var list = jQuery('#kalison_stdarchivebundle_piecetype_compositors');
var newCompositor = list.attr('data-prototype');
newCompo = newCompositor.replace(/'$'$name'$'$/g, nbCompositors);
nbCompositors++;
var html = jQuery('<p></p>').html(newCompositor);
html.appendTo(jQuery('#kalison_stdarchivebundle_piecetype_compositors'));
return false;
});
});
这是管理数据的操作。正如@l3l0所通知的,我没有检查上一次迭代,这导致$addCompositor为null:
// Controller'PieceController.php
$newPiece = new Piece();
$form = $this->get('form.factory')->create(new PieceType(), $newPiece);
if ('POST' == $request->getMethod()) {
$form->bindRequest($request);
if ($form->isValid()) {
$this->get('piece_manager')->savePiece($newPiece);
if ($form["compositors"] != null) {
foreach ($form["compositors"] as $formCompositor) {
$addCompositor = $formCompositor->getData();
if ($addCompositor != null) {
$newPerfomance = new Performance();
$newPerfomance->setPerson($addCompositor); // Throw Catchable Fatal Error
$newPerfomance->setPiece($newPiece);
$newPerfomance->setInstrument($instrument);
$this->get('performance_manager')->savePerformance($newPerfomance);
}
}
}
}
}
我的问题是我得到了一个Symfony错误,告诉它:Argument 1 passed to Performance::setPerson() must be an instance of Person, null given
。(当然,Performance类中的方法是public function setPerson(Person $person)
)
我尝试过的
我试着确认:
if (!is_object($addCompositor)) throw new Exception('NOT AN OBJECT');
是的,
$addCompositor
实际上是一个物体。我假设它是一个不兼容的对象,但如果尝试打印对象:
var_dump($addCompositor)
。我得到
object(...'Entity'Person)
并不感到惊讶最后,我的最后一个测试是在这个对象上调用一个方法:
echo $addCompositor->getLastName();
。同样有效,打印了名称,然后抛出了这个众所周知的错误:
Call to a member function getLastName() on a non-object
。
所以,我的代码现在看起来是这样的:
foreach ($form["compositors"] as $formCompositor) {
$addCompositor = $formCompositor->getData();
if (!is_object($addCompositor))
throw new Exception('NOT AN OBJECT'); // Not throwing: it's an object
var_dump($addCompositor); // Print a Person object
echo $addCompositor->getLastName(); // Print the name, then break on "non-object" error
$newPerfomance = new Performance();
$newPerfomance->setPerson($addCompositor); // Throw Catchable Fatal Error
$newPerfomance->setPiece($newPiece);
$newPerfomance->setInstrument($instrument);
$this->get('performance_manager')->savePerformance($newPerfomance);
}
你的神现在在哪里
起初我以为这是一个Symfony错误,但最后一次测试让我有点困惑。你曾经面对过吗?
在接下来的迭代中,您似乎没有数据对象。
请检查提交的数据,并向我们展示JS代码。你也可以检查$form["compositors"]
中的
啊,你能说出你用的是哪种版本的Symfony吗?(2.0.x,2.1?)