【问题标题】:Sonata AdminBundle with a Child cannot be renderedSonata AdminBundle with a Child 无法呈现
【发布时间】:2015-01-01 20:14:46
【问题描述】:

我正在尝试配置 Sonata AdminBundle。它是一个非常有趣的捆绑包,具有许多功能,但使用起来并不简单。我有一个 Post 实体,因此我可以调整帖子,如文档手册中所述。我想为每个帖子的 cmets 实现一个子管理员(多对一关系)。我将它实现为服务和变量的 __toString() 方法,但是我收到以下错误:

在渲染模板期间引发了异常 (“可捕获的致命错误:类的对象 Doctrine\ORM\PersistentCollection 无法转换为字符串") 在 SonataDoctrineORMAdminBundle:CRUD:list_orm_many_to_one.html.twig 中 第 17 行。

我不明白为什么不能将 ManyToOne 变量的内容转换为字符串。任何帮助在这里表示赞赏。

这里是 Post 实体代码:

<?php

namespace Blog\BlogBundle\Entity;

class Post

/**
 * @var ArrayCollection
 *
 * @ORM\OneToMany(targetEntity="Comment", mappedBy="post", cascade={"remove"})
 */
private $comments;

/**
* Construct DateTime and Comments Array
*/
public function __construct()
{
    $this->createdAt = new \DateTime();

    $this->comments = new ArrayCollection();
}

/**
 * @return mixed
 */
public function __toString()
{
    return $this->comments;
}

Other private and setters and getters

【问题讨论】:

    标签: php symfony doctrine twig sonata-admin


    【解决方案1】:

    删除

    /**
     * @return mixed
     */
    public function __toString()
    {
        return $this->comments;
    }
    

    从您的 Post 实体并将其添加到您的 cmets 实体,但将 $this->cmets 更改为用于评论正文的字段。

    【讨论】:

      【解决方案2】:

      国际海事组织:

      /**
       * @return mixed
       */
       public function __toString()
       {
           return $this->comments;
       }
      

      这个方法必须返回一个字符串而不是一个数组集合

      【讨论】:

      • 因为 __toString() 方法必须在您的评论实体中,而不是在帖子中。
      • 我已经在 Post 和 Comment 实体上实现了 __toString() 并且仍然得到相同的错误,即使我只有评论实体上的方法。请问还有什么其他建议吗?
      猜你喜欢
      • 2013-10-27
      • 2010-09-23
      • 2019-01-12
      • 2022-01-02
      • 2016-10-02
      • 2012-09-29
      • 2021-06-29
      • 1970-01-01
      • 2013-07-23
      相关资源
      最近更新 更多