php - Symfony2 Inserting Error "Warning: spl_object_hash() expects parameter..." -
when try persist object , flush error message:
warning: spl_object_hash() expects parameter 1 object, integer given 500 internal server error - contexterrorexception
i know kind of question has been posted lot in stack overflow still couldn't solve problem. that's why ask again here, 1 can me.
below code persist user class:
$package = $em->getrepository('mybundle:package')->findoneby(array('id' => 1)); $new_user->addpackage($package); $role = $em->getrepository('mybundle:role')->findoneby(array('id' => 3)); $new_user->addrole($role); $new_user->setcmsprize(2); //int $new_user->setcmsbet(3); //int $new_user->setname("new user"); $new_user->setusername("test123"); $new_user->setpassword("abc"); $new_user->setcreditlimit(1000); $new_user->setcreditbalance(2000); $new_user->setselectedpackage($currentuser->getselectedpackage()); $new_user->setparentid($currentuser->getid()); $new_user->setlayer($currentuser->getlayer() + 1); $em->persist($new_user); $em->flush();
below user class:
/** * mybundle\entity\user * * @orm\table(name="") * @orm\entity(repositoryclass="mybundle\entity\userrepository") */ class user implements advanceduserinterface, \serializable { /** * @orm\column(type="integer") * @orm\id * @orm\generatedvalue(strategy="auto") */ private $id; /** * @orm\column(type="string", length=25, unique=true) */ private $username; /** * @orm\column(type="string", length=64, options={"fixed" = true})) */ private $password; /** * @orm\column(type="string", length=100) */ private $name; /** * @orm\onetomany(targetentity="user", mappedby="parent_id") * */ private $children; /** * @orm\manytoone(targetentity="user", inversedby="children") * @orm\joincolumn(name="parent_id", referencedcolumnname="id", nullable=true) * */ private $parent_id; /** * @orm\column(name="layer", type="integer") */ private $layer; /** * @orm\column(name="credit_limit", type="float") */ private $credit_limit; /** * @orm\column(name="credit_balance", type="float") */ private $credit_balance; /** * @orm\column(name="cms_bet", type="integer") */ private $cms_bet; /** * @orm\column(name="cms_prize", type="integer") */ private $cms_prize; /** * @orm\onetoone(targetentity="onetoonepackage", inversedby="users") * @orm\joincolumn(name="selected_package", referencedcolumnname="id") */ private $selected_package; /** * @orm\column(name="is_allow_open_acc", type="boolean") */ private $is_allow_open_acc; /** * @orm\column(name="status", type="string", length=10) */ private $status; /** * @orm\column(name="created_at", type="datetime") */ private $created_at; /** * @orm\manytomany(targetentity="role", inversedby="users") * */ private $roles; /** * @orm\manytomany(targetentity="package", inversedby="users") * */ private $packages; public function __construct() { //$this->isactive = true; $this->roles = new arraycollection(); $this->packages = new arraycollection(); $this->created_at = new \datetime('now');//date('y-m-d h:i:s'); $this->credit_balance = 0; $this->credit_limit = 0; $this->status = 'active'; $this->is_allow_open_acc = true; $this->children = new arraycollection(); // may not needed, see section on salt below // $this->salt = md5(uniqid(null, true)); } /** * @inheritdoc */ public function getusername() { return $this->username; } /** * @inheritdoc */ public function getsalt() { // *may* need real salt depending on encoder // see section on salt below return null; } /** * @inheritdoc */ public function getpassword() { return $this->password; } /** * @inheritdoc */ public function getroles() { return $this->roles->toarray(); } /** * @inheritdoc */ public function erasecredentials() { } /** * @see \serializable::serialize() */ public function serialize() { return serialize(array( $this->id, $this->username, $this->password, $this->name, $this->is_allow_open_acc, $this->created_at, $this->credit_balance, $this->credit_limit, $this->parent_id, $this->status, $this->cms_bet, $this->cms_prize, // see section on salt below // $this->salt, )); } /** * @see \serializable::unserialize() */ public function unserialize($serialized) { list ( $this->id, $this->username, $this->password, $this->name, $this->is_allow_open_acc, $this->created_at, $this->credit_balance, $this->credit_limit, $this->parent_id, $this->status, $this->cms_bet, $this->cms_prize, // see section on salt below // $this->salt ) = unserialize($serialized); } /** * id * * @return integer */ public function getid() { return $this->id; } /** * set username * * @param string $username */ public function setusername($username) { $this->username = $username; } /** * set password * * @param string $password */ public function setpassword($password) { $this->password = $password; } /** * isactive * * @return boolean */ public function getisactive() { if ($this->status == 'active') { return true; } else { return false; } } public function isaccountnonexpired() { return true; } public function isaccountnonlocked() { return true; } public function iscredentialsnonexpired() { return true; } public function isenabled() { return $this->getisactive(); } /** * add roles * * @param \mybundle\entity\role $roles */ public function addrole(\mybundle\entity\role $roles) { $this->roles[] = $roles; } /** * remove roles * * @param \mybundle\entity\role $roles */ public function removerole(\mybundle\entity\role $roles) { $this->roles->removeelement($roles); } /** * set name * * @param string $name * @return user */ public function setname($name) { $this->name = $name; return $this; } /** * name * * @return string */ public function getname() { return $this->name; } /** * set parent_id * * @param integer $parentid */ public function setparentid($parentid) { $this->parent_id = $parentid; } /** * parent_id * * @return integer */ public function getparentid() { return $this->parent_id; } /** * set credit_limit * * @param float $creditlimit */ public function setcreditlimit($creditlimit) { $this->credit_limit = $creditlimit; } /** * credit_limit * * @return float */ public function getcreditlimit() { return $this->credit_limit; } /** * set credit_balance * * @param float $creditbalance */ public function setcreditbalance($creditbalance) { $this->credit_balance = $creditbalance; } /** * credit_balance * * @return float */ public function getcreditbalance() { return $this->credit_balance; } /** * set is_allow_open_acc * * @param boolean $isallowopenacc */ public function setisallowopenacc($isallowopenacc) { $this->is_allow_open_acc = $isallowopenacc; } /** * is_allow_open_acc * * @return boolean */ public function getisallowopenacc() { return $this->is_allow_open_acc; } /** * set status * * @param string $status */ public function setstatus($status) { $this->status = $status; } /** * status * * @return string */ public function getstatus() { return $this->status; } /** * set created_at * * @param \datetime $createdat */ public function setcreatedat($createdat) { $this->created_at = $createdat; } /** * created_at * * @return \datetime */ public function getcreatedat() { return $this->created_at; } /** * set layer * * @param integer $layer * @return user */ public function setlayer($layer) { $this->layer = $layer; return $this; } /** * layer * * @return integer */ public function getlayer() { return $this->layer; } /** * set selected_package * * @param integer $selectedpackage * @return user */ public function setselectedpackage($selectedpackage) { $this->selected_package = $selectedpackage; return $this; } /** * selected_package * * @return integer */ public function getselectedpackage() { return $this->selected_package; } /** * add children * * @param \mybundle\entity\user $children * @return user */ public function addchild(\mybundle\entity\user $children) { $this->children[] = $children; return $this; } /** * remove children * * @param \mybundle\entity\user $children */ public function removechild(\mybundle\entity\user $children) { $this->children->removeelement($children); } /** * children * * @return \doctrine\common\collections\collection */ public function getchildren() { return $this->children; } /** * add package * * @param \mybundle\entity\package $package * @return user */ public function addpackage(\mybundle\entity\package $package) { $this->packages[] = $package; return $this; } /** * remove package * * @param \mybundle\entity\package $package */ public function removepackage(\mybundle\entity\package $package) { $this->packages->removeelement($package); } /** * packages * * @return \doctrine\common\collections\collection */ public function getpackages() { return $this->packages; } /** * set packages * * @param collection $packages * @return user */ public function setpackages($packages) { $this->packages = $packages; return $this; } /** * set cms_bet * * @param integer $cmsbet * @return user */ public function setcmsbet($cmsbet) { $this->cms_bet = $cmsbet; return $this; } /** * cms_bet * * @return integer */ public function getcmsbet() { return $this->cms_bet; } /** * set cms_prize * * @param integer $cmsprize * @return user */ public function setcmsprize($cmsprize) { $this->cms_prize = $cmsprize; return $this; } /** * cms_prize * * @return integer */ public function getcmsprize() { return $this->cms_prize; } }
below dump of loaded user object (current user):
below dump of new user object:
the error log:
critical - uncaught php exception symfony\component\debug\exception\contexterrorexception: "warning: spl_object_hash() expects parameter 1 object, integer given" @ c:\xampp\htdocs\project\vendor\doctrine\orm\lib\doctrine\orm\unitofwork.php line 1389
the different thing can see data type of $child, $roles , $packages variable. make different? variable child didn't set because not belong variable database. self-referencing usage, $parent_id has self-referencing relationship $child set $parent_id.
i got no idea, maybe have wrong concept self-referencing.
thank your help.
looking @ annotations, guess user::setparentid() expecting user object, not integer:
/** * @orm\manytoone(targetentity="user", inversedby="children") * @orm\joincolumn(name="parent_id", referencedcolumnname="id", nullable=true) * */
so instead of passing
$new_user->setparentid($currentuser->getid());
try:
$new_user->setparentid($currentuser);
if works, field name should changed appropriately parent instead of parent_id.
Comments
Post a Comment