In my project, I use the data form of one table (user task) to describe the data for creating a reservation record (reservation table). In the form, I will try to get a selection list.
When starting the error:Expected argument of type "Doctrine\ORM\QueryBuilder", "array" given
Why is the "array" data incorrect? What changed?
My code is:
the form:
<?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 extends AbstractType
{
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
])
;
}
public function configureOptions( OptionsResolver $resolver )
{
$resolver->setDefaults( [
'data_class' => 'AppBundle\Entity\Task',
'userId' => null,
] );
}
public function getBlockPrefix()
{
return 'appbundle_reservation';
}
}
Repository:
<?php
namespace AppBundle\Repository;
use \Doctrine\ORM\EntityRepository;
class TaskRepository extends EntityRepository
{
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;
class ReservationController extends Controller
{
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(),
] );
}
}
When am I wrong?
I am learning a form example and running Symfony 3.2. Please help me.