gpt4 book ai didi

php - 如何在表单中列出查询结果(EntityType)

转载 作者:可可西里 更新时间:2023-10-31 23:15:38 26 4
gpt4 key购买 nike

在我的项目中,我使用一个表(用户任务)中的数据来描述创建预订记录(预订表)的数据。在表单中,我尝试接收选择列表。

运行时报错:

Expected argument of type "Doctrine\ORM\QueryBuilder", "array" given

为什么“数组”数据是错误的?什么变化?

我的代码:

表格:

<?php
namespace AppBundle\Form;
use AppBundle\Repository\TaskRepository;
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;

/**
* Class ReservationType
*
* @package AppBundle\Form
*/
class ReservationType extends AbstractType
{
/**
* {@inheritdoc}
*/
public function buildForm( FormBuilderInterface $builder, array $options )
{
$builder
->add( 'taskId', EntityType::class, [
'class' => 'AppBundle:Task',
'query_builder' => function (TaskRepository $tr) use ($options) {
return $tr->TaskUserListQuery( $options['userId']);
},
'attr' => [
'data-type' => 'text',
'class' => 'table-select',
'disabled' => true
],
'required' => false
])
;
}

/**
* {@inheritdoc}
*/
public function configureOptions( OptionsResolver $resolver )
{
$resolver->setDefaults( [
'data_class' => 'AppBundle\Entity\Task',
'userId' => null,
] );
}


/**
* {@inheritdoc}
*/
public function getBlockPrefix()
{
return 'appbundle_reservation';
}
}

存储库:

<?php
namespace AppBundle\Repository;
use \Doctrine\ORM\EntityRepository;

/**
* TaskRepository
*/
class TaskRepository extends EntityRepository
{
/**
* Function TaskUserListQuery
* @return array
*/
public function TaskUserListQuery( $userId )
{
return $this->createQueryBuilder( 't' )
->select(
't.id',
't.taskName'
)
->orderBy( 't.id', 'ASC' )
->where( 't.userId = :par1' )
->setParameter( 'par1', $userId )
->getQuery()
->getResult();
}
}

Controller :

<?php

namespace AppBundle\Controller;

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

/**
* Reservation controller.
*
* @Route("/re")
*/
class ReservationController extends Controller
{

/**
* Creates a new reservation entity.
*
* @Route("/new", name="r_new")
* @Method({"GET", "POST"})
*/
public function newAction( Request $request )
{
$userId = 1;

$reservation = new Reservation();
$tableForm = $this->createForm( 'AppBundle\Form\ReservationType', $reservation, [
'userId' => $userId,
] );
$form = $tableForm->createView();
$form->handleRequest( $request );

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

return $this->redirectToRoute( 'r_show', [ 'id' => $reservation->getId() ] );
}

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

// (... more)

}

什么时候出错?

我正在学习表单示例并在 Symfony 3.2 上运行。请帮我。

最佳答案

此外,您不能在 FormView 对象上调用 handleRequest 方法。

$tableForm = $this->createForm( 'AppBundle\Form\ReservationType',
$reservation, ['userId' => $userId,]);
$form = $tableForm->createView(); // WTF ?
$form->handleRequest( $request );

任务库

public function TaskUserListQuery( $userId )
{
return $this->createQueryBuilder( 't' )
->select('t') // in this way
->orderBy( 't.id', 'ASC' )
->where( 't.userId = :par1' )
->setParameter( 'par1', $userId );
}

在您使用 selecting ('t.id', ...)QueryBuilder 将返回纯数组

array:1 [▼
0 => array:2 [▼
"id" => 1
"task_name" => "New Task"
]
]

但是 EntityType 需要一个像这样的对象数组

array:1 [▼
0 => Task {#448 ▶}
]

最后,不要忘记在 buildForm 方法中添加选项 'choice_label' => 'task_name'

关于php - 如何在表单中列出查询结果(EntityType),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44207914/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com