spl_object_hash() 尝试提交我的 Symfony 表单时出错?

spl_object_hash() error while trying to submit my Symfony forms?

我想提交 2 个表单,ArticlegroupeAuteur,当我点击提交时我收到了这个错误。

我正在尝试嵌入表单,但在提交时出现此错误:

Warning: spl_object_hash() expects parameter 1 to be object, string given

enter image description here

我的实体文章:

<?php

namespace App\Entity;

use App\Repository\ArticleRepository;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;


/**
 * @ORM\Entity(repositoryClass=ArticleRepository::class)
 */
class Article
{
    /**
     * @ORM\Id()
     * @ORM\GeneratedValue()
     * @ORM\Column(type="integer")
     */
    private $id;

    /**
     * @ORM\Column(type="string", length=255)
     */
    private $title;

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

    /**
     * @ORM\Column(type="string", length=255)
     */
    private $keyword;

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

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

    /**
     * @ORM\ManyToOne(targetEntity=Users::class, inversedBy="articles")
     * @ORM\JoinColumn(nullable=false)
     */
    private $User;

    /**
     * @ORM\ManyToOne(targetEntity="App\Entity\Categorie", inversedBy="article")
     * @ORM\JoinColumn(name="categorie_id", referencedColumnName="id", nullable=true)
     */
    private $categorie;

    /**
     * @ORM\ManyToOne(targetEntity="App\Entity\Revue", inversedBy="article")
     * @ORM\JoinColumn(name="revue_id", referencedColumnName="id", nullable=true)
     */
    private $revue;

    /**
     * @ORM\ManyToMany(targetEntity=GroupeAuteur::class, inversedBy="articles", cascade={"persist", "remove"})
     * @var $data
     */
    private $auteurs;

    public function __construct()
    {
        $this->auteurs = new ArrayCollection();
    }

    /**
     * @return mixed
     */
    public function getCategorie()
    {
        return $this->categorie;
    }

    /**
     * @param mixed $categorie
     */
    public function setCategorie($categorie): void
    {
        $this->categorie = $categorie;
    }

    /**
     * @return mixed
     */
    public function getRevue()
    {
        return $this->revue;
    }

    /**
     * @param mixed $revue
     */
    public function setRevue($revue): void
    {
        $this->revue = $revue;
    }

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

    public function getTitle(): ?string
    {
        return $this->title;
    }

    public function setTitle(string $title): self
    {
        $this->title = $title;

        return $this;
    }

    public function getAbstract(): ?string
    {
        return $this->abstract;
    }

    public function setAbstract(string $abstract): self
    {
        $this->abstract = $abstract;

        return $this;
    }

    public function getKeyword(): ?string
    {
        return $this->keyword;
    }

    public function setKeyword(string $keyword): self
    {
        $this->keyword = $keyword;

        return $this;
    }

    public function getContent(): ?string
    {
        return $this->content;
    }

    public function setContent(string $content): self
    {
        $this->content = $content;

        return $this;
    }

    public function getSize(): ?int
    {
        return $this->size;
    }

    public function setSize(int $size): self
    {
        $this->size = $size;

        return $this;
    }

    public function getUser(): ?Users
    {
        return $this->User;
    }

    public function setUser(Users $User): self
    {
        $this->User = $User;

        return $this;
    }

    /**
     * @return Collection|GroupeAuteur[]
     */
    public function getAuteurs(): Collection
    {
        return $this->auteurs;
    }

    public function addAuteur(GroupeAuteur $auteur): self
    {
        if (!$this->auteurs->contains($auteur)) {
            $this->auteurs[] = $auteur;
        }

        return $this;
    }

    public function removeAuteur(GroupeAuteur $auteur): self
    {
        if ($this->auteurs->contains($auteur)) {
            $this->auteurs->removeElement($auteur);
        }    
        return $this;
    }
}

我的实体GroupeAuteur

<?php

namespace App\Entity;

use App\Repository\GroupeAuteurRepository;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;

/**
 * @ORM\Entity(repositoryClass=GroupeAuteurRepository::class)
 */
class GroupeAuteur
{
    /**
     * @ORM\Id()
     * @ORM\GeneratedValue()
     * @ORM\Column(type="integer")
     */
    private $id;

    /**
     * @ORM\Column(type="string", length=255, nullable=true)
     */
    private $nomAuteur;

    /**
     * @ORM\Column(type="string", length=255, nullable=true)
     */
    private $prenomAuteur;

    /**
     * @ORM\Column(type="string", length=255 , nullable=true, nullable=true)
     */
    private $NomIns;

    /**
     * @ORM\Column(type="string", length=255 , nullable=true, nullable=true)
     */
    private $adresseIns;

    /**
     * @ORM\ManyToOne(targetEntity="App\Entity\Users", inversedBy="groupeAuteur")
     */
    private $user;

    /**
     * @ORM\ManyToMany(targetEntity=Article::class, mappedBy="auteurs")
     */
    private $articles;

    public function __construct()
    {
        $this->articles = new ArrayCollection();
    }

    /**
     * @return mixed
     */
    public function getNomAuteur()
    {
        return $this->nomAuteur;
    }

    /**
     * @param mixed $nomAuteur
     */
    public function setNomAuteur($nomAuteur): void
    {
        $this->nomAuteur = $nomAuteur;
    }

    /**
     * @return mixed
     */
    public function getPrenomAuteur()
    {
        return $this->prenomAuteur;
    }

    /**
     * @param mixed $prenomAuteur
     */
    public function setPrenomAuteur($prenomAuteur): void
    {
        $this->prenomAuteur = $prenomAuteur;
    }

    /**
     * @return mixed
     */
    public function getNomIns()
    {
        return $this->NomIns;
    }

    /**
     * @param mixed $NomIns
     */
    public function setNomIns($NomIns): void
    {
        $this->NomIns = $NomIns;
    }

    /**
     * @return mixed
     */
    public function getAdresseIns()
    {
        return $this->adresseIns;
    }

    /**
     * @param mixed $adresseIns
     */
    public function setAdresseIns($adresseIns): void
    {
        $this->adresseIns = $adresseIns;
    }

    /**
     * @return mixed
     */
    public function getUser()
    {
        return $this->user;
    }

    /**
     * @param mixed $user
     */
    public function setUser($user): void
    {
        $this->user = $user;
    }



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

    public function getListeAuteur(): ?array
    {
        return $this->listeAuteur;
    }

    public function setListeAuteur(?array $listeAuteur): self
    {
        $this->listeAuteur = $listeAuteur;

        return $this;
    }

    /**
     * @return Collection|Article[]
     */
    public function getArticles(): Collection
    {
        return $this->articles;
    }

    public function addArticle(Article $article): self
    {
        if (!$this->articles->contains($article)) {
            $this->articles[] = $article;
            $article->addAuteur($this);
        }

        return $this;
    }

    public function removeArticle(Article $article): self
    {
        if ($this->articles->contains($article)) {
            $this->articles->removeElement($article);
            $article->removeAuteur($this);
        }
        return $this;
    }
}

我的Articlecontroller:

   /**
     * @Route("/create",name="article-new")
     * @param Request $request
     * @param EntityManagerInterface $manager
     * @return Response
     */
    public function newArticle(Request $request,EntityManagerInterface $manager):Response
    {
        $article =new Article();
        $formArticle = $this->createForm(ArticleType::class,$article);
        $formArticle->handleRequest($request);

        if($formArticle->isSubmitted() && $formArticle->isValid() )
        {
            $article->setUser($this->getUser());
            $manager->persist($article);
            $manager->flush();
            return new Response("your data saved with success");
        }

        return $this->render('Article/addNew.html.twig',[
            'formArticle' => $formArticle->createView(),
            'article'=> $article
        ]);
    }

我的表格GroupeAuteurType:

class GroupeAuteurType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('nomAuteur',TextType::class)
            ->add('prenomAuteur',TextType::class)
            ->add('NomIns',TextType::class)
            ->add('adresseIns',TextType::class)
        ;
    }

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

我的表格ArticleType:

class ArticleType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('title',TextType::class)
            ->add('abstract',TextType::class)
            ->add('keyword',TextType::class)
            ->add('content',TextareaType::class)
            ->add('size',IntegerType::class)
            ->add('categorie',EntityType::class, [
                'class' => Categorie::class,
                'choice_label' => 'nomCategorie'
            ])
            ->add('revue',EntityType::class, [
                'class' => Revue::class,
                'choice_label' => 'nomRevue'
            ])

            ->add('auteurs', GroupeAuteurType::class, [
                'data_class' => null,
                'mapped' => true
        ])
        ;
    }

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

我的 Twig 模板:

{% extends 'base.html.twig' %}

{% block title %}Enregistrer{% endblock %}

{% block body %}

    <div class="container">
        <fieldset class="border col-md-6">
            <legend class="col-md-3"> Article</legend>
            {{ form_start(formArticle, {'attr': {'novalidate': 'novalidate'}}) }}

            <div class="form-group required">
                {{ form_label(formArticle.categorie, "categorie :", {'label_attr': {'class': 'col-sm-6 control-label requis'}}) }}
                <div class="col-sm-9">
                    {{ form_widget(formArticle.categorie) }}
                </div>
            </div>

            <div class="form-group required">
                {{ form_label(formArticle.revue, "revue :", {'label_attr': {'class': 'col-sm-6 control-label requis'}}) }}
                <div class="col-sm-9">
                    {{ form_widget(formArticle.revue) }}
                </div>
            </div>

            <div class="form-group  required">
                {{ form_label(formArticle.title, "Title : ", {'label_attr': {'class': 'col-sm-6 control-label requis'}}) }}
                <div class="col-sm-9">
                    {{ form_widget(formArticle.title,{'attr':{'class':'form-control'}}) }}
                </div>
            </div>

            <div class="form-group required">
                {{ form_label(formArticle.abstract, "abstract :", {'label_attr': {'class': 'col-sm-6 control-label requis'}}) }}
                <div class="col-sm-9">
                    {{ form_widget(formArticle.abstract) }}
                </div>
            </div>

            <div class="form-group required">
                {{ form_label(formArticle.keyword, "keyword :", {'label_attr': {'class': 'col-sm-6 control-label requis'}}) }}
                <div class="col-sm-9">
                    {{ form_widget(formArticle.keyword) }}
                </div>
            </div>

            <div class="form-group required">
                {{ form_label(formArticle.content, "content :", {'label_attr': {'class': 'col-sm-6 control-label requis'}}) }}
                <div class="col-sm-9">
                    {{ form_widget(formArticle.content) }}
                </div>
            </div>

            <div class="form-group required">
                {{ form_label(formArticle.size, "size :", {'label_attr': {'class': 'col-sm-6 control-label requis'}}) }}
                <div class="col-sm-9">
                    {{ form_widget(formArticle.size) }}
                </div>
            </div>
        </fieldset>
        <div class="form-group required">
            <div class="col-sm-9">
                <fieldset class="border col-md-6">
                    <legend class="col-md-3"> Auteur</legend>
                    {{ form_row(formArticle.auteurs) }}
                </fieldset>
                <p  style="margin-top: -50px;margin-right: 180px!important;float: right!important;">
                    <button type="submit" class="btn btn-primary float-right mr-5" id="articleStatus"> Enregistrer</button>
                </p>
            </div>
        </div>
        {{ form_end(formArticle) }}
    </div>
{% endblock %}

*我通过添加初始化 articleType 中的 Collection auteurs 解决了这个问题 这行 *

       $builder->addEventListener(FormEvents::PRE_SET_DATA, function (FormEvent $event) {
            $event->getData()->addAuteur(new GroupeAuteur());
        });

我希望这个解决方案可以帮助其他人