关于php:在渲染模板的过程中抛出了异常(“A”__toString()”

An exception has been thrown during the rendering of a template (“A “__toString()”

我在继续操作时遇到了这个错误
tutorial.symblog.co.uk[第 4 部分]评论模型:添加评论、Doctrine 存储库和迁移]

An exception has been thrown during the rendering of a template (“A”__toString()” method was not found on the objects of type”Blogger//BlogBundle//Entity//Blog” passed to the choice field. To read a custom getter instead, set the option”property” to the desired property path.”)
in BloggerBlogBundle:Blog:show.html.twig at line 23.

有人对此有所了解吗?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
// src/Blogger/BlogBundle/Entity/Blog.php
namespace Blogger//BlogBundle//Entity;
use Doctrine//ORM//Mapping as ORM;
use Doctrine//Common//Collections//ArrayCollection;
/**
 * @ORM//Entity(repositoryClass="Blogger//BlogBundle//Entity/
epository//BlogRepositorya€?a€?")
 * @ORM//Table(name="blog")
 * @ORM//HasLifecycleCallbacks()
 */

class Blog {
    //……….
    public function __toString() {
        return $this->getTitle();
    }
}


只需在 Blog 实体中添加 __toString() 方法

1
2
3
4
5
6
7
8
class Blog {

    //…

    function __toString() {
        return $this->getTitle(); // or any other field
    }
}


原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/268530.html

(0)
上一篇 2022年6月20日
下一篇 2022年6月20日

相关推荐

发表回复

登录后才能评论