警告:spl_object_hash()期望参数1是对象,给定Collection表单的数组

时间:2015-04-19 14:19:20

标签: forms symfony collections

我为Web应用程序的每个用户创建了一个CV的集合表单 每个简历都包含许多经验,语言,形成,竞争力!

并添加一个Jquery代码以在集合表单中使用data-prototype,这样用户就可以向他的cv添加多个经验和形成...

现在一切都还可以,我的表格很好,但问题何时提交!

我收到此错误: 警告:spl_object_hash()期望参数1为object,给定数组为

这是我的yml for doctrine:

cv.orm.yml:

cyn\UserBundle\Entity\Cv:
type: entity
table: cv
id:
    id:
        type: integer
        generator: { strategy: AUTO }
fields:
    expenmois:
       type: integer
    divers:
       type: text
oneToMany:
    experience:
        targetEntity: Experience
        mappedBy: cv
        cascade: ["persist", "merge"]
    formation:
        targetEntity: Formation
        mappedBy: cv
        cascade: ["persist", "merge"]
    competance:
        targetEntity: Competance 
        mappedBy: cv
        cascade: ["persist", "merge"]
    langue:
        targetEntity: Langue 
        mappedBy: cv
        cascade: ["persist", "merge"]

Experience.orm.yml:

cyn\UserBundle\Entity\Experience:
type: entity
table: experience
id:
    id:
        type: integer
        generator: { strategy: AUTO }
fields:
    nom_societe:
        type: string
        length: 255
        nullable: false
    lieutravail:
        type: string
        length: 255
        nullable: false
    poste:
        type: string
        length: 255
        nullable: false
    datedebut:
        type: date 
    datefin:
        type: date
    description:
        type: text
manyToOne:
    cv:
        targetEntity: Cv 
        inversedBy: experience
        joinColumn:
            name: cv_id
            referencedColumnName: id

formation.orm.yml:

cyn\UserBundle\Entity\Formation:
type: entity
table: formation
id:
    id:
        type: integer
        generator: { strategy: AUTO }
fields:
    etablissement:
        type: string
        length: 255
        nullable: false
    datedebut:
        type: date 
    datefin:
        type: date
manyToOne:
    cv:
        targetEntity: Cv 
        inversedBy: formation
        joinColumn:
            name: cv_id
            referencedColumnName: id

competance.orm.yml:

cyn\UserBundle\Entity\Competance:
type: entity
table: competance 
id:
    id:
        type: integer
        generator: { strategy: AUTO }
fields:
    domaine:
        type: string
        length: 255
    details:
        type: string
        length: 255
manyToOne:
    cv:
        targetEntity: Cv 
        inversedBy: competance 
        joinColumn:
            name: cv_id
            referencedColumnName: id

langue.orm.yml

cyn\UserBundle\Entity\Langue:
type: entity
table: langue  
id:
    id:
        type: integer
        generator: { strategy: AUTO }
fields:
    langue:
        type: string
        length: 255
    niveau:
        type: string
        length: 255
manyToOne:
    cv:
        targetEntity: Cv 
        inversedBy: langue
        joinColumn:
            name: cv_id
            referencedColumnName: id

这是我的收藏表格类型:

    <?php

namespace cyn\UserBundle\Form\Type;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use cyn\UserBundle\Entity\Cv;
use cyn\UserBundle\Entity\Experience;
use cyn\UserBundle\Entity\Competance;
use cyn\UserBundle\Entity\Langue;
use cyn\UserBundle\Entity\Formation;

class CvType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {

        $builder->add('expEnMois', 'text');
        $builder->add('divers', 'textarea');
        $builder->add('experience', 'collection',array(
                'type'=> new ExperienceType(),
                'allow_add'=>true,
                'allow_delete' => true )
                );


        $builder->add('Enregistrer', 'submit');
    }

    public function getDefaultOptions(array $options)
    {
        return array(
            'data_class' => 'cyn\UserBundle\Entity\Cv',



        );
    }

    public function getName()
    {
        return 'cv';
    }
}

这是我的添加控制器:

<?php

namespace cyn\UserBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Doctrine\Common\Collections\Collection;

use cyn\UserBundle\Form\Type\CvType;
use cyn\UserBundle\Entity\Cv;
use cyn\UserBundle\Entity\Competance;
use cyn\UserBundle\Entity\Langue;
use cyn\UserBundle\Entity\Experience;
use cyn\UserBundle\Entity\Formation;

use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;

class CvController extends Controller
{

    public function addAction()
    {
        $cv = new Cv();
        $exp = new Experience();
        $exp->setCv($cv);

        $form =  $this->get('form.factory')->create(new CvType(), $cv);


        $request = $this->getRequest();

        if ('POST' === $request->getMethod()) {
            $form->handleRequest($request);

            if ($form->isValid()) {
                $em = $this->getDoctrine()->getManager();

                $em->persist($cv);
                $em->flush();

                $this->get('session')->getFlashBag()->add('notice', 'Félicitations, votre cv a bien été enregistré.' );



            }
        }

        return $this->render('cynUserBundle:Default:ajoutercv.html.twig', array(
            'form' => $form->createView(),
        ));
    }

}

我的简历实体:

<?php

namespace cyn\UserBundle\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * Cv
 */
class Cv
{
    /**
     * @var integer
     */
    private $id;

    /**
     * @var integer
     */
    private $expenmois;

    /**
     * @var string
     */
    private $divers;

    /**
     * @var \Doctrine\Common\Collections\Collection
     */
    private $experience;

    /**
     * @var \Doctrine\Common\Collections\Collection
     */
    private $formation;

    /**
     * @var \Doctrine\Common\Collections\Collection
     */
    private $competance;

    /**
     * @var \Doctrine\Common\Collections\Collection
     */
    private $langue;

    /**
     * Constructor
     */
    public function __construct()
    {
        $this->experience = new \Doctrine\Common\Collections\ArrayCollection();
        $this->formation = new \Doctrine\Common\Collections\ArrayCollection();
        $this->competance = new \Doctrine\Common\Collections\ArrayCollection();
        $this->langue = new \Doctrine\Common\Collections\ArrayCollection();
    }

    /**
     * Get id
     *
     * @return integer 
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * Set expenmois
     *
     * @param integer $expenmois
     * @return Cv
     */
    public function setExpenmois($expenmois)
    {
        $this->expenmois = $expenmois;

        return $this;
    }

    /**
     * Get expenmois
     *
     * @return integer 
     */
    public function getExpenmois()
    {
        return $this->expenmois;
    }

    /**
     * Set divers
     *
     * @param string $divers
     * @return Cv
     */
    public function setDivers($divers)
    {
        $this->divers = $divers;

        return $this;
    }

    /**
     * Get divers
     *
     * @return string 
     */
    public function getDivers()
    {
        return $this->divers;
    }

    /**
     * Add experience
     *
     * @param \cyn\UserBundle\Entity\Experience $experience
     * @return Cv
     */
    public function addExperience(\cyn\UserBundle\Entity\Experience $experience)
    {
        $this->experience[] = $experience;

        return $this;
    }

    /**
     * Remove experience
     *
     * @param \cyn\UserBundle\Entity\Experience $experience
     */
    public function removeExperience(\cyn\UserBundle\Entity\Experience $experience)
    {
        $this->experience->removeElement($experience);
    }

    /**
     * Get experience
     *
     * @return \Doctrine\Common\Collections\Collection 
     */
    public function getExperience()
    {
        return $this->experience;
    }

    /**
     * Add formation
     *
     * @param \cyn\UserBundle\Entity\Formation $formation
     * @return Cv
     */
    public function addFormation(\cyn\UserBundle\Entity\Formation $formation)
    {
        $this->formation[] = $formation;

        return $this;
    }

    /**
     * Remove formation
     *
     * @param \cyn\UserBundle\Entity\Formation $formation
     */
    public function removeFormation(\cyn\UserBundle\Entity\Formation $formation)
    {
        $this->formation->removeElement($formation);
    }

    /**
     * Get formation
     *
     * @return \Doctrine\Common\Collections\Collection 
     */
    public function getFormation()
    {
        return $this->formation;
    }

    /**
     * Add competance
     *
     * @param \cyn\UserBundle\Entity\Competance $competance
     * @return Cv
     */
    public function addCompetance(\cyn\UserBundle\Entity\Competance $competance)
    {
        $this->competance[] = $competance;

        return $this;
    }

    /**
     * Remove competance
     *
     * @param \cyn\UserBundle\Entity\Competance $competance
     */
    public function removeCompetance(\cyn\UserBundle\Entity\Competance $competance)
    {
        $this->competance->removeElement($competance);
    }

    /**
     * Get competance
     *
     * @return \Doctrine\Common\Collections\Collection 
     */
    public function getCompetance()
    {
        return $this->competance;
    }

    /**
     * Add langue
     *
     * @param \cyn\UserBundle\Entity\Langue $langue
     * @return Cv
     */
    public function addLangue(\cyn\UserBundle\Entity\Langue $langue)
    {
        $this->langue[] = $langue;

        return $this;
    }

    /**
     * Remove langue
     *
     * @param \cyn\UserBundle\Entity\Langue $langue
     */
    public function removeLangue(\cyn\UserBundle\Entity\Langue $langue)
    {
        $this->langue->removeElement($langue);
    }

    /**
     * Get langue
     *
     * @return \Doctrine\Common\Collections\Collection 
     */
    public function getLangue()
    {
        return $this->langue;
    }
}

这是我的其他实体经验之一.php:

<?php

namespace cyn\UserBundle\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * Experience
 */
class Experience
{
    /**
     * @var integer
     */
    private $id;

    /**
     * @var string
     */
    private $nom_societe;

    /**
     * @var string
     */
    private $lieutravail;

    /**
     * @var string
     */
    private $poste;

    /**
     * @var \DateTime
     */
    private $datedebut;

    /**
     * @var \DateTime
     */
    private $datefin;

    /**
     * @var string
     */
    private $description;

    /**
     * @var \cyn\UserBundle\Entity\Cv
     */
    private $cv;


    /**
     * Get id
     *
     * @return integer 
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * Set nom_societe
     *
     * @param string $nomSociete
     * @return Experience
     */
    public function setNomSociete($nomSociete)
    {
        $this->nom_societe = $nomSociete;

        return $this;
    }

    /**
     * Get nom_societe
     *
     * @return string 
     */
    public function getNomSociete()
    {
        return $this->nom_societe;
    }

    /**
     * Set lieutravail
     *
     * @param string $lieutravail
     * @return Experience
     */
    public function setLieutravail($lieutravail)
    {
        $this->lieutravail = $lieutravail;

        return $this;
    }

    /**
     * Get lieutravail
     *
     * @return string 
     */
    public function getLieutravail()
    {
        return $this->lieutravail;
    }

    /**
     * Set poste
     *
     * @param string $poste
     * @return Experience
     */
    public function setPoste($poste)
    {
        $this->poste = $poste;

        return $this;
    }

    /**
     * Get poste
     *
     * @return string 
     */
    public function getPoste()
    {
        return $this->poste;
    }

    /**
     * Set datedebut
     *
     * @param \DateTime $datedebut
     * @return Experience
     */
    public function setDatedebut($datedebut)
    {
        $this->datedebut = $datedebut;

        return $this;
    }

    /**
     * Get datedebut
     *
     * @return \DateTime 
     */
    public function getDatedebut()
    {
        return $this->datedebut;
    }

    /**
     * Set datefin
     *
     * @param \DateTime $datefin
     * @return Experience
     */
    public function setDatefin($datefin)
    {
        $this->datefin = $datefin;

        return $this;
    }

    /**
     * Get datefin
     *
     * @return \DateTime 
     */
    public function getDatefin()
    {
        return $this->datefin;
    }

    /**
     * Set description
     *
     * @param string $description
     * @return Experience
     */
    public function setDescription($description)
    {
        $this->description = $description;

        return $this;
    }

    /**
     * Get description
     *
     * @return string 
     */
    public function getDescription()
    {
        return $this->description;
    }

    /**
     * Set cv
     *
     * @param \cyn\UserBundle\Entity\Cv $cv
     * @return Experience
     */
    public function setCv(\cyn\UserBundle\Entity\Cv $cv = null)
    {
        $this->cv = $cv;

        return $this;
    }

    /**
     * Get cv
     *
     * @return \cyn\UserBundle\Entity\Cv 
     */
    public function getCv()
    {
        return $this->cv;
    }
}

experiencetype.php:

<?php

namespace cyn\UserBundle\Form\Type;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use cyn\UserBundle\Entity\Experience;

class ExperienceType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {

        $builder->add('nomsociete', 'text');
        $builder->add('lieutravail', 'text');
        $builder->add('poste', 'text');
        $builder->add('datedebut','birthday');
        $builder->add('datefin', 'birthday');
        $builder->add('description', 'text');


    }

    public function getDefaultOptions(array $options)
    {
        return array(
            'data_class' => 'cyn\UserBundle\Entity\Experience',
        );
    }

    public function getName()
    {
        return 'experience';
    }
}

请帮助我,我已经1周了,我无法解决这个问题

1 个答案:

答案 0 :(得分:0)

此错误表示对象包含实体中不存在的字段。使用自定义表单类型时,必须使用数据转换器,或在preUpdate中手动更改它们。检查对象,查看使用自定义类型后显示的外部字段的内容。

echo "<pre>";
\Doctrine\Common\Util\Debug::dump();
echo "</pre>";
die;

在preUpdate函数中使用此代码。