实体:
class Wheel {
....
/**
* @ORM\ManyToOne(targetEntity="Car")
*/
protected car;
...
}
形式:
class WheelType extends AbstractType {
public function buildForm(FormBuilderInterface $builder, array $options) {
....
$form->add('car', 'entity', array(
'class' => 'namespace\Car',
);
...
控制器:
$form->handleRequest($request);
dump($form->getErrors(true));
exit;
嫩枝:
{{ form_row(form.car) }}
输出:
FormErrorIterator {#1897 ▼
-form: Form {#1861 ▶}
-errors: array:1 [▼
0 => FormError {#2083 ▼
-message: "This value is not valid."
#messageTemplate: "This value is not valid."
#messageParameters: array:1 [▼
"{{ value }}" => "BMW"
]
#messagePluralization: null
-cause: ConstraintViolation {#2094 ▼
-message: "This value is not valid."
-messageTemplate: "This value is not valid."
-parameters: array:1 [▶]
-plural: null
-root: Form {#1861}
-propertyPath: "children[car]"
-invalidValue: "BMW"
-constraint: Form {#1999 ▶}
-code: 1
-cause: TransformationFailedException {#1879 ▼
#message: "Unable to reverse value for property path "car": The choice "BMW" does not exist or is not unique"
如果我理解错误,则表示无法从doctrine
宝马获取PK
对象。或者宝马是唯一的,并且确实存在于实体Car
。
有什么想法吗?我需要使用数据转换器吗?在这种情况下它非常奇怪,因为这曾经在以前的symfony2版本(2.0)中工作
当前版本:symfony 2.7.6