简体   繁体   English

Symfony 5:在属性路径“myEntity”中给出的“字符串”、“对象”类型的预期参数

[英]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' ).我的目标是让该字段返回所选MyEntityname ,而不是整个实体(因此,与'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.

相关问题 “类型为“int 或 null”的预期参数,在属性路径中给出的“对象” - "Expected argument of type "int or null", "object" given at property path 给定Symfony的预期类型为“字符串”,“ DateTime”的参数 - Expected argument of type “string”, “DateTime” given Symfony Symfony:属性路径中给出的“?Doctrine\Common\Collections\Collection”类型的预期参数,“array” - Symfony: Expected argument of type "?Doctrine\Common\Collections\Collection", "array" given at property path Symfony“类型”约束:给定类型“字符串”,“数组”的预期参数 - Symfony “Type” constraint: Expected argument of type “string”, “array” given exception CollectionType 类型为“字符串或空”的预期参数,在属性路径“cc”中给出的“数组” - CollectionType expected argument of type "string or null", "array" given at property path "cc" 在属性路径“user”中给出的类型为“?App\Entity\User”的预期参数,“string” - Expected argument of type "?App\Entity\User", "string" given at property path "user" Symfony形式-给定类型为“字符串”,“ DateTime”的预期参数 - Symfony forms - Expected argument of type “string”, “DateTime” given Symfony从2.2升级到2.3预期参数类型为“string或Symfony \\ Component \\ PropertyAccess \\ PropertyPath”,“boolean”给出 - Symfony upgrade from 2.2 to 2.3 Expected argument of type “string or Symfony\Component\PropertyAccess\PropertyPath”, “boolean” given Symfony Form - 类型为“string或Symfony \\ Component \\ Form \\ FormTypeInterface”的预期参数,给出“数组” - Symfony Form - Expected argument of type “string or Symfony\Component\Form\FormTypeInterface”, “array” given 给定Symfony 3.0的预期类型为“字符串”,“ Vendor \\ NameBundle \\ Form \\ EntitynameType”的参数 - Expected argument of type “string”, “Vendor\NameBundle\Form\EntitynameType” given Symfony 3.0
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM