我有 3 个实体:用户、用户角色、用户配置文件。我的用户配置文件实体的主键实际上是来自用户实体的外键,即 UserId。 每次我尝试使用外键用户 ID 访问 userProfile 时。我总是收到未定义用户 ID 索引的错误。
这是确切的错误信息
**> 注意:未定义索引:userID in
C:\wamp\www\SymfonyFolder\vendor\doctrine\orm\lib\Doctrine\ORM\UnitOfWork.php 行 2583**
控制器 ..... ....
public function profileAction($id)
{
$user = new User();
$user_repository = $this->getDoctrine()->getRepository('AcmePodUserBundle:User');
$user = $user_repository->find($id);
$userprofile = new UserProfile();
$userprofile_repository = $this->getDoctrine()->getRepository('AcmeUserBundle:UserProfile');
$userprofile = $userprofile_repository->find($user);
return new Response($userprofile); // this is not the actual output i need. I just used this simply to test if userprofile has some value.
}
用户.orm.yml
type: entity
table: reg_user
oneToOne:
id:
userID:
column: user_id
type: integer
generator: { strategy: AUTO }
targetEntity: UserProfile
mappedBy: userProfileID
用户配置文件.orm.yml
type: entity
table: users_profile
id:
userProfileID:
associationKey: true
fields:
firstName:
column: first_name
type: string
length: 255
unique: true
nullable: false
lastName:
column: last_name
type: string
length: 255
nullable: false
oneToOne:
userProfileID:
targetEntity: User
inversedBy: userID
joinColumn:
name: fk_user_id
referencedColumnName: user_id
用户.php
class User implements AdvancedUserInterface, \Serializable
{
// @var auto integer
protected $userID;
// @var string
protected $userName;
// @var string
protected $salt;
// @var string - must not persist to DB
protected $plainPassword;
// @var string
protected $password;
// @var string
protected $email;
// @var boolean
protected $isActive;
// @var string
protected $confirmationCode;
// @var datetime
protected $createdAt;
// @var string - must not persist to DB
protected $chooseRole;
// @var for roles fk_role_id - mapped
protected $userRole;
/* _construct() is an instant instance of the entity*/
public function __construct()
{
date_default_timezone_set('Asia/Manila');
$this->createdAt = new \DateTime("now");
//all users are inactive upon registration.
//isActive turns 'true' with the use of email confirmation
$this->isActive = false;
}
public function __toString()
{
return strval($this->userID);
}
/**
* Get userID
*
* @return integer
*/
public function getUserID()
{
return $this->userID;
}
/**
* Set userName
*
* @param string $userName
* @return User
*/
public function setUserName($userName)
{
$this->userName = $userName;
return $this;
}
/**
* Get userName
*
* @return string
*/
public function getUserName()
{
return $this->userName;
}
/**
* Set salt
*
* @param string $salt
* @return User
*/
public function setSalt($salt)
{
$this->salt = $salt;
return $this;
}
/**
* Get salt
*
* @return string
*/
public function getSalt()
{
return $this->salt;
}
public function setPlainPassword($plainPassword)
{
$this->plainPassword = $plainPassword;
return $this;
}
public function getPlainPassword()
{
return $this->plainPassword;
}
/**
* Set password
*
* @param string $password
* @return User
*/
public function setPassword($password)
{
$this->password = $password;
return $this;
}
/**
* Get password
*
* @return string
*/
public function getPassword()
{
return $this->password;
}
/**
* Set email
*
* @param string $email
* @return User
*/
public function setEmail($email)
{
$this->email = $email;
return $this;
}
/**
* Get email
*
* @return string
*/
public function getEmail()
{
return $this->email;
}
/**
* Set isActive
*
* @param boolean $isActive
* @return User
*/
public function setIsActive($isActive)
{
$this->isActive = $isActive;
return $this;
}
/**
* Get isActive
*
* @return boolean
*/
public function getIsActive()
{
return $this->isActive;
}
/**
* Set confirmationCode
*
* @param string $confirmationCode
* @return User
*/
public function setConfirmationCode($confirmationCode)
{
$this->confirmationCode = $confirmationCode;
return $this;
}
/**
* Get confirmationCode
*
* @return string
*/
public function getConfirmationCode()
{
return $this->confirmationCode;
}
/**
* Set createdAt
*
* @param \DateTime $createdAt
* @return User
*/
public function setCreatedAt($createdAt)
{
$this->createdAt = $createdAt;
return $this;
}
/**
* Get createdAt
*
* @return \DateTime
*/
public function getCreatedAt()
{
return $this->createdAt;
}
public function setChooseRole($chooseRole)
{
$this->chooseRole = $chooseRole;
return $this;
}
public function getChooseRole()
{
return $this->chooseRole;
}
/**
* Set userRole
*
* @param CloudPod\UserBundle\Entity\UserRoles $userRole
* @return User
*/
public function setUserRole(\CloudPod\UserBundle\Entity\UserRoles $userRole = null)
{
$this->userRole = $userRole;
return $this;
}
/**
* Get userRole
*
* @return CloudPod\UserBundle\Entity\UserRoles
*/
public function getUserRole()
{
return $this->userRole;
}
/**
* @see \Serializable::serialize()
*/
public function serialize()
{
return serialize(array(
$this->userID,
));
}
/**
* @see \Serializable::unserialize()
*/
public function unserialize($serialized)
{
list (
$this->userID,
) = unserialize($serialized);
}
public function isAccountNonExpired()
{ return true; }
public function isAccountNonLocked()
{ return true; }
public function isCredentialsNonExpired()
{ return true; }
public function isEnabled()
{ return $this->isActive; }
/**
* @inheritDoc
*/
public function eraseCredentials()
{
}
public function getRoles()
{
return array($this->userRole);
}
}
我认为问题出在我的控制器代码上。有人可以告诉我编码的正确方法吗?PS 我将用户与用户个人资料分开,因为我不想在表格中将用户帐户数据(如用户名、角色等)与其用户信息混为一谈。