当我尝试保留我的对象并刷新它时,我收到此错误消息:
警告:spl_object_hash()期望参数1为object,integer 给出500内部服务器错误 - ContextErrorException
我知道在堆栈溢出中已经发布了很多这样的问题,但它仍然无法解决我的问题。这就是为什么我在这里再问一次,希望有人可以帮助我。
以下是我保留用户类的代码:
$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();
以下是我的用户类:
/**
* 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 be needed, see section on salt below
// $this->salt = md5(uniqid(null, true));
}
/**
* @inheritDoc
*/
public function getUsername() {
return $this->username;
}
/**
* @inheritDoc
*/
public function getSalt() {
// you *may* need a real salt depending on your 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);
}
/**
* Get 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;
}
/**
* Get 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;
}
/**
* Get name
*
* @return string
*/
public function getName() {
return $this->name;
}
/**
* Set parent_id
*
* @param integer $parentId
*/
public function setParentId($parentId) {
$this->parent_id = $parentId;
}
/**
* Get 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;
}
/**
* Get 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;
}
/**
* Get 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;
}
/**
* Get 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;
}
/**
* Get status
*
* @return string
*/
public function getStatus() {
return $this->status;
}
/**
* Set created_at
*
* @param \DateTime $createdAt
*/
public function setCreatedAt($createdAt) {
$this->created_at = $createdAt;
}
/**
* Get 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;
}
/**
* Get 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;
}
/**
* Get 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);
}
/**
* Get 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);
}
/**
* Get 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;
}
/**
* Get 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;
}
/**
* Get cms_prize
*
* @return integer
*/
public function getCmsPrize() {
return $this->cms_prize;
}
}
以下是加载的用户对象(当前用户)的转储:
以下是新用户对象的转储:
错误日志:
CRITICAL - 未捕获的PHP异常 Symfony \ Component \ Debug \ Exception \ ContextErrorException:“警告: spl_object_hash()期望参数1是对象,给定“at”的整数 C:\ XAMPP \ htdocs中\项目\供应商\原则\ ORM \ LIB \原则\ ORM \ UnitOfWork.php 1389行
我能看到的另一件事是$ child,$ roles和$ packages变量的数据类型。它有什么不同吗?对于变量子,我没有设置任何东西,因为它不属于数据库的变量。它仅用于自引用,$ parent_id与此$ child具有自引用关系,所以我只设置$ parent_id。
我真的不知道,也许我对自我引用有错误的概念。
感谢您的帮助。
答案 0 :(得分:5)
看一下注释,我的猜测是User :: setParentId()期望一个User对象,而不是一个整数:
/**
* @ORM\ManyToOne(targetEntity="User", inversedBy="children")
* @ORM\JoinColumn(name="parent_id", referencedColumnName="id", nullable=true)
*
*/
所以不要传递
$new_user->setParentId($currentuser->getId());
尝试:
$new_user->setParentId($currentuser);
如果可行,则应将字段名称适当更改为parent而不是parent_id。