php - 无法访问树枝模板 symfony 4 中的布尔值
问题描述
虽然{{ 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;
}
}
解决方案
利用
{{ post.ispublished ?'Yes':'No' }}
不是is_published
推荐阅读
- angular - 如何在Angular的另一个模块中更改组件的模型?
- python - 使用 Python 一次性从邮件附件中提取 ZIP 文件中的 CSV 文件
- java - 使用 Spring Roo 在 Crud 屏幕上显示 ID
- c# - Http post请求以图像格式发送表单数据
- java - 一旦推送通知到达android,如何更改家庭活动中的通知图标
- reactjs - 附加到传播属性的正确反应语法是什么
- webpack - 我为这两种文件类型添加了 LESS 和 CSS 加载器。这是 webpack 的开销吗?
- oracle - 根据Oracle中的日期指定条件获取数据
- javascript - 需要将内容占位符 id 与 javascript 代码的 div id 连接起来
- mysql - UPDATE 触发器导致死锁 - Mysql 5.6