我在一个项目中看到 property-path可以与方括号一起使用,如下所示:
foreach ($foo as $i => $object) {
$builder->add("foo_{$i}", 'checkbox', array(
'property_path' => "[$i].foo",
));
}
这似乎将每个字段与给定的 id
对象的 foo
属性相关联,但我还没有找到任何有关它的文档。仅this example 。
知道它是什么意思以及它记录在哪里吗?
最佳答案
这就是 Symfony UPGRADE from 2.0 to 2.1说:
The mapping of property paths to arrays has changed.
Previously, a property path
"street"
mapped to both a field$street
of a class (or its accessorsgetStreet()
andsetStreet()
) and an index['street']
of an array or an object implementing\ArrayAccess
.Now, the property path
"street"
only maps to a class field (or accessors), while the property path"[street]"
only maps to indices.
另请参阅 Symfony 的有关 the PropertyAccess component 的文档。 .
property_path
的目的在 FormType Field 中进行了解释页面,但我想您已经知道它的用途了。
对于您发布的代码,我猜想(我只使用过 Symfony 表单一次,只是触及了表面) 对于每个属性 bar
对象 $foo
它创建了一个 <input type="checkbox" name="foo_bar">
.
提交表单后,property_path
告诉表单输入名为 foo_bar
的复选框的值进入$data['bar']->foo
哪里$data
是方法 getData()
返回的对象的形式。
关于php - property_path Symfony 表单选项中方括号的含义是什么,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35729015/