1
votes

I am new in Symfony and try to creating a simple form in Symfony and try to get the value from Database through EntityType Field but I can't get the value when I dump the submitted form data it shows object of EntityType Field.

my code

public function indexAction(Request $request)
{ 
    // user details
    if($this->getUser()){
        $user = $this->getUser()->getId();
    } else {
        $user = '';
    }
   
    $classarray = array(
    'School 1' => 1,
    'School 2' => 2,
    'School 3' => 3,
    );

    // create a class booking form
    $classbooking = new Classbooking();
    
    $classbooking->setUserId($user);
    
    $form = $this->createFormBuilder($classbooking)
    ->add('school',  ChoiceType::class, array(
    'choices'  => $classarray,
    // *this line is important*
    'choices_as_values' => true,
    'label' => 'Select School',
    'mapped' => false,
    ))
    ->add('class_id', EntityType::class, array(
        // query choices from this entity
        'class' => 'RoomBundle:Classlist',

        // use the User.username property as the visible option string
        'choice_label' => 'className',
        'choice_value' => 'id',

        // used to render a select box, check boxes or radios
        // 'multiple' => true,
        // 'expanded' => true,
    ))
    
    ->add('book_dt', TextType::class)
    ->add('purpose', TextType::class)
    ->add('numberStudents', IntegerType::class)
    ->add('save', SubmitType::class, array('label' => 'Book Now!'))
    ->getForm();

    $form->handleRequest($request);

if ($form->isSubmitted() && $form->isValid()) {
    $classbooking = $form->getData();
    //print_r($classbooking); exit();
    
     $em = $this->getDoctrine()->getManager();
     $em->persist($classbooking);
     $em->flush();

    return $this->redirectToRoute('booking_success');
}

Classlist

/**
 * @var integer
 *
 * @ORM\Column(name="id", type="integer")
 * @ORM\Id
 * @ORM\GeneratedValue(strategy="AUTO")
 */
private $id;

/**
 * @var string
 *
 * @ORM\Column(name="class_name", type="string", length=255)
 */
private $className;

/**
 * @var integer
 *
 * @ORM\Column(name="school_id", type="integer")
 */
private $schoolId;

/**
 * @var string
 *
 * @ORM\Column(name="slug", type="string", length=255)
 */
private $slug;

/**
 * @var integer
 *
 * @ORM\Column(name="status", type="integer")
 */
private $status;

/**
 * @var \DateTime
 *
 * @ORM\Column(name="date_created", type="datetimetz")
 */
private $dateCreated;
public function __construct() {
    $this->dateCreated = new \DateTime('now');
    
}

Classbooking entity

   class Classbooking
{
    /**
     * @var integer
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

    /**
     * @var integer
     *
     * @ORM\Column(name="class_id", type="integer")
     */
    private $classId;

    /**
     * @var integer
     *
     * @ORM\Column(name="user_id", type="integer")
     */
    private $userId;

    /**
     * @var string
     * @Assert\NotBlank()
     * @ORM\Column(name="purpose", type="string", length=255)
     */
    private $purpose;

    /**
     * @var integer
     * @Assert\NotBlank()
     * @ORM\Column(name="number_students", type="integer")
     */
    private $numberStudents;

    /**
     * @var \DateTime
     * 
     * @ORM\Column(name="book_dt", type="datetimetz")
     */
    private $bookDt;

    /**
     * @var integer
     *
     * @ORM\Column(name="status", type="integer")
     */
    private $status = 1;

    /**
     * @var \DateTime
     *
     * @ORM\Column(name="date_created", type="datetimetz")
     */
    private $dateCreated;
    public function __construct() {
        $this->dateCreated = new \DateTime('now');
        
    }

My error

An exception occurred while executing 'INSERT INTO Classbooking (class_id, user_id, purpose, number_students, book_dt, status, date_created) VALUES (?, ?, ?, ?, ?, ?, ?)' with params [{}, 1849039287, "aaa", 5, "2018-02-28 00:00:00", 1, "2018-02-25 08:22:02"]:

Catchable Fatal Error: Object of class AYA\Cocorico\RoomBundle\Entity\Classlist could not be converted to string

1
Could you also post the code of the Classbooking-entity, the properties and the ORM-annotations should be enough. - dbrumann
The problem is with class_id which in your case is a (or an array of) Classlist-entity as per EntityType, but probably should be something else according to the ORM-annotation on the Classbooking-entity. - dbrumann
@dbrumann I added classlist entity and booking entity both can you tell me how fix it. - Zaheer Ahmad

1 Answers

0
votes

The problem is with this part of the form:

->add('class_id', EntityType::class, array(
    // query choices from this entity
    'class' => 'RoomBundle:Classlist',

    // use the User.username property as the visible option string
    'choice_label' => 'className',
    'choice_value' => 'id',

    // used to render a select box, check boxes or radios
    // 'multiple' => true,
    // 'expanded' => true,
))

In combination with this part of the entity:

/**
 * @var integer
 *
 * @ORM\Column(name="class_id", type="integer")
 */
private $classId;

You have a property called classId, which expects an int, where you want to have the connect class or classes.

You should change the property on the Entity to look something like this:

/**
 * @var RoomBundle\Entity\Classlist
 *
 * @ORM\ManyToOne(targetEntity="RoomBundle\Entity\Classlist")
 */
private $classList;

The relationship might be off, for example you could have a ManyToMany or OneToMany-relationship. For more details check the Doctrine documentation on assications: http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/reference/association-mapping.html

You will also have to update your schema after this change!

Now your form matches with the column type as it will provide an entity and doctrine expects an associated entity.