警告:spl_object_hash()期望参数1为object,给定字符串

时间:2016-11-17 17:55:01

标签: symfony doctrine-orm

我有这个问题“警告:spl_object_hash()期望参数1是对象,给出字符串”当我尝试保存我的关系的onetomay实体方面时,下面的代码可能会给你一些想法:

<?php

namespace BackendBundle\Entity;

use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Mapping as ORM;


/**
 * BulletinSalaire
 *
 * @ORM\Table(name="bulletin_salaire")
 * @ORM\Entity(repositoryClass="BackendBundle\Repository\BulletinSalaireRepository")
 */
class BulletinSalaire
{
    /**
     * @var int
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;


    /**
     * @var \DateTime
     *
     * @ORM\Column(name="periode", type="date")
     */
    private $periode;

    /**
     * @var string
     *
     * @ORM\Column(name="mode_reglement", type="string", length=255)
     */
    private $modeReglement;

    /**
     *
     * @ORM\ManyToOne(targetEntity="Employee", inversedBy="employees", cascade={"persist"})
     * @ORM\JoinColumn(name="id_employee", referencedColumnName="id")
     */

    private $employee;

    /**
     *
     * @ORM\OneToMany(targetEntity="LibelleBulletin", mappedBy="bulletinsalaire", cascade={"persist"})
     * @ORM\JoinColumn(name="id_libelleBulltin", referencedColumnName="id")
     */

    private $libelleBulletins;

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

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

    /**
     * Set libelle
     *
     * @param string $libelle
     *
     * @return BulletinSalaire
     */
    public function setLibelle($libelle)
    {
        $this->libelle = $libelle;

        return $this;
    }

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

    /**
     * Set base
     *
     * @param integer $base
     *
     * @return BulletinSalaire
     */
    public function setBase($base)
    {
        $this->base = $base;

        return $this;
    }

    /**
     * Get base
     *
     * @return int
     */
    public function getBase()
    {
        return $this->base;
    }

    /**
     * Set retenues
     *
     * @param float $retenues
     *
     * @return BulletinSalaire
     */
    public function setRetenues($retenues)
    {
        $this->retenues = $retenues;

        return $this;
    }

    /**
     * Get retenues
     *
     * @return float
     */
    public function getRetenues()
    {
        return $this->retenues;
    }

    /**
     * Set periode
     *
     * @param \DateTime $periode
     *
     * @return BulletinSalaire
     */
    public function setPeriode($periode)
    {
        $this->periode = $periode;

        return $this;
    }

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

    /**
     * Set modeReglement
     *
     * @param string $modeReglement
     *
     * @return BulletinSalaire
     */
    public function setModeReglement($modeReglement)
    {
        $this->modeReglement = $modeReglement;

        return $this;
    }

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

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

    /**
     * @param mixed $employee
     */
    public function setEmployee($employee)
    {
        $this->employee = $employee;
    }


    /**
     * @param LibelleBulletin $libellebulletin
     * @return $this
     */
    public function addLibelleBulletin(LibelleBulletin $libellebulletin)
    {
        $this->libelleBulletins[] = $libellebulletin;
        $libellebulletin->setBulletinSalaire($this);

        return $this;
    }

    /**
     * @param LibelleBulletin $libellebulletin
     */
    public function removeLibelleBulletin(LibelleBulletin $libellebulletin)
    {
        $this->libelleBulletins->removeElement($libellebulletin);
    }

    public function getLibelleBulletins()
    {
        return $this->libelleBulletins;
    }

}

    /---------------------Entity Many libelle to one bulletin de salaire ----------------/
<?php
/**
 * Created by PhpStorm.
 * User: achouri
 * Date: 17/11/2016
 * Time: 4:05 PM
 */

namespace BackendBundle\Entity;

use Doctrine\ORM\Mapping as ORM;


/**
 * BulletinSalaire
 *
 * @ORM\Table(name="libelle_bulletins")
 * @ORM\Entity()
 */

class LibelleBulletin
{
    /**
     * @var int
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;
    /**
     * @ORM\ManyToOne(targetEntity="BulletinSalaire", inversedBy="libellebulletins")
     * @ORM\JoinColumn(nullable=false)
     */
    private $bulletinSalaire;

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

    /**
     * @var int
     *
     * @ORM\Column(name="base", type="integer")
     */
    private $base;

    /**
     * @var float
     *
     * @ORM\Column(name="retenues", type="float")
     */
    private $retenues;

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

    /**
     * @param int $id
     */
    public function setId($id)
    {
        $this->id = $id;
    }

    /**
     * @return string
     */
    public function getLibelle()
    {
        return $this->libelle;
    }

    /**
     * @param string $libelle
     */
    public function setLibelle($libelle)
    {
        $this->libelle = $libelle;
    }

    /**
     * @return int
     */
    public function getBase()
    {
        return $this->base;
    }

    /**
     * @param int $base
     */
    public function setBase($base)
    {
        $this->base = $base;
    }

    /**
     * @return float
     */
    public function getRetenues()
    {
        return $this->retenues;
    }

    /**
     * @param float $retenues
     */
    public function setRetenues($retenues)
    {
        $this->retenues = $retenues;
    }

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

    /**
     * @param mixed $bulletinSalaire
     */
    public function setBulletinSalaire($bulletinSalaire)
    {
        $this->bulletinSalaire = $bulletinSalaire;
    }


}

    /---------------------bulletinsallaireType---------------------------/
<?php

namespace BackendBundle\Form;

use Symfony\Bridge\Doctrine\Form\Type\EntityType;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use BackendBundle\Form\LibelleBulletinType;
class BulletinSalaireType extends AbstractType
{
    /**
     * {@inheritdoc}
     */
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder->add('libelleBulletins',LibelleBulletinType::class,array('label' => '','data_class' => null))
            ->add('periode')->add('modeReglement')
            ->add('employee',EntityType::class,array('attr'   =>  array(
            'class'   => 'form-control'),
            'class' => 'BackendBundle:Employee',
            'choice_label' => function ($employee) {
                return $employee->getNom()." ".$employee->getPrenom();
            }));
    }

    /**
     * {@inheritdoc}
     */
    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' => 'BackendBundle\Entity\BulletinSalaire'
        ));
    }

    /**
     * {@inheritdoc}
     */
    public function getBlockPrefix()
    {
        return 'backendbundle_bulletinsalaire';
    }


}
/------------------------libelleBultinType-----------------------/
<?php

namespace BackendBundle\Form;

use Symfony\Bridge\Doctrine\Form\Type\EntityType;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use BackendBundle\Form\EmployeeType;
class LibelleBulletinType extends AbstractType
{
    /**
     * {@inheritdoc}
     */
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder->add('libelle')->add('base')->add('retenues');
    }

    /**
     * {@inheritdoc}
     */
    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' => 'BackendBundle\Entity\LibelleBulletin'
        ));
    }

    /**
     * {@inheritdoc}
     */
    public function getBlockPrefix()
    {
        return 'backendbundle_libellebulletin';
    }


}
/----------------------Action du save-----------------/
    /**
     * Creates a new bulletinSalaire entity.
     *
     * @Route("/new", name="bulletinsalaire_new")
     * @Method({"GET", "POST"})
     */
    public function newAction(Request $request)
    {
        $bulletinSalaire = new Bulletinsalaire();
        $libellebulletin= new LibelleBulletin();
        $form = $this->createForm('BackendBundle\Form\BulletinSalaireType', $bulletinSalaire);
        $form->handleRequest($request);

        if ($form->isSubmitted() && $form->isValid()) {
            $em = $this->getDoctrine()->getManager();
            $bulletinSalaire->addLibelleBulletin($libellebulletin);
            $em->persist($bulletinSalaire);
            $em->flush($bulletinSalaire);

            return $this->redirectToRoute('bulletinsalaire_show', array('id' => $bulletinSalaire->getId()));
        }

        return $this->render('BackendBundle:bulletinsalaire:new.html.twig', array(
            'bulletinSalaire' => $bulletinSalaire,
            'form' => $form->createView(),
        ));
    }

1 个答案:

答案 0 :(得分:0)

正如@xabbuh指出的那样,我们需要看看如何创建对象。看起来你试图用字符串而不是对象设置$ employee或$ libelleBulletins。

错误

$bulletin->setEmployee('some value');

正确

$employee = $dm->getRepository('BackendBundle:Employee')->findOneById('employeeId');

$bulletin->setEmployee($employee);