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

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

运行时出错:

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

为什么“数组”数据有误?有什么变化?

我的代码:

形成:

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';

}

}

库:

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();

}

}

控制器:

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 );

TaskRepository

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将返回plain数组

array:1 [▼

0 => array:2 [▼

"id" => 1

"task_name" => "New Task"

]

]

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

array:1 [▼

0 => Task {#448 ▶}

]

最后,不要忘记添加选项’choice_label’=> buildForm方法中的’task_name’.

标签:php,symfony,symfony-forms,query-builder,doctrine-orm

来源: https://codeday.me/bug/20190627/1307238.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值