Good day. I would like to add to the default form of the selected author in the hidden field. Adding occurs as expected, but during the recording I get:

Could not determine access type for property "id". 

Now in order:

Controller. Here I set the default mentor. And in the form of everything as it should be shown.

 public function newAction(Request $request) { $meeting = new Meeting(); $meeting->setMentor($this->getUser()); $form = $this->createForm('App\AdminBundle\Form\MeetingType', $meeting); $form->handleRequest($request); if ($form->isSubmitted() && $form->isValid()) { $em = $this->getDoctrine()->getManager(); $em->persist($meeting); $em->flush($meeting); return $this->redirectToRoute('meeting_show', array('id' => $meeting->getId())); } return $this->render('meeting/new.html.twig', array( 'meeting' => $meeting, 'form' => $form->createView(), )); } 

The form.

 public function buildForm(FormBuilderInterface $builder, array $options) { $builder ... ->add('mentor', HiddenType::class,[ 'property_path' => 'mentor.id' ]) ... } 

And, actually, the field of this form in html. Everything is fine here.

 <input type="hidden" id="app_adminbundle_meeting_mentor" name="app_adminbundle_meeting[mentor]" value="1"> 

    1 answer 1

    There is no standard way to set an ID in your user (mentor) class. When writing, symfony tries to write a value (id) there, but cannot do it because no setter ( setId($id) ), and the property itself is not public.

    Options:

    1. Do you really need this form field? Perhaps it makes sense to call $meeting->setMentor($this->getUser()); just before writing to the database.

    2. To prevent the form from trying to write a value to an object, you can set the mapped => false option to the field, but then you have to manually set the value for the field: data => $id , in general it will look like this:

    ->add('mentor', HiddenType::class,[ 'mapped' => false, 'data' => $id, ])

    1. Use DataMapper . By the way, not many people know about them. for some reason they are omitted from the documentation, and the thing is very powerful.

    2. You can add the setId ($ id) method to a user class; you can simply use a function without a body. This method of solution is very doubtful, although it will work. But, once again, at least for aesthetic reasons, you should not do this.

    • well, something I felt like an idiot. The first option was essentially the only correct one, in fact. I can not understand why I even shoved the participant before the participant. Complicated the task only. Thanks for the help. And for DateMapper a special thank you, I ’ll get a better look now :) - Vladislav