簡體   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