繁体   English   中英

无法访问树枝模板symfony 4中的布尔值

[英]Unable to access boolean value in twig template 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' }} {{ 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

您的问题是您的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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM