首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >给定“整型”、“App\Entity\Entity”类型的预期参数

给定“整型”、“App\Entity\Entity”类型的预期参数
EN

Stack Overflow用户
提问于 2018-10-22 20:38:44
回答 2查看 1.3K关注 0票数 0

我使用两个实体:舞台和企业。每个舞台上都有一个企业。我只需要把id企业保存在舞台上的桌子上。当我创建一个新的stagiaire时,为他选择一个企业并保存表单,我有以下错误

在stagiaire控制器中"integer“类型的预期参数:”App\Entity\Entity“给定

以下是stagiaire实体的代码:

代码语言:javascript
复制
namespace App\Entity;
use Doctrine\ORM\Mapping as ORM;

/**
 * @ORM\Entity(repositoryClass="App\Repository\StagiaireRepository")
 */

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

    /**
     * @var string
     *
     * @ORM\Column(name="civilite", type="string", length=3, nullable=false)
     */
    private $civilite = 'Mr';

    /**
     * @var string
     *
     * @ORM\Column(name="nom", type="string", length=24, nullable=false)
     */
    private $nom;

    /**
     * @var string
     *
     * @ORM\Column(name="prenom", type="string", length=16, nullable=false)
     */
    private $prenom;

    /**
     * @ORM\ManyToOne(targetEntity="App\Entity\Entreprise")
     * @ORM\JoinColumn(nullable=false)
     */
    private $entreprise;

    /**
     * @var string
     *
     * @ORM\Column(name="status", type="string", length=12, nullable=false)
     */
    private $status;

    public function __construct()
  {
    //$this->date       = new \Datetime();
    //$this->entreprise = new ArrayCollection();
  }

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

    public function getCivilite(): ?string
    {
        return $this->civilite;
    }

    public function setCivilite(string $civilite): self
    {
        $this->civilite = $civilite;

        return $this;
    }

    public function getNom(): ?string
    {
        return $this->nom;
    }

    public function setNom(string $nom): self
    {
        $this->nom = $nom;

        return $this;
    }

    public function getPrenom(): ?string
    {
        return $this->prenom;
    }

    public function setPrenom(string $prenom): self
    {
        $this->prenom = $prenom;

        return $this;
    }

    public function getEntreprise(): ?int
    {
        return $this->entreprise;
    }

    public function setEntreprise(int $entreprise): self
    {
        $this->entreprise = $entreprise;

        return $this;
    }

    public function getStatus(): ?string
    {
        return $this->status;
    }

    public function setStatus(string $status): self
    {
        $this->status = $status;

        return $this;
    }

这是表格的代码:

代码语言:javascript
复制
use App\Entity\Stagiaire;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Form\Extension\Core\Type\FormType;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Bridge\Doctrine\Form\Type\EntityType;

class StagiaireType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $choicesCivil = [
            'Mr' => '1',
            'Mme' => '2'
        ];
        $choicesStatus = [
            'Gérant' => '1',
            'Salarié' => '2'
        ];

        $builder
            ->add('civilite', ChoiceType::class, [
                'data' => '1', // cochée par défaut
                'choices' => $choicesCivil,
                'expanded' => true,  // => boutons
                'label' => 'Civilité',
                'multiple' => false
            ])
            ->add('nom')
            ->add('prenom')
            ->add('entreprise', EntityType::class, array(
                'class'         => 'App:Entreprise',
                'placeholder'   => 'Choisir une entreprise',
                'choice_label'  => 'enseigne',
            ))
            ->add('status', ChoiceType::class, [
                'data' => '1', // cochée par défaut
                'choices' => $choicesStatus,
                'expanded' => true,  // => boutons
                'label' => 'Statut',
                'multiple' => false
            ])
            ->add('create', SubmitType::class, ['label' => 'Enregistrer', 'attr' => ['class' => 'btn btn-primary']]);
    }

    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults([
            'data_class' => Stagiaire::class,
        ]);
    }
}

这里是控制器:

代码语言:javascript
复制
namespace App\Controller;

use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;

use App\Entity\Stagiaire;
use App\Entity\Entreprise;
use App\Repository\StagiaireRepository;
use App\Form\StagiaireType;

class StagiaireController extends Controller
{
    public function fStagiaire($id,$cat,$action, Request $request)
    {
        if ($action=='insert') {
            $stagiaire = new Stagiaire();
        }
        elseif($action=='update' || $action=='delete') {
            $stagiaire = $this->getDoctrine()
              ->getManager()
              ->getRepository('App:Stagiaire')
              ->find($id)
            ;
        }
        else { return;}

            $form = $this->get('form.factory')->create(StagiaireType::Class, $stagiaire);
        
        if ($request->isMethod('POST')) { 
          $form->handleRequest($request);

          if ($form->isValid()) {...

错误发生在$form->handleRequest($request);行,我搜索了几天,但没有找到解决方案。有人想帮忙吗?

EN

回答 2

Stack Overflow用户

发布于 2018-10-22 21:22:15

您的function setEntreprise(int $entreprise): self正在被赋予一个对象( App\Entity\Entreprise),它是而不是。虽然数据库中的记录将是int ( Entreprise实体上的主键id --事实上,它将是一个单独的数据库表,引用该记录的ID,其中包含零条或多条Entreprise记录),但它作为对象提供给您的代码(从getEntreprise())可用(因此当您读取链接的实体时,返回可空的int也会失败--它应该是一个空的ArrayCollection())。

您的setter & getter需要处理对象--因为它是一个“ManyToOne”关系,所以可以有多个对象。您可能还希望addEntreprise()函数能够添加更多的对象。ORM将处理如何将其中的数据保存到数据库中,并在读取此记录时将其链接回数据库。

代码语言:javascript
复制
public function __construct()
{
    //$this->date       = new \Datetime();
    //$this->entreprise = new ArrayCollection();
}
# These lines say that `$this->entreprise` is an array - by default empty, but 
# it can be filled up with a number of linked objects.
票数 0
EN

Stack Overflow用户

发布于 2018-10-23 09:39:49

您需要更改您的函数,以接受entreprise,而不是id:

代码语言:javascript
复制
public function getEntreprise(): ?Entreprise
{
    return $this->entreprise;
}

public function setEntreprise(Entreprise $entreprise): self
{
    $this->entreprise = $entreprise;

    return $this;
}

您必须将use App\Entity\Entreprise;添加到Stagiaire类中。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52937445

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档