Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/244.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 添加带有Symfony中关系值的selectbox_Php_Forms_Symfony - Fatal编程技术网

Php 添加带有Symfony中关系值的selectbox

Php 添加带有Symfony中关系值的selectbox,php,forms,symfony,Php,Forms,Symfony,我是一个Symfony初学者,我渴望了解更多关于Symfony的知识,这就是为什么我开始了一个学习项目 我有一个多对一关系(类型有许多游戏,但游戏有一个类型),我想显示一个选择框,其中有一个“类型”列表可供选择 我得到这个错误: Could not load type "AppBundle\Form\choiceType" 这是我的代码: GameController.php <?php namespace AppBundle\Controller; use AppBundle\Ent

我是一个Symfony初学者,我渴望了解更多关于Symfony的知识,这就是为什么我开始了一个学习项目

我有一个多对一关系(类型有许多游戏,但游戏有一个类型),我想显示一个选择框,其中有一个“类型”列表可供选择

我得到这个错误:

Could not load type "AppBundle\Form\choiceType"
这是我的代码:

GameController.php

<?php
namespace AppBundle\Controller;

use AppBundle\Entity\Game;
use AppBundle\Entity\Type;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;use Symfony\Component\HttpFoundation\Request;

/**
 * Game controller.
 *
 * @Route("game")
 */
class GameController extends Controller
{
    /**
     * Lists all game entities.
     *
     * @Route("/", name="game_index")
     * @Method("GET")
     */
    public function indexAction()
    {
        $em = $this->getDoctrine()->getManager();

        $games = $em->getRepository('AppBundle:Game')->findAll();

        return $this->render('game/index.html.twig', array(
            'games' => $games,
        ));
    }

    /**
     * Creates a new game entity.
     *
     * @Route("/new", name="game_new")
     * @Method({"GET", "POST"})
     */
    public function newAction(Request $request)
    {

        $type = new Type();
        $type->setName('test');

        $game = new Game();

        $game->setType($type);

        $form = $this->createForm('AppBundle\Form\GameType', $game);
        $form->handleRequest($request);

        if ($form->isSubmitted() && $form->isValid()) {
            $em = $this->getDoctrine()->getManager();
            $em->persist($type);
            $em->persist($game);
            $em->flush($game);

            return $this->redirectToRoute('game_show', array('id' => $game->getId()));
        }

        return $this->render('game/new.html.twig', array(
            'game' => $game,
            'form' => $form->createView(),
        ));
    }

    /**
     * Finds and displays a game entity.
     *
     * @Route("/{id}", name="game_show")
     * @Method("GET")
     */
    public function showAction(Game $game)
    {
        $deleteForm = $this->createDeleteForm($game);

        return $this->render('game/show.html.twig', array(
            'game' => $game,
            'delete_form' => $deleteForm->createView(),
        ));
    }

    /**
     * Displays a form to edit an existing game entity.
     *
     * @Route("/{id}/edit", name="game_edit")
     * @Method({"GET", "POST"})
     */
    public function editAction(Request $request, Game $game)
    {
        $deleteForm = $this->createDeleteForm($game);
        $editForm = $this->createForm('AppBundle\Form\GameType', $game);
        $editForm->handleRequest($request);

        if ($editForm->isSubmitted() && $editForm->isValid()) {
            $this->getDoctrine()->getManager()->flush();

            return $this->redirectToRoute('game_edit', array('id' => $game->getId()));
        }

        return $this->render('game/edit.html.twig', array(
            'game' => $game,
            'edit_form' => $editForm->createView(),
            'delete_form' => $deleteForm->createView(),
        ));
    }

    /**
     * Deletes a game entity.
     *
     * @Route("/{id}", name="game_delete")
     * @Method("DELETE")
     */
    public function deleteAction(Request $request, Game $game)
    {
        $form = $this->createDeleteForm($game);
        $form->handleRequest($request);

        if ($form->isSubmitted() && $form->isValid()) {
            $em = $this->getDoctrine()->getManager();
            $em->remove($game);
            $em->flush($game);
        }

        return $this->redirectToRoute('game_index');
    }

    /**
     * Creates a form to delete a game entity.
     *
     * @param Game $game The game entity
     *
     * @return \Symfony\Component\Form\Form The form
     */
    private function createDeleteForm(Game $game)
    {
        return $this->createFormBuilder()
            ->setAction($this->generateUrl('game_delete', array('id' => $game->getId())))
            ->setMethod('DELETE')
            ->getForm()
        ;
    }
}
Entity\Type.php

namespace AppBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;


/**
 * Type
 *
 * @ORM\Table(name="type")
 * @ORM\Entity(repositoryClass="AppBundle\Repository\TypeRepository")
 */
class Type
{
    /**
     * @ORM\OneToMany(targetEntity="Game", mappedBy="type")
     */
    private $games;

    public function __construct()
    {
        $this->games = new ArrayCollection();
    }
    /**
     * @var int
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

    /**
     * @var string
     *
     * @ORM\Column(name="name", type="string", length=255)
     */
    private $name;




    /**
     * Get id
     *
     * @return int
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * Set name
     *
     * @param string $name
     *
     * @return Type
     */
    public function setName($name)
    {
        $this->name = $name;

        return $this;
    }

    /**
     * Get name
     *
     * @return string
     */
    public function getName()
    {
        return $this->name;
    }

    /**
     * @return mixed
     */
    public function getGames()
    {
        return $this->games;
    }

    /**
     * @param mixed $games
     */
    public function setGames($games)
    {
        $this->games = $games;
    }

}

如果您使用的是symfony表单组件,则
choiceType
不正确。类是
ChoiceType
,只需将C大写,并在类声明上方添加
use
语句

编辑:如果要为该字段选择现有实体,还需要更新表单组件类型

use Symfony\Component\Form\Extension\Core\Type\EntityType;

class GameType extends AbstractType
{
    /**
     * {@inheritdoc}
     */
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder->add('name');


        $builder
        ->add( 'types', EntityType::class, [
            'class' => 'AppBundle:Type',
            'choice_label' => 'name',
            'multiple' => false,
            'expanded' => true
        ] );


    }

关于使用实体和表单的额外阅读

如果您使用的是symfony表单组件,则
选择类型
不正确。类是
ChoiceType
,只需将C大写,并在类声明上方添加
use
语句

编辑:如果要为该字段选择现有实体,还需要更新表单组件类型

use Symfony\Component\Form\Extension\Core\Type\EntityType;

class GameType extends AbstractType
{
    /**
     * {@inheritdoc}
     */
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder->add('name');


        $builder
        ->add( 'types', EntityType::class, [
            'class' => 'AppBundle:Type',
            'choice_label' => 'name',
            'multiple' => false,
            'expanded' => true
        ] );


    }

关于使用实体和表单的额外阅读在您的案例中,您应该使用EntityType而不是ChoiseType

       $builder
    ->add('type', EntityType::class, [
    'class' => Type::class,
    'choice_label' => 'name',
    'multiple' => false,
    'expanded' => true

注意上限或下限。

在您的情况下,应该使用EntityType而不是ChoiseType

       $builder
    ->add('type', EntityType::class, [
    'class' => Type::class,
    'choice_label' => 'name',
    'multiple' => false,
    'expanded' => true

注意上限或下限。

我已经这样做了,现在我得到了以下错误:“class”选项不存在。定义的选项有:“操作”、“允许额外字段”、“属性”、“自动初始化”、“块名称”、“按引用”、“选择属性”、“选择标签”、“选择加载器”、“选择名称”、“选择翻译域”、“选择值”、“选择”、“选择为值”、“复合”、“约束”、“csrf字段名称”、“csrf消息”、“csrf保护”、“csrf令牌id”,“csrf\u令牌\u管理器”、“数据”、“数据类”、“禁用”、“空\u数据”、(截断)这是什么意思?啊,我现在看到了错误。更新我的答案以帮助解释现在我得到了这个错误:“传递到选择字段的实体必须进行管理。”。也许可以将它们保存在实体管理器中?500内部服务器错误“。我已将types的复数改为单数,因为在我的例子中没有types的复数变量。我将用当前代码更新我的问题。谢谢您的耐心,请耐心等待:)您是否正在将AppBundle:Type类的实例传递到表单中?如果是这样的话,那可能是您出错的原因。将Appbundle:Type的实例传递到表单是什么意思?我已经这样做了,现在我得到了以下错误:“class”选项不存在。定义的选项有:“操作”、“允许额外字段”、“属性”、“自动初始化”、“块名称”、“按引用”、“选择属性”、“选择标签”、“选择加载器”、“选择名称”、“选择翻译域”、“选择值”、“选择”、“选择为值”、“复合”、“约束”、“csrf字段名称”、“csrf消息”、“csrf保护”、“csrf令牌id”,“csrf\u令牌\u管理器”、“数据”、“数据类”、“禁用”、“空\u数据”、(截断)这是什么意思?啊,我现在看到了错误。更新我的答案以帮助解释现在我得到了这个错误:“传递到选择字段的实体必须进行管理。”。也许可以将它们保存在实体管理器中?500内部服务器错误“。我已将types的复数改为单数,因为在我的例子中没有types的复数变量。我将用当前代码更新我的问题。谢谢您的耐心,请耐心等待:)您是否正在将AppBundle:Type类的实例传递到表单中?如果是,这可能是您错误的原因。将Appbundle:Type的实例传递到表单是什么意思?我已经这样做了,但现在出现了以下错误:未知实体命名空间别名“Type”。您应该添加use语句。我已经这样做了,但现在出现了以下错误:未知实体命名空间别名“Type”。您应该添加use语句。