虽然{{ dump($post) }}
可以看到is_published
,但无法访问它。
array:20 [▼
0 => Post {#604 ▼
-id: 1
-title: "The Mouse gave a."
-content: "Ipsum ea accusamus maxime et nemo delectus iste. Ex et corrupti tempora aliquid. In dolor fuga excepturi ut praesentium in quas."
-slug: "the-mouse-gave-a"
-is_published: true
-author: User {#628 ▼
+__isInitialized__: false
-id: 27
-email: null
-roles: []
-password: null
-name: null
-created_at: null
-updated_at: null
-posts: null
…2
}
-published_at: DateTime @1551615126 {#598 ▶}
-created_at: DateTime @1551615126 {#600 ▶}
-updated_at: DateTime @1551615126 {#601 ▶}
}
1 => Post
......
......
......
尝试{{ post.is_published ? 'Yes':'No' }}
,但返回错误
Uncaught PHP Exception Twig_Error_Runtime: "Neither the property "is_published" nor one of the methods "is_published()", "getis_published()"/"isis_published()"/"hasis_published()" or "__call()" exist and have public access in class "App\Entity\Post"
更新: 发布实体
<?php
namespace App\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity(repositoryClass="App\Repository\PostRepository")
*/
class Post
{
/**
* @ORM\Id()
* @ORM\GeneratedValue()
* @ORM\Column(type="integer")
*/
private $id;
/**
* @ORM\Column(type="string", length=255)
*/
private $title;
/**
* @ORM\Column(type="text")
*/
private $content;
/**
* @ORM\Column(type="string", length=255)
*/
private $slug;
/**
* @ORM\Column(type="boolean")
*/
private $is_published;
/**
* @ORM\ManyToOne(targetEntity="App\Entity\User", inversedBy="posts")
* @ORM\JoinColumn(nullable=false)
*/
private $author;
/**
* @ORM\Column(type="datetime", nullable=true)
*/
private $published_at;
/**
* @ORM\Column(type="datetime")
*/
private $created_at;
/**
* @ORM\Column(type="datetime")
*/
private $updated_at;
public function getId(): ?int
{
return $this->id;
}
public function getTitle(): ?string
{
return $this->title;
}
public function setTitle(string $title): self
{
$this->title = $title;
return $this;
}
public function getContent(): ?string
{
return $this->content;
}
public function setContent(string $content): self
{
$this->content = $content;
return $this;
}
public function getSlug(): ?string
{
return $this->slug;
}
public function setSlug(string $slug): self
{
$this->slug = $slug;
return $this;
}
public function getIsPublished(): ?bool
{
return $this->is_published;
}
public function setIsPublished(bool $is_published): self
{
$this->is_published = $is_published;
return $this;
}
public function getAuthor(): ?User
{
return $this->author;
}
public function setAuthor(?User $author): self
{
$this->author = $author;
return $this;
}
public function getPublishedAt(): ?\DateTimeInterface
{
return $this->published_at;
}
public function setPublishedAt(?\DateTimeInterface $published_at): self
{
$this->published_at = $published_at;
return $this;
}
public function getCreatedAt(): ?\DateTimeInterface
{
return $this->created_at;
}
public function setCreatedAt(\DateTimeInterface $created_at): self
{
$this->created_at = $created_at;
return $this;
}
public function getUpdatedAt(): ?\DateTimeInterface
{
return $this->updated_at;
}
public function setUpdatedAt(\DateTimeInterface $updated_at): self
{
$this->updated_at = $updated_at;
return $this;
}
}
答案 0 :(得分:2)
使用
{{ post.ispublished ?'Yes':'No' }}
不是is_published
答案 1 :(得分:0)
您的问题是您的getter和setter没有正确的名称(或者相反,您的参数没有正确的名称)。
好的习惯是将您的参数名称用驼峰式表示。
/**
* @ORM\Column(type="boolean")
*/
private $isPublished;
public function getIsPublished(): ?bool
{
return $this->isPublished;
}
public function setIsPublished(bool $isPublished): self
{
$this->isPublished = $isPublished;
return $this;
}
那样,它应该可以工作。
如果您不想重命名参数,请重命名getter和setter