简体   繁体   中英

Could not determine access type for property “id” - HandleRequest failing unless I make the entity id public

I have a form builder set up from a question entity:

class Question
{
    /**
     * @ORM\Id
     * @ORM\Column(type="integer")
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $id;
    /**
     * @ORM\Column(type="string", length=255, name="question")
     * @var string $question
     */
    protected $question;
    /**
     * @ORM\Column(type="string", length=255, name="answer1")
     * @var string $answer1
     */
    protected $answer1;
    /**
     * @ORM\Column(type="string", length=255, name="answer2")
     * @var string $answer2
     */
    protected $answer2;
    /**
     * @ORM\Column(type="string", length=255, name="answer3")
     * @var string $answer3
     */
    protected $answer3;
    /**
     * @ORM\Column(type="integer", name="correct_answer")
     * @var int $correctAnswer
     */
    protected $correctAnswer;
    /**
     * @ORM\Column(type="datetime", name="post_date")
     */
    protected $postDate;
    /....

Now the builder looks like this:

 $question = new Question();

        $form = $this->createFormBuilder($question)
            ->add('id', HiddenType::class, array(
                    'data' => 0, 
                    'attr' => array(
                        'class' => 'question-form__id'
                    )
                )
            )
            ->add('question', TextType::class, array('label' => 'Pytanie', 'required' => true))
            ->add('answer1', TextType::class, array('label' => 'Odpowiedź 1', 'required' => true))
            ->add('answer2', TextType::class, array('label' => 'Odpowiedź 2', 'required' => true))
            ->add('answer3', TextType::class, array('label' => 'Odpowiedź 3', 'required' => true))
            ->add('correctAnswer', ChoiceType::class, array(
                'label' => 'Poprawna odpowieź',
                'choices' => array(
                    'Pierwsza' => 1,
                    'Druga' => 2,
                    'Trzecia' => 3
                )
            ))
            ->add('save', SubmitType::class, array(
                'label' => 'Dodaj Pytanie',
                'attr' => array(
                    'class' => 'btn btn-primary btn--add-question'
                )
            ))
            ->getForm();

The reason id has data set to 0 is because the value from it will either be formed into a new question ( if id==0 ) or edit a current one ( else ).

The problem is, upon submitting the form I'm getting

Could not determine access type for property "id"

Playing around with exit; showed the error happening right at the $form->handleRequest($request);

The funny thing here is that if I set entities id as public the handleRequest works just fine.

Tho we all know that having the id as public isn't really such a good idea.

So my question is how can the error be avoided without having to do probably the worst practice of all time?

EDIT: Setting the id value to private results in same error

您可以为$ id添加一个setter,就像其他属性的setter一样

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM