【问题标题】:Save symfony2 entity form with entity field用实体字段保存 symfony2 实体表单
【发布时间】:2013-03-13 11:36:40
【问题描述】:

我有一个城市添加表单:

public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder->add('name', null, array(
            'label' => 'City',
        ));
        $builder->add('country', 'genemu_jqueryautocompleter_entity', array(
            'class' => 'X\tBundle\Entity\Country',
            'property' => 'name',
            'route_name' => 'ajax_country',
            'required' => true,
            'label' => 'Country',
        ));
    }

国家是一个实体。当我提交此表单时 - S2 返回错误:cannot save because cannot convert Object to String

为此,我使用魔术方法__toString(),它返回(string)$this->getId()

但我不确定 - 这是对的吗?

决定: Symfony/Doctrine: "Catchable Fatal Error: Object of class <type> could not be converted to string" when persisting

【问题讨论】:

  • 您能向我们展示您的实体吗?你的表之间有链接吗?
  • 在标题中不要写 solved,你能发布一个答案并接受它吗?
  • @j0k,我无法在 6 楼后发布我的问题的答案,所以我在标题中写了 Solved。并将决策链接设置为内容。
  • @Salavat 我们不使用标题来表示问题的问题已解决。接受答案是正确的方法。如果你还不能这样做也没关系(尽管我希望你现在可以);时机成熟时就去做。与此同时,如果你得到更多的答案也没关系。这些也可能对其他人有所帮助。
  • @AndrewBarber,感谢您的详细评论。

标签: ajax forms symfony


【解决方案1】:

最好的办法是为您的 Country 实体创建一个 FormType 并将其嵌入到主窗体中。请参阅 Cookbook 中的 How to Embed a Collection of Forms,了解具体如何执行此操作。

您尝试做的事情是可能的,但它会让事情很快变得一团糟。

【讨论】:

    【解决方案2】:

    回答:Symfony/Doctrine: "Catchable Fatal Error: Object of class <type> could not be converted to string" when persisting

    所以,我们使用下一件事:

    /**
     * @var X\tBundle\Entity\Country;
     *
     * @ORM\ManyToOne(targetEntity="X\tBundle\Entity\Country")
     * @ORM\JoinColumn(name="country_id", referencedColumnName="id", nullable=false)
     */
    public $country;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-06-22
      • 2012-11-10
      • 2015-02-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多