[英]Symfony 5: Expected argument of type “string”, “object” given at property path “myEntity”
I am having an issue with the EntityType class in Symfony 5 where I cannot find a way to make the form row return a string
instead of an entity ( MyEntity
).我在 Symfony 5 中遇到 EntityType class 的问题,我找不到使表单行返回
string
而不是实体 ( MyEntity
) 的方法。 This is what I basically have as part of the form builder:作为表单构建器的一部分,这基本上是我所拥有的:
$builder->add('myEntity', EntityType::class, [
'class' => MyEntity::class,
'placeholder' => "(Please select an option.)",
'choice_label' => 'name'
])
My aim is to have this field return the name
for the MyEntity
selected, not the entire entity (so, the exact same value as the 'choice_label'
).我的目标是让该字段返回所选
MyEntity
的name
,而不是整个实体(因此,与'choice_label'
值完全相同)。 I've already tried adding a 'choice_value' => 'name'
attribute (with a 'mapped' => false
attribute, which is apparently necessary in that case), but to no avail.我已经尝试添加
'choice_value' => 'name'
属性(带有'mapped' => false
属性,在这种情况下这显然是必要的),但无济于事。 More precisely, the form row starts returning null
rather than a string
for any entity.更准确地说,表单行开始返回
null
而不是任何实体的string
。
How would I be able to achieve my goal?我将如何实现我的目标?
It turns out that defining the __toString()
function in MyEntity
as returning the name
field (the exact same way you would define getName()
) fixed it, and I now get no errors plus a successful submission to the DB.事实证明,在
MyEntity
中将__toString()
function定义为返回name
字段(与定义getName()
的方式完全相同)修复了它,现在我没有收到任何错误,而且成功提交到数据库。
public function __toString() {
return $this->name;
}
(In case you're wondering why just this addition and nothing else fixes it, it's because __toString
is already being used by default in some pre-existing entity-related code to express an entity in a simpler and custom way, if I'm not mistaken. Now that I made this function available, it's going to go to it and safely assume that when my entity might need to be expressed in a simpler [string] format, __toString
should be called to "transform" MyEntity
into a meaningful string, such as its name
. I guess all that is to say that for instance, you won't refer to a car by all of its internal parts, but rather by just its model.) (如果您想知道为什么只有这个添加而没有其他修复它,这是因为
__toString
已经在一些预先存在的实体相关代码中默认使用,以更简单和自定义的方式表达实体,如果我是没记错。现在我使这个 function 可用,它会转到 go 并安全地假设当我的实体可能需要以更简单的 [string] 格式表示时,应该调用__toString
以将MyEntity
“转换”为有意义的字符串,比如它的name
。我想这就是说,例如,你不会用它的所有内部部件来指代汽车,而只是用它的 model 来指代汽车。)
I'm glad you found the right solution, but it didn't work for me.我很高兴您找到了正确的解决方案,但它对我不起作用。 I use the EntityType in its own Type which is called in a CollectionType (yes, something special).
我在自己的类型中使用实体类型,该类型在 CollectionType 中调用(是的,有些特殊)。 To solve the problem for me, I used the DataTransformer, as follows:
为了解决我的问题,我使用了 DataTransformer,如下:
class ProductTextTransformer implements DataTransformerInterface
{
public function reverseTransform($text)
{
if (null === $text) {
return '';
}
return $text->getName();
}
public function transform($name)
{
// NOTE: Should not Called!
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.