草庐IT

php - Symfony 不能决定哪个类需要

coder 2024-05-03 原文

这是我的错误:

The form's view data is expected to be an instance of class My\Bundle\Entity\Tags, but is an instance of class Doctrine\Common\Collections\ArrayCollection. You can avoid this error by setting the "data_class" option to null or by adding a view transformer that transforms an instance of class Doctrine\Common\Collections\ArrayCollection to an instance of My\Bundle\Entity\Tags

这是我的表单生成器

 $builder
            ->add('name')
            ->add('tags','collection',array(
                        'data_class' => 'My\Bundle\Entity\Tags'
                        )
            )
            ->add('save','submit')
        ;

我将 data_class 更改为 null(仅此),但出现错误:

The form's view data is expected to be of type scalar, array or an instance of \ArrayAccess, **but is an instance of class My\Bundle\Entity\Tags*. You can avoid this error by setting the "data_class" option to "My\Bundle\Entity\Tags" or by adding a view transformer that transforms an instance of class My\Bundle\Entity\Tags to scalar, array or an instance of \ArrayAccess.

我试过用变压器,所以它看起来像这样:

    $transformer = new TagTransformer($this->entityManager);
    $builder
        ->add(
            $builder->create(
                'tags','collection',array(
                    'data_class' => 'My\Bundle\Entity\Tags'
                    )
            )->addModelTransformer($transformer)
        );

和变压器:

public function transform($tag)
{
    if (null === $tag) {
        return "";
    }

    return $tag->toArray();
}

并再次将 data_class 更改为 null。我得到的:

The form's view data is expected to be of type scalar, array or an instance of \ArrayAccess, but is an instance of class My\Bundle\Entity\Tags. You can avoid this error by setting the "data_class" option to "My\Bundle\Entity\Tags" or by adding a view transformer that transforms an instance of class My\Bundle\Entity\Tags to scalar, array or an instance of \ArrayAccess.

当我将 data_class 更改为 My\Bundle\Entity\Tags

The form's view data is expected to be an instance of class My\Bundle\Entity\Tags, but is a(n) array. You can avoid this error by setting the "data_class" option to null or by adding a view transformer that transforms a(n) array to an instance of My\Bundle\Entity\Tags.

嗯..我的意思是...wtf?我究竟做错了什么?我该如何改变它?

编辑:

我的用户实体:

class User
{
    /**
     * @var integer
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

    /**
     * @var string
     *
     * @ORM\Column(name="name", type="string", length=255)
     */
    private $name;

    /**
     * @ORM\ManyToMany(targetEntity="Tags", cascade={"persist"})
     */
    protected $tags;

// methods, etc..
}

最佳答案

所以你得到错误的原因是因为你使用的集合字段类型有点不正确。首先,集合字段类型不支持data_class。当你说

->add('tags','collection',array(
    'data_class' => 'My\Bundle\Entity\Tags'
     )
)

你基本上是在说标签(根据你的声明,它是一个数组集合)实际上是一个标签。如果您查看集合类型的文档,您会注意到 data_class 甚至不是受支持的选项。 http://symfony.com/doc/current/reference/forms/types/collection.html

所以如果你想呈现一个多选标签列表,你正在寻找实体类型,但是这些是标签,如果你有任何类型的体面网站,你可能有比多选列表更多的方式将是实用的。在设计方面,您只想拥有一个自动完成器来显示您键入的文本中已经存在哪些标签,然后让用户按回车键添加标签,无论标签是否存在。然后在自动完成器上方,您将显示已添加的标签,并且旁边有 x ,他们可以按下以删除标签。

您可以通过将表单中的标签字段设置为未映射的文本类型并使用 javascript 将标签组合成表单提交时的字符串来作弊,然后在您的操作中将字符串转换为您的标签。

关于php - Symfony 不能决定哪个类需要,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22119163/

有关php - Symfony 不能决定哪个类需要的更多相关文章

  1. ruby - 我需要将 Bundler 本身添加到 Gemfile 中吗? - 2

    当我使用Bundler时,是否需要在我的Gemfile中将其列为依赖项?毕竟,我的代码中有些地方需要它。例如,当我进行Bundler设置时:require"bundler/setup" 最佳答案 没有。您可以尝试,但首先您必须用鞋带将自己抬离地面。 关于ruby-我需要将Bundler本身添加到Gemfile中吗?,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/4758609/

  2. ruby - rspec 需要 .rspec 文件中的 spec_helper - 2

    我注意到像bundler这样的项目在每个specfile中执行requirespec_helper我还注意到rspec使用选项--require,它允许您在引导rspec时要求一个文件。您还可以将其添加到.rspec文件中,因此只要您运行不带参数的rspec就会添加它。使用上述方法有什么缺点可以解释为什么像bundler这样的项目选择在每个规范文件中都需要spec_helper吗? 最佳答案 我不在Bundler上工作,所以我不能直接谈论他们的做法。并非所有项目都checkin.rspec文件。原因是这个文件,通常按照当前的惯例,只

  3. ruby - 如何在 Lion 上安装 Xcode 4.6,需要用 RVM 升级 ruby - 2

    我实际上是在尝试使用RVM在我的OSX10.7.5上更新ruby,并在输入以下命令后:rvminstallruby我得到了以下回复:Searchingforbinaryrubies,thismighttakesometime.Checkingrequirementsforosx.Installingrequirementsforosx.Updatingsystem.......Errorrunning'requirements_osx_brew_update_systemruby-2.0.0-p247',pleaseread/Users/username/.rvm/log/138121

  4. ruby - 为什么在 ruby​​ 中创建 Rational 不需要新方法 - 2

    这个问题在这里已经有了答案:关闭10年前。PossibleDuplicate:Rubysyntaxquestion:Rational(a,b)andRational.new!(a,b)我正在阅读ruby镐书,我对创建有理数的语法感到困惑。Rational(3,4)*Rational(1,2)产生=>3/8为什么Rational不需要new方法(我还注意到例如我可以在没有new方法的情况下创建字符串)?

  5. ruby - 正则表达式在哪个位置失败? - 2

    我需要一个非常简单的字符串验证器来显示第一个符号与所需格式不对应的位置。我想使用正则表达式,但在这种情况下,我必须找到与表达式相对应的字符串停止的位置,但我找不到可以做到这一点的方法。(这一定是一种相当简单的方法……也许没有?)例如,如果我有正则表达式:/^Q+E+R+$/带字符串:"QQQQEEE2ER"期望的结果应该是7 最佳答案 一个想法:你可以做的是标记你的模式并用可选的嵌套捕获组编写它:^(Q+(E+(R+($)?)?)?)?然后你只需要计算你获得的捕获组的数量就可以知道正则表达式引擎在模式中停止的位置,你可以确定匹配结束

  6. ruby-on-rails - 需要帮助最大化多个相似对象中的 3 个因素并适当排序 - 2

    我需要用任何语言编写一个算法,根据3个因素对数组进行排序。我以度假村为例(如Hipmunk)。假设我想去度假。我想要最便宜的地方、最好的评论和最多的景点。但是,显然我找不到在所有3个中都排名第一的方法。Example(assumingthereare20importantattractions):ResortA:$150/night...98/100infavorablereviews...18of20attractionsResortB:$99/night...85/100infavorablereviews...12of20attractionsResortC:$120/night

  7. ruby - 为什么不能使用类IO的实例方法noecho? - 2

    print"Enteryourpassword:"pass=STDIN.noecho(&:gets)puts"Yourpasswordis#{pass}!"输出:Enteryourpassword:input.rb:2:in`':undefinedmethod`noecho'for#>(NoMethodError) 最佳答案 一开始require'io/console'后来的Ruby1.9.3 关于ruby-为什么不能使用类IO的实例方法noecho?,我们在StackOverflow上

  8. ruby - 我需要从 facebook 游戏中抓取数据——使用 ruby - 2

    修改(澄清问题)我已经花了几天时间试图弄清楚如何从Facebook游戏中抓取特定信息;但是,我遇到了一堵又一堵砖墙。据我所知,主要问题如下。我可以使用Chrome的检查元素工具手动查找我需要的html-它似乎位于iframe中。但是,当我尝试抓取该iframe时,它​​是空的(属性除外):如果我使用浏览器的“查看页面源代码”工具,这与我看到的输出相同。我不明白为什么我看不到iframe中的数据。答案不是它是由AJAX之后添加的。(我知道这既是因为“查看页面源代码”可以读取Ajax添加的数据,也是因为我有b/c我一直等到我可以看到数据页面之后才抓取它,但它仍然不存在)。发生这种情况是因为

  9. ruby - 需要重构为新的 Ruby 1.9 哈希语法 - 2

    这个问题在这里已经有了答案:HashsyntaxinRuby[duplicate](1个回答)关闭5年前。我有一个Recipe,其中包含以下未通过lint测试的代码:service'apache'dosupports:status=>true,:restart=>true,:reload=>trueend失败并出现错误:UsethenewRuby1.9hashsyntax.supports:status=>true,:restart=>true,:reload=>true不确定新语法是什么样的...有人可以帮忙吗?

  10. ruby-on-rails - 我真的需要在 Rails 中使用 csv gem 吗? - 2

    我的问题很简单:我是否必须在使用RubyonRails的类上require'csv'?如果我打开一个railsconsole并尝试使用CSVgem它可以工作,但我必须在文件中这样做吗? 最佳答案 CSVlibrary是ruby​​标准库的一部分;它不是gem(即第三方库)。与所有标准库(与核心库不同)一样,csv不会由ruby​​解释器自动加载。所以是的,在您的应用程序中某处您确实需要要求它:irb(main):001:0>CSVNameError:uninitializedconstantCSVfrom(irb):1from/Us

随机推荐