在 symfony 中显示问题的实体
Posted
技术标签:
【中文标题】在 symfony 中显示问题的实体【英文标题】:Entity showing problems in symfony 【发布时间】:2021-11-05 02:18:58 【问题描述】:我正在创建一个小型 QA 应用。我有与用户和问题相关的实体答案。我正在尝试为 Answer 创建一个表单。我能够显示所有答案的列表,但是在创建新答案时,出现了问题。 the error
这是我的 AnswerEntity 代码:
namespace App\Entity;
use App\Repository\AnswerRepository;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity(repositoryClass=AnswerRepository::class)
* @ORM\Table(name="answers")
*/
class Answer
/**
* @ORM\Id
* @ORM\GeneratedValue
* @ORM\Column(type="integer")
*/
private $id;
/**
* @ORM\Column(type="string", length=200)
*/
private $answer_text;
/**
* @ORM\ManyToOne(targetEntity=Question::class, inversedBy="answer")
* @ORM\JoinColumn(nullable=false)
*/
private $question;
public function getId(): ?int
return $this->id;
public function getAnswerText(): ?string
return $this->answer_text;
public function setAnswerText(string $answer_text): void
$this->answer_text = $answer_text;
public function getQuestion(): ?Question
return $this->question;
public function setQuestion(?Question $question): void
$this->question = $question;
以及 AnswerForm 的代码
/**
* Answer type.
*/
namespace App\Form;
use App\Entity\Answer;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
/**
* Class AnswerType.
*/
class AnswerType extends AbstractType
/**
* Builds the form.
*
* This method is called for each type in the hierarchy starting from the
* top most type. Type extensions can further modify the form.
*
* @see FormTypeExtensionInterface::buildForm()
*
* @param \Symfony\Component\Form\FormBuilderInterface $builder The form builder
* @param array $options The options
*/
public function buildForm(FormBuilderInterface $builder, array $options): void
$builder->add(
'AnswerText',
TextType::class,
[
'label' => 'label_answertext',
'required' => true,
'attr' => ['max_length' => 200],
]
);
/**
* Configures the options for this type.
*
* @param \Symfony\Component\OptionsResolver\OptionsResolver $resolver The resolver for the options
*/
public function configureOptions(OptionsResolver $resolver): void
$resolver->setDefaults(['data_class' => Answer::class]);
/**
* Returns the prefix of the template block name for this type.
*
* The block prefix defaults to the underscored short class name with
* the "Type" suffix removed (e.g. "UserProfileType" => "user_profile").
*
* @return string The prefix of the template block name
*/
public function getBlockPrefix(): string
return 'answer';
还有应答控制器
/**
* Answer Controller
*/
namespace App\Controller;
use App\Entity\Answer;
use App\Entity\Question;
use App\Form\AnswerType;
use App\Repository\AnswerRepository;
use Symfony\Component\Form\Extension\Core\Type\FormType;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Knp\Component\Pager\PaginatorInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Flex\PackageFilter;
use Symfony\Component\Routing\Annotation\Route;
/**
* Class AnswerController.
*
* @Route("/answer")
*/
class AnswerController extends AbstractController
private $answerRepository;
private $answer;
private $paginator;
/**
* AnswerController constructor
*
* @param \App\Repository\AnswerRepository $answerRepository Answer Repository
* @param \Knp\Component\Pager\PaginatorInterface $paginator
*/
public function __construct(AnswerRepository $answerRepository, PaginatorInterface $paginator)
$this->answerRepository = $answerRepository;
$this->paginator = $paginator;
/**
* Index action.
*
* @param \Symfony\Component\HttpFoundation\Request $request HTTP request
* @return \Symfony\Component\HttpFoundation\Response HTTP response
*
* @Route(
* "/",
* methods="GET",
* name="answer_index",
* )
*/
public function index(Request $request, PaginatorInterface $paginator, AnswerRepository $answerRepository): Response
$pagination = $paginator->paginate(
$answerRepository->queryAll(),
$request->query->getInt('page', 1),
AnswerRepository::PAGINATOR_ITEMS_PER_PAGE
);
return $this->render(
'answer/index.html.twig',
['pagination' => $pagination]
);
/**
* Create action.
*
* @param \Symfony\Component\HttpFoundation\Request $request HTTP request
*
* @param \App\Repository\AnswerRepository $answerRepository Answer repository
* @param \App\Entity\Answer $answer Answer Entity
*
* @return \Symfony\Component\HttpFoundation\Response HTTP response
*
* @throws \Doctrine\ORM\ORMException
* @throws \Doctrine\ORM\OptimisticLockException
*
* @Route(
* "/create",
* methods="GET", "POST",
* name="answer_create",
* )
*/
public function create(Request $request, AnswerRepository $answerRepository, Answer $answer): Response
$answer = new Answer();
$form = $this->createForm(AnswerType::class, $answer);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid())
$answerRepository->save($answer);
$this->addFlash('success', 'answer_created_successfully');
return $this->redirectToRoute('question_index');
return $this->render(
'answer/create.html.twig',
['form' => $form->createView()]
);
此外,问题是..,如何路由答案/创建以使其成为问题的答案?我的意思是,我有 localhost/question/questionid,我想在这里创建问题。
感谢您的帮助!
【问题讨论】:
【参考方案1】:您的错误是ParamConverter
错误。在您的控制器中,您要求 symfony 给您一个Answer $answer
,但它无法提供它,因为没有办法做到这一点。您的控制器方法中不需要此参数。
我也怀疑你的AnswerType
是否会起作用,因为你的属性被命名为answer_text
并且你的字段被命名为AnserText
。它们必须匹配。
【讨论】:
谢谢,我删除了这个参数,它成功了! :D 我现在将尝试处理表单(它更像是一个草图,因为我之前遇到了一个错误)。但是在将新答案与问题联系起来时仍然会遇到问题。你有什么想法吗? 这是另一个问题。但是您需要在控制器中获取问题并设置您的 Answer 实体的问题以上是关于在 symfony 中显示问题的实体的主要内容,如果未能解决你的问题,请参考以下文章
Symfony2 实体中除 id 以外的字段的关联在表单中未正确显示
Symfony Sonata Admin 仅将属性显示为只读文本