在我的项目中,我使用数据表(用户任务)来描述数据以创建预订记录(预订表)。在表格中,我尝试收到选择列表。
运行时报错:
为什么“数组”数据是错误的?什么变化?
我的代码:
形式:
<?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 上运行。
请帮我。
最佳答案
你也不能在 handleRequest
对象上调用 FormView
方法。
$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 );
}
在您使用选择 ('t.id', ...) 的情况下
QueryBuilder 将返回普通数组
array:1 [▼
0 => array:2 [▼
"id" => 1
"task_name" => "New Task"
]
]
但是
EntityType
需要一个对象数组,例如array:1 [▼
0 => Task {#448 ▶}
]
最后,不要忘记在
'choice_label' => 'task_name'
方法中添加选项 buildForm
。关于php - 如何在表单中列出查询结果(EntityType),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/44207914/