(Symfony3/php7) 不能使用 AppBundle\Entity 类型的对象作为数组

sha*_*lex 1 php entity symfony

我有一个错误:“不能使用 AppBundle\Entity 类型的对象作为数组”,这对我来说没有意义,我没关系。

if ($form->isSubmitted() && $form->isValid()) {
            $user = $form->getData();
            $repository = $this->getDoctrine()->getRepository(User::class);
            $findUser = $repository->findOneBy(['username' => $form->getData()['username']]);
            if ($findUser) {
                return $this->redirectToRoute('login');
            }
Run Code Online (Sandbox Code Playgroud)

错误无法使用 AppBundle\Entity\User 类型的对象,因为数组与“ $findUser =对齐

有人可以解释我的为什么它不起作用吗?

Symfony 3.3,PHP 7

Ale*_*hin 5

这个例子在这里解释了一切:

$form->handleRequest($request);

if ($form->isSubmitted() && $form->isValid()) {
    // $form->getData() holds the submitted values
    // but, the original `$task` variable has also been updated
    $task = $form->getData();

    ...
}
Run Code Online (Sandbox Code Playgroud)

因此,$form->getData()持有实体。所以,如果你的User实体有一个username属性,你应该把它称为$form->getData()->getUsername()或者你在那里拥有的任何 getter。