使用 Symfony/Doctrine 设置的数据类型的输出值

问题描述 投票:0回答:0

Synfony 6
学说 3

我的桌子:

CREATE TABLE `isbn` (
    `isbn` BIGINT(20) UNSIGNED NOT NULL,
    `state` SET('Free','Used','Dead') NOT NULL DEFAULT 'Free' COLLATE 'utf8_general_ci',
    PRIMARY KEY (`isbn`) USING BTREE,
    UNIQUE INDEX `isbn` (`isbn`) USING BTREE
)
COLLATE='utf8_general_ci'
ENGINE=InnoDB
ROW_FORMAT=DYNAMIC
;

我的实体类

<?php

namespace App\Entity;

use Doctrine\DBAL\Types\Types;
use Doctrine\ORM\Mapping as ORM;

/**
 * Isbn
 *
 * @ORM\Table(name="isbn", uniqueConstraints={@ORM\UniqueConstraint(name="isbn", columns={"isbn"})})
 * @ORM\Entity
 */
class Isbn
{
    /**
     * @var int
     *
     * @ORM\Column(name="isbn", type="bigint", nullable=false, options={"unsigned"=true})
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="IDENTITY")
     */
    private $isbn;

    /**
     * @var array
     *
     * @ORM\Column(name="state", type="simple_array", length=0, nullable=false, options={"default"="Free"})
     */
    private $state = 'Free';

    public function getIsbn(): ?string
    {
        return $this->isbn;
    }

    public function getState(): array
    {
        return $this->state;
    }

    public function setState(array $state): self
    {
        $this->state = $state;

        return $this;
    }


}

示例数据:


isbn 状态
9783943466003 二手

我的 CRUD 控制器

<?php

namespace App\Controller;

use App\Entity\Isbn;
use App\Form\IsbnType;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;

#[Route('/isbn/')]
class IsbnController extends AbstractController
{
    #[Route('/', name: 'app_isbn_index', methods: ['GET'])]
    public function index(EntityManagerInterface $entityManager): Response
    {
        $isbnDummies = $entityManager
            ->getRepository(Isbn::class)
            ->findAll();

        return $this->render('isbn/index.html.twig', [
            'isbns' => $isbns,
        ]);
    }

    #[Route('/new', name: 'app_isbn_new', methods: ['GET', 'POST'])]
    public function new(Request $request, EntityManagerInterface $entityManager): Response
    {
        $isbn = new Isbn();
        $form = $this->createForm(IsbnType::class, $isbn);
        $form->handleRequest($request);

        if ($form->isSubmitted() && $form->isValid()) {
            $entityManager->persist($isbn);
            $entityManager->flush();

            return $this->redirectToRoute('app_isbn_index', [], Response::HTTP_SEE_OTHER);
        }

        return $this->renderForm('isbn/new.html.twig', [
            'isbn' => $isbn,
            'form' => $form,
        ]);
    }

    #[Route('/{isbn}', name: 'app_isbn_show', methods: ['GET'])]
    public function show(Isbn $isbn): Response
    {
        return $this->render('isbn/show.html.twig', [
            'isbn' => $isbn,
        ]);
    }

    #[Route('/{isbn}/edit', name: 'app_isbn_edit', methods: ['GET', 'POST'])]
    public function edit(Request $request, Isbn $isbn, EntityManagerInterface $entityManager): Response
    {
        $form = $this->createForm(IsbnType::class, $isbn);
        $form->handleRequest($request);

        if ($form->isSubmitted() && $form->isValid()) {
            $entityManager->flush();

            return $this->redirectToRoute('app_isbn_index', [], Response::HTTP_SEE_OTHER);
        }

        return $this->renderForm('isbn/edit.html.twig', [
            'isbn' => $isbn,
            'form' => $form,
        ]);
    }

    #[Route('/{isbn}', name: 'app_isbn_delete', methods: ['POST'])]
    public function delete(Request $request, Isbn $isbn, EntityManagerInterface $entityManager): Response
    {
        if ($this->isCsrfTokenValid('delete'.$isbn->getIsbn(), $request->request->get('_token'))) {
            $entityManager->remove($isbn);
            $entityManager->flush();
        }

        return $this->redirectToRoute('app_isbn_index', [], Response::HTTP_SEE_OTHER);
    }
}

我的树枝模板:

{% extends 'base.html.twig' %}

{% block title %}Isbn index{% endblock %}

{% block body %}
    <h1>Isbn index</h1>

    <table class="table">
        <thead>
            <tr>
                <th>Isbn</th>
                <th>State</th>
                <th>actions</th>
            </tr>
        </thead>
        <tbody>
        {% for isbn in isbns %}
            <tr>
                <td>{{ isbn.isbn }}</td>
                <td>{{ isbn.state }}</td>
                <td>
                    <a href="{{ path('app_isbn_show', {'isbn': isbn.isbn}) }}">show</a>
                    <a href="{{ path('app_isbn_edit', {'isbn': isbn.isbn}) }}">edit</a>
                </td>
            </tr>
        {% else %}
            <tr>
                <td colspan="3">no records found</td>
            </tr>
        {% endfor %}
        </tbody>
    </table>

    <a href="{{ path('app_isbn_new') }}">Create new</a>
{% endblock %}

错误:

模板渲染过程中抛出异常 (“警告:数组到字符串的转换”)。

                <td>{{ isbn.state }}</td>

isbn 数组转储

0 => App\Entity\Isbn {#1 ▼
-国际标准书号:“9783943466003”
-state: array:1 [ …1] }

我的问题:怎样才能不报错地输出值“Used”?

php symfony doctrine
© www.soinside.com 2019 - 2024. All rights reserved.