symfony - Warning: spl_object_hash() expects parameter 1 to be object, string given -
i have issues "warning: spl_object_hash() expects parameter 1 object, string given" when try save onetomay entity side of relationship, code below may give ideas:
<?php namespace backendbundle\entity; use doctrine\common\collections\arraycollection; use doctrine\orm\mapping 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(); } /** * 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; } /** * 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; } /** * 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; } /** * 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; } /** * 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; } /** * 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 1 bulletin de salaire ----------------/ <?php /** * created phpstorm. * user: achouri * date: 17/11/2016 * time: 4:05 pm */ namespace backendbundle\entity; use doctrine\orm\mapping 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 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(), )); }
as @xabbuh pointed, need see how create object. looks trying set $employee or $libellebulletins string instead of object.
this wrong.
$bulletin->setemployee('some value');
this right.
$employee = $dm->getrepository('backendbundle:employee')->findonebyid('employeeid'); $bulletin->setemployee($employee);
Comments
Post a Comment