猿问

如何在 Symfony 实体中从数组中进行选择

我是 symfony 的新手,还在学习,我的问题是如何在一个包含静态选项数组的表单中填充选择下拉列表。假设我有一个名为 Cake 的类,我希望能够从在同一个 CakeEntity 中创建status的Cake数组中填充一个下拉列表:statuses


<?php


namespace App\Entity;


use Doctrine\ORM\Mapping as ORM;


/**

 * @ORM\Entity(repositoryClass="App\Repository\CakeRepository")

 */

class Cake

{

    /**

     * @ORM\Column(type="string", length=50)

     */


    private $status;


    private $statuses = array(

        'not_ready' => 'Not Ready',

        'almost_ready' => 'Almost Ready',

        'ready'=>'Ready',

        'too_late'=>'Too late'

    );

    public function getStatus(): ?string

    {

        return $this->status;

    }


    public function setStatus(string $status): self

    {

        $this->status = $status;

        return $this;

    }


    public function getStatuses()

    {

       return $this->statuses;

    }

}

我的控制器看起来像:


namespace App\Controller;


use App\Entity\Cake;

use App\Form\CakeType;

use App\Repository\CakeRepository;

use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;

use Symfony\Component\HttpFoundation\Request;

use Symfony\Component\HttpFoundation\Response;

use Symfony\Component\Routing\Annotation\Route;



/**

 * @Route("/cake")

 */

class CakeController extends AbstractController

{

    /**

     * @Route("/new", name="cake_new", methods={"GET","POST"})

     */

    public function new(Request $request): Response

    {

        $cake = new Cake();

        $form = $this->createForm(CakeType::class, $cake);

        $form->handleRequest($request);


        if ($form->isSubmitted() && $form->isValid()) {


            $cake->setCreatedAt(\DateTime::createFromFormat('d-m-Y', date('d-m-Y')));

            $cake->setCreatedBy(1);

            $entityManager = $this->getDoctrine()->getManager();

            $entityManager->persist($cake);

            $entityManager->flush();


            return $this->redirectToRoute('cake_index');

        }


        return $this->render('cake/new.html.twig', [

            'cake' => $cake,

            'form' => $form->createView(),

        ]);

    }


郎朗坤
浏览 127回答 1
1回答

哆啦的时光机

您可以声明getStatuses为Cake,static或使用公共常量。例如:class Cake{&nbsp; &nbsp; // with static variables&nbsp; &nbsp; private static $statuses = [&nbsp; &nbsp; &nbsp; &nbsp; 'not_ready'&nbsp; &nbsp; => 'Not Ready',&nbsp; &nbsp; &nbsp; &nbsp; 'almost_ready' => 'Almost Ready',&nbsp; &nbsp; &nbsp; &nbsp; 'ready'&nbsp; &nbsp; &nbsp; &nbsp; => 'Ready',&nbsp; &nbsp; &nbsp; &nbsp; 'too_late'&nbsp; &nbsp; &nbsp;=> 'Too late',&nbsp; &nbsp; ];&nbsp; &nbsp; public static function getStatuses()&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; return self::$statuses;&nbsp; &nbsp; }&nbsp; &nbsp; // or with public const&nbsp; &nbsp; public const STATUSES = [&nbsp; &nbsp; &nbsp; &nbsp; 'not_ready'&nbsp; &nbsp; => 'Not Ready',&nbsp; &nbsp; &nbsp; &nbsp; 'almost_ready' => 'Almost Ready',&nbsp; &nbsp; &nbsp; &nbsp; 'ready'&nbsp; &nbsp; &nbsp; &nbsp; => 'Ready',&nbsp; &nbsp; &nbsp; &nbsp; 'too_late'&nbsp; &nbsp; &nbsp;=> 'Too late',&nbsp; &nbsp; ];}这似乎是合理的,因为返回值不是实例而是特定于类的。然后你可以使用:public function buildForm(FormBuilderInterface $builder, array $options){&nbsp; &nbsp; $builder->add('status', ChoiceType::class, [&nbsp; &nbsp; &nbsp; &nbsp; 'choices'=> Cake::getStatuses(),&nbsp; &nbsp; ]);&nbsp; &nbsp; // or&nbsp; &nbsp; $builder->add('status', ChoiceType::class, [&nbsp; &nbsp; &nbsp; &nbsp; 'choices'=> Cake::STATUSES,&nbsp; &nbsp; ]);}如果选择实际上取决于给定的 Cake 实例,您可以通过选项数组或使用表单事件传递它。
随时随地看视频慕课网APP
我要回答