首页 > 解决方案 > 错误:在属性路径“provincie_id”处给出的“int”、“App\Entity\Provincie”类型的预期参数

问题描述

错误:在属性路径“provincie_id”处给出的“int”类型、“App\Entity\Provincie”的预期参数。

我使用实体在我的表单中创建一个选择框。它加载正常,当我提交表单时出现上述错误。我使用 symfony CLI 命令生成了我的实体。这是我使用的代码。

src/Controller/UserController.php

<?php

namespace App\Controller;


use App\Form\ProfielType;
use App\Entity\Profiel;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;


/**
* Controller used to manage current user.
*
* @Route("/profile")
* @IsGranted("ROLE_USER")
*
* @author Romain Monteil <monteil.romain@gmail.com>
*/
class UserController extends AbstractController
{


 /**
 * @Route("/edit_profile_meta", methods={"GET", "POST"}, name="user_edit_meta")
 */
public function edit_profile_meta(Request $request): Response
{

    $user = $this->getUser();
    $profielId = $user->getId();      
    $profile = $this->getDoctrine()
        ->getRepository(Profiel::class)
        ->findOneBy(['user_id' => $profielId]);        

    $form = $this->createForm(ProfielType::class, $profile);      
    $form->handleRequest($request);

    if ($form->isSubmitted() && $form->isValid()) {

        $entityManager = $this->getDoctrine()->getManager();
        $entityManager->persist($profile);
        $entityManager->flush();            

        $this->addFlash('success', 'user.updated_successfully');

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

    return $this->render('user/edit_meta.html.twig', [
        'profile' => $profile,
        'form' => $form->createView(),
    ]);
}  


}

src/Form/ProfielType.phpp

<?php    

namespace App\Form;

use App\Entity\Profiel;
use Symfony\Component\Form\AbstractType;
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
use App\Entity\Provincie;

use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;

/**
* Defines the form used to edit an user.
*
* @author Romain Monteil <monteil.romain@gmail.com>
*/
class ProfielType extends AbstractType
{
    /**
    * {@inheritdoc}
    */
    public function buildForm(FormBuilderInterface $builder, array $options): void
{       

    $builder            
        ->add('provincie_id', EntityType::class, [
                'class' => Provincie::class,                    
            ]
        )
    ;

}

/**
 * {@inheritdoc}
 */
public function configureOptions(OptionsResolver $resolver): void
{
    $resolver->setDefaults([
        'data_class' => Profiel::class,
    ]);
}
}

src/Entity/Provincie.php

<?php

namespace App\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
* @ORM\Entity(repositoryClass="App\Repository\ProvincieRepository")
*/
class Provincie
{
/**
 * @ORM\Id()
 * @ORM\GeneratedValue()
 * @ORM\Column(type="integer")
 */
private $id;

/**
 * @ORM\Column(type="text")
 */
private $naam;

/**
 * @ORM\Column(type="integer")
 */
private $land_id;

public function getId(): ?int
{
    return $this->id;
}

public function getNaam(): ?string
{
    return $this->naam;
}

public function setNaam(string $naam): self
{
    $this->naam = $naam;

    return $this;
}

public function getLandid(): ?int
{
    return $this->land_id;
}

public function setLandid(int $land_id): self
{
    $this->land_id = $land_id;

    return $this;
}

public function __toString()
{
    return $this->getNaam();
}
}

标签: phpsymfony-4.2

解决方案


推荐阅读