Symfony 实体关系不起作用
Symfony Entity Relation not working
我是 Symfony 的新手,我正在尝试设置实体 User
(包含用户数据)和实体 Apply
(包含用户应用程序)之间的 manyToOne
关系。一个用户可以申请任意多次 he/she。
在我的 User
实体中,我有一个 id
,我想将其与 Apply
实体
中的 user
匹配
我正在使用 yml 格式来实现这个
这是我的User
实体
namespace Acme\UserBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Acme\CoreBundle\Entity\Apply;
use Symfony\Component\Security\Core\User\AdvancedUserInterface;
use Doctrine\Common\Collections\ArrayCollection;
/**
* User
*/
class User implements AdvancedUserInterface, \Serializable
{
/**
* @var integer
*/
private $id;
protected $apply;
/**
* @var string
*/
private $username;
/**
* @var string
*/
private $email;
/**
* @var string
*/
private $password;
/**
* @var string
*/
private $salt;
/**
* @var boolean
*/
private $isActive;
/**
* @var string
*/
private $role;
/**
* @var integer
*/
private $mobile;
/**
* @var string
*/
private $code;
/**
* @var string
*/
private $first_name;
/**
* @var string
*/
private $last_name;
public function __construct()
{
$this->isActive = true;
$this->apply = new ArrayCollection();
}
public function setApply(Apply $apply = null)
{
$this->apply = $apply;
return $this;
}
/**
* Get user
*
* @return \Acme\CoreBundle\Entity\Apply
*/
public function getApply()
{
return $this->apply;
}
public function isAccountNonExpired()
{
return true;
}
public function isAccountNonLocked()
{
return true;
}
public function isCredentialsNonExpired()
{
return true;
}
public function isEnabled()
{
return $this->isActive;
}
/**
* @inheritDoc
*/
public function getSalt()
{
return $this->salt;
}
public function setSalt($salt)
{
$this->salt = $salt;
return $this;
}
/**
* @inheritDoc
*/
public function getRoles()
{
return array($this->role);
}
/**
* @inheritDoc
*/
public function eraseCredentials()
{
}
/**
* @see \Serializable::serialize()
*/
public function serialize()
{
return serialize(array(
$this->id,
$this->username,
$this->email,
$this->password,
$this->mobile,
$this->code
));
}
/**
* @see \Serializable::unserialize()
*/
public function unserialize($serialized)
{
list (
$this->id,
$this->username,
$this->email,
$this->password,
$this->salt
) = unserialize($serialized);
}
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* 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 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 role
*
* @param string $role
* @return User
*/
public function setRole($role)
{
$this->role = $role;
return $this;
}
/**
* Get role
*
* @return string
*/
public function getRole()
{
return $this->role;
}
/**
* Set mobile
*
* @param integer $mobile
* @return User
*/
public function setMobile($mobile)
{
$this->mobile = $mobile;
return $this;
}
/**
* Get mobile
*
* @return integer
*/
public function getMobile()
{
return $this->mobile;
}
/**
* Set code
*
* @param string $code
* @return User
*/
public function setCode($code)
{
$this->code = $code;
return $this;
}
/**
* Get code
*
* @return string
*/
public function getCode()
{
return $this->code;
}
/**
* Set first_name
*
* @param string $firstName
* @return User
*/
public function setFirstName($firstName)
{
$this->first_name = $firstName;
return $this;
}
/**
* Get first_name
*
* @return string
*/
public function getFirstName()
{
return $this->first_name;
}
/**
* Set last_name
*
* @param string $lastName
* @return User
*/
public function setLastName($lastName)
{
$this->last_name = $lastName;
return $this;
}
/**
* Get last_name
*
* @return string
*/
public function getLastName()
{
return $this->last_name;
}
/**
* Add apply
*
* @param \Acme\CoreBundle\Entity\Apply $apply
* @return User
*/
public function addApply(\Acme\CoreBundle\Entity\Apply $apply)
{
$this->apply[] = $apply;
return $this;
}
/**
* Remove apply
*
* @param \Acme\CoreBundle\Entity\Apply $apply
*/
public function removeApply(\Acme\CoreBundle\Entity\Apply $apply)
{
$this->apply->removeElement($apply);
}
}
这是我的Apply
实体
namespace Acme\CoreBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\HttpFoundation\File\UploadedFile;
/**
* Apply
*/
class Apply
{
/**
* @var integer
*/
private $id;
/**
* @var string
*/
private $dob;
/**
* @var string
*/
private $gender;
/**
* @var string
*/
private $institute;
/**
* @var string
*/
private $degree;
/**
* @var string
*/
private $stuID;
/**
* @var string
*/
private $interestReason;
/**
* @var string
*/
private $leadershipSkills;
/**
* @var string
*/
private $teamwork;
/**
* @var string
*/
private $experience;
/**
* @var string
*/
private $levelOfExperience;
/**
* @var string
*/
private $firstLocPreference;
/**
* @var string
*/
private $secondLocPreference;
/**
* @var string
*/
private $cv;
/**
* @var string
*/
private $path;
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set dob
*
* @param string $dob
* @return Apply
*/
public function setDob($dob)
{
$this->dob = $dob;
return $this;
}
/**
* Get dob
*
* @return string
*/
public function getDob()
{
return $this->dob;
}
/**
* Set gender
*
* @param string $gender
* @return Apply
*/
public function setGender($gender)
{
$this->gender = $gender;
return $this;
}
/**
* Get gender
*
* @return string
*/
public function getGender()
{
return $this->gender;
}
/**
* Set institute
*
* @param string $institute
* @return Apply
*/
public function setInstitute($institute)
{
$this->institute = $institute;
return $this;
}
/**
* Get institute
*
* @return string
*/
public function getInstitute()
{
return $this->institute;
}
/**
* Set degree
*
* @param string $degree
* @return Apply
*/
public function setDegree($degree)
{
$this->degree = $degree;
return $this;
}
/**
* Get degree
*
* @return string
*/
public function getDegree()
{
return $this->degree;
}
/**
* Set stuID
*
* @param string $stuID
* @return Apply
*/
public function setStuID($stuID)
{
$this->stuID = $stuID;
return $this;
}
/**
* Get stuID
*
* @return string
*/
public function getStuID()
{
return $this->stuID;
}
/**
* Set interestReason
*
* @param string $interestReason
* @return Apply
*/
public function setInterestReason($interestReason)
{
$this->interestReason = $interestReason;
return $this;
}
/**
* Get interestReason
*
* @return string
*/
public function getInterestReason()
{
return $this->interestReason;
}
/**
* Set leadershipSkills
*
* @param string $leadershipSkills
* @return Apply
*/
public function setLeadershipSkills($leadershipSkills)
{
$this->leadershipSkills = $leadershipSkills;
return $this;
}
/**
* Get leadershipSkills
*
* @return string
*/
public function getLeadershipSkills()
{
return $this->leadershipSkills;
}
/**
* Set teamwork
*
* @param string $teamwork
* @return Apply
*/
public function setTeamwork($teamwork)
{
$this->teamwork = $teamwork;
return $this;
}
/**
* Get teamwork
*
* @return string
*/
public function getTeamwork()
{
return $this->teamwork;
}
/**
* Set experience
*
* @param string $experience
* @return Apply
*/
public function setExperience($experience)
{
$this->experience = $experience;
return $this;
}
/**
* Get experience
*
* @return string
*/
public function getExperience()
{
return $this->experience;
}
/**
* Set levelOfExperience
*
* @param string $levelOfExperience
* @return Apply
*/
public function setLevelOfExperience($levelOfExperience)
{
$this->levelOfExperience = $levelOfExperience;
return $this;
}
/**
* Get levelOfExperience
*
* @return string
*/
public function getLevelOfExperience()
{
return $this->levelOfExperience;
}
/**
* Set firstLocPreference
*
* @param string $firstLocPreference
* @return Apply
*/
public function setFirstLocPreference($firstLocPreference)
{
$this->firstLocPreference = $firstLocPreference;
return $this;
}
/**
* Get firstLocPreference
*
* @return string
*/
public function getFirstLocPreference()
{
return $this->firstLocPreference;
}
/**
* Set secondLocPreference
*
* @param string $secondLocPreference
* @return Apply
*/
public function setSecondLocPreference($secondLocPreference)
{
$this->secondLocPreference = $secondLocPreference;
return $this;
}
/**
* Get secondLocPreference
*
* @return string
*/
public function getSecondLocPreference()
{
return $this->secondLocPreference;
}
public function setCv($cv)
{
$this->cv = $cv;
return $this;
}
/**
* Get fileName
*
* @return string
*/
public function getCv()
{
return $this->cv;
}
/**
* @var integer
*/
private $user;
/**
* Set user
*
* @param \Acme\UserBundle\Entity\User $user
* @return Apply
*/
public function setUser($user)
{
$this->user = $user;
return $this;
}
/**
* Get user
*
* @return integer
*/
public function getUser()
{
return $this->user;
}
}
这是我的User.orm.yml
Acme\UserBundle\Entity\User:
type: entity
table: pe_users
id:
id:
type: integer
id: true
generator:
strategy: AUTO
fields:
username:
type: string
length: 255
password:
type: string
length: 255
salt:
type: string
length: 255
nullable: true
first_name:
type: string
length: 255
last_name:
type: string
length: 255
email:
type: string
length: 255
unique: true
isActive:
type: boolean
role:
type: string
length: 255
mobile:
type: integer
code:
type: string
length: 20
nullable: true
oneToMany:
apply:
targetEntity: Acme\CoreBundle\Entity\Apply
mappedBy: user
lifecycleCallbacks: { }
这是我的Applu.orm.yml
Acme\CoreBundle\Entity\Apply:
type: entity
table: pe_apply
id:
id:
type: integer
id: true
generator:
strategy: AUTO
fields:
dob:
type: string
length: 255
gender:
type: string
length: '10'
institute:
type: string
length: 255
degree:
type: string
length: 255
stuID:
type: string
length: 255
interestReason:
type: text
column: interest_reason
leadershipSkills:
type: text
column: leadership_skills
teamwork:
type: text
experience:
type: text
levelOfExperience:
type: string
length: '50'
column: level_of_experience
firstLocPreference:
type: string
length: '50'
column: first_loc_preference
secondLocPreference:
type: string
length: '50'
column: second_loc_preference
cv:
type: string
length: '50'
nullable: true
manyToOne:
user:
targetEntity: Acme\UserBundle\Entity\User
inversedBy: apply
joinColumn:
name: user
referencedColumnName: id
lifecycleCallbacks: { }
现在关系已经建立,我可以创建一个用户的应用程序,用户 ID 可以很好地存储在 Apply table 中,所以我猜关系是正确建立的。
然而,当我在我的控制器中并尝试获取应用程序时。
$applications = $this->getDoctrine()->getRepository('CoreBundle:Apply')->findAll();
使用 dump() 我可以很好地看到应用程序数据,但是用户对象都显示为空
那不应该显示用户实体的数据吗?我为此而疯狂,无法弄清楚我做错了什么。任何帮助将不胜感激。
首先,你不需要
protected $userid;
属性 和
public function setUserid(\Acme\CoreBundle\Entity\Apply userid = null)
{
$this->userid = $userid;
return $this;
}
public function getUserid()
{
return $this->userid;
}
您的 User.php 文件中的方法。 yml文件中的配置看起来没问题。 Apply 实体中 userId 字段的 getter 和 setter 方法应如下所示
/**
* Set user
*
* @param Your\NameSpace\User $user
*/
public function setUserid(Your\NameSpace\User $user = null)
{
$this->userid = $user;
}
/**
* Get user
*
* @return Your\NameSpace\User
*/
public function getUserid()
{
return $this->userid;
}
我最近在 Symfony2 中也注意到了这种行为。
我不得不通过 运行 它的 getter 方法之一强制对空对象进行初始化。不过很奇怪。
我是 Symfony 的新手,我正在尝试设置实体 User
(包含用户数据)和实体 Apply
(包含用户应用程序)之间的 manyToOne
关系。一个用户可以申请任意多次 he/she。
在我的 User
实体中,我有一个 id
,我想将其与 Apply
实体
user
匹配
我正在使用 yml 格式来实现这个
这是我的User
实体
namespace Acme\UserBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Acme\CoreBundle\Entity\Apply;
use Symfony\Component\Security\Core\User\AdvancedUserInterface;
use Doctrine\Common\Collections\ArrayCollection;
/**
* User
*/
class User implements AdvancedUserInterface, \Serializable
{
/**
* @var integer
*/
private $id;
protected $apply;
/**
* @var string
*/
private $username;
/**
* @var string
*/
private $email;
/**
* @var string
*/
private $password;
/**
* @var string
*/
private $salt;
/**
* @var boolean
*/
private $isActive;
/**
* @var string
*/
private $role;
/**
* @var integer
*/
private $mobile;
/**
* @var string
*/
private $code;
/**
* @var string
*/
private $first_name;
/**
* @var string
*/
private $last_name;
public function __construct()
{
$this->isActive = true;
$this->apply = new ArrayCollection();
}
public function setApply(Apply $apply = null)
{
$this->apply = $apply;
return $this;
}
/**
* Get user
*
* @return \Acme\CoreBundle\Entity\Apply
*/
public function getApply()
{
return $this->apply;
}
public function isAccountNonExpired()
{
return true;
}
public function isAccountNonLocked()
{
return true;
}
public function isCredentialsNonExpired()
{
return true;
}
public function isEnabled()
{
return $this->isActive;
}
/**
* @inheritDoc
*/
public function getSalt()
{
return $this->salt;
}
public function setSalt($salt)
{
$this->salt = $salt;
return $this;
}
/**
* @inheritDoc
*/
public function getRoles()
{
return array($this->role);
}
/**
* @inheritDoc
*/
public function eraseCredentials()
{
}
/**
* @see \Serializable::serialize()
*/
public function serialize()
{
return serialize(array(
$this->id,
$this->username,
$this->email,
$this->password,
$this->mobile,
$this->code
));
}
/**
* @see \Serializable::unserialize()
*/
public function unserialize($serialized)
{
list (
$this->id,
$this->username,
$this->email,
$this->password,
$this->salt
) = unserialize($serialized);
}
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* 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 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 role
*
* @param string $role
* @return User
*/
public function setRole($role)
{
$this->role = $role;
return $this;
}
/**
* Get role
*
* @return string
*/
public function getRole()
{
return $this->role;
}
/**
* Set mobile
*
* @param integer $mobile
* @return User
*/
public function setMobile($mobile)
{
$this->mobile = $mobile;
return $this;
}
/**
* Get mobile
*
* @return integer
*/
public function getMobile()
{
return $this->mobile;
}
/**
* Set code
*
* @param string $code
* @return User
*/
public function setCode($code)
{
$this->code = $code;
return $this;
}
/**
* Get code
*
* @return string
*/
public function getCode()
{
return $this->code;
}
/**
* Set first_name
*
* @param string $firstName
* @return User
*/
public function setFirstName($firstName)
{
$this->first_name = $firstName;
return $this;
}
/**
* Get first_name
*
* @return string
*/
public function getFirstName()
{
return $this->first_name;
}
/**
* Set last_name
*
* @param string $lastName
* @return User
*/
public function setLastName($lastName)
{
$this->last_name = $lastName;
return $this;
}
/**
* Get last_name
*
* @return string
*/
public function getLastName()
{
return $this->last_name;
}
/**
* Add apply
*
* @param \Acme\CoreBundle\Entity\Apply $apply
* @return User
*/
public function addApply(\Acme\CoreBundle\Entity\Apply $apply)
{
$this->apply[] = $apply;
return $this;
}
/**
* Remove apply
*
* @param \Acme\CoreBundle\Entity\Apply $apply
*/
public function removeApply(\Acme\CoreBundle\Entity\Apply $apply)
{
$this->apply->removeElement($apply);
}
}
这是我的Apply
实体
namespace Acme\CoreBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\HttpFoundation\File\UploadedFile;
/**
* Apply
*/
class Apply
{
/**
* @var integer
*/
private $id;
/**
* @var string
*/
private $dob;
/**
* @var string
*/
private $gender;
/**
* @var string
*/
private $institute;
/**
* @var string
*/
private $degree;
/**
* @var string
*/
private $stuID;
/**
* @var string
*/
private $interestReason;
/**
* @var string
*/
private $leadershipSkills;
/**
* @var string
*/
private $teamwork;
/**
* @var string
*/
private $experience;
/**
* @var string
*/
private $levelOfExperience;
/**
* @var string
*/
private $firstLocPreference;
/**
* @var string
*/
private $secondLocPreference;
/**
* @var string
*/
private $cv;
/**
* @var string
*/
private $path;
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set dob
*
* @param string $dob
* @return Apply
*/
public function setDob($dob)
{
$this->dob = $dob;
return $this;
}
/**
* Get dob
*
* @return string
*/
public function getDob()
{
return $this->dob;
}
/**
* Set gender
*
* @param string $gender
* @return Apply
*/
public function setGender($gender)
{
$this->gender = $gender;
return $this;
}
/**
* Get gender
*
* @return string
*/
public function getGender()
{
return $this->gender;
}
/**
* Set institute
*
* @param string $institute
* @return Apply
*/
public function setInstitute($institute)
{
$this->institute = $institute;
return $this;
}
/**
* Get institute
*
* @return string
*/
public function getInstitute()
{
return $this->institute;
}
/**
* Set degree
*
* @param string $degree
* @return Apply
*/
public function setDegree($degree)
{
$this->degree = $degree;
return $this;
}
/**
* Get degree
*
* @return string
*/
public function getDegree()
{
return $this->degree;
}
/**
* Set stuID
*
* @param string $stuID
* @return Apply
*/
public function setStuID($stuID)
{
$this->stuID = $stuID;
return $this;
}
/**
* Get stuID
*
* @return string
*/
public function getStuID()
{
return $this->stuID;
}
/**
* Set interestReason
*
* @param string $interestReason
* @return Apply
*/
public function setInterestReason($interestReason)
{
$this->interestReason = $interestReason;
return $this;
}
/**
* Get interestReason
*
* @return string
*/
public function getInterestReason()
{
return $this->interestReason;
}
/**
* Set leadershipSkills
*
* @param string $leadershipSkills
* @return Apply
*/
public function setLeadershipSkills($leadershipSkills)
{
$this->leadershipSkills = $leadershipSkills;
return $this;
}
/**
* Get leadershipSkills
*
* @return string
*/
public function getLeadershipSkills()
{
return $this->leadershipSkills;
}
/**
* Set teamwork
*
* @param string $teamwork
* @return Apply
*/
public function setTeamwork($teamwork)
{
$this->teamwork = $teamwork;
return $this;
}
/**
* Get teamwork
*
* @return string
*/
public function getTeamwork()
{
return $this->teamwork;
}
/**
* Set experience
*
* @param string $experience
* @return Apply
*/
public function setExperience($experience)
{
$this->experience = $experience;
return $this;
}
/**
* Get experience
*
* @return string
*/
public function getExperience()
{
return $this->experience;
}
/**
* Set levelOfExperience
*
* @param string $levelOfExperience
* @return Apply
*/
public function setLevelOfExperience($levelOfExperience)
{
$this->levelOfExperience = $levelOfExperience;
return $this;
}
/**
* Get levelOfExperience
*
* @return string
*/
public function getLevelOfExperience()
{
return $this->levelOfExperience;
}
/**
* Set firstLocPreference
*
* @param string $firstLocPreference
* @return Apply
*/
public function setFirstLocPreference($firstLocPreference)
{
$this->firstLocPreference = $firstLocPreference;
return $this;
}
/**
* Get firstLocPreference
*
* @return string
*/
public function getFirstLocPreference()
{
return $this->firstLocPreference;
}
/**
* Set secondLocPreference
*
* @param string $secondLocPreference
* @return Apply
*/
public function setSecondLocPreference($secondLocPreference)
{
$this->secondLocPreference = $secondLocPreference;
return $this;
}
/**
* Get secondLocPreference
*
* @return string
*/
public function getSecondLocPreference()
{
return $this->secondLocPreference;
}
public function setCv($cv)
{
$this->cv = $cv;
return $this;
}
/**
* Get fileName
*
* @return string
*/
public function getCv()
{
return $this->cv;
}
/**
* @var integer
*/
private $user;
/**
* Set user
*
* @param \Acme\UserBundle\Entity\User $user
* @return Apply
*/
public function setUser($user)
{
$this->user = $user;
return $this;
}
/**
* Get user
*
* @return integer
*/
public function getUser()
{
return $this->user;
}
}
这是我的User.orm.yml
Acme\UserBundle\Entity\User:
type: entity
table: pe_users
id:
id:
type: integer
id: true
generator:
strategy: AUTO
fields:
username:
type: string
length: 255
password:
type: string
length: 255
salt:
type: string
length: 255
nullable: true
first_name:
type: string
length: 255
last_name:
type: string
length: 255
email:
type: string
length: 255
unique: true
isActive:
type: boolean
role:
type: string
length: 255
mobile:
type: integer
code:
type: string
length: 20
nullable: true
oneToMany:
apply:
targetEntity: Acme\CoreBundle\Entity\Apply
mappedBy: user
lifecycleCallbacks: { }
这是我的Applu.orm.yml
Acme\CoreBundle\Entity\Apply:
type: entity
table: pe_apply
id:
id:
type: integer
id: true
generator:
strategy: AUTO
fields:
dob:
type: string
length: 255
gender:
type: string
length: '10'
institute:
type: string
length: 255
degree:
type: string
length: 255
stuID:
type: string
length: 255
interestReason:
type: text
column: interest_reason
leadershipSkills:
type: text
column: leadership_skills
teamwork:
type: text
experience:
type: text
levelOfExperience:
type: string
length: '50'
column: level_of_experience
firstLocPreference:
type: string
length: '50'
column: first_loc_preference
secondLocPreference:
type: string
length: '50'
column: second_loc_preference
cv:
type: string
length: '50'
nullable: true
manyToOne:
user:
targetEntity: Acme\UserBundle\Entity\User
inversedBy: apply
joinColumn:
name: user
referencedColumnName: id
lifecycleCallbacks: { }
现在关系已经建立,我可以创建一个用户的应用程序,用户 ID 可以很好地存储在 Apply table 中,所以我猜关系是正确建立的。
然而,当我在我的控制器中并尝试获取应用程序时。
$applications = $this->getDoctrine()->getRepository('CoreBundle:Apply')->findAll();
使用 dump() 我可以很好地看到应用程序数据,但是用户对象都显示为空
那不应该显示用户实体的数据吗?我为此而疯狂,无法弄清楚我做错了什么。任何帮助将不胜感激。
首先,你不需要
protected $userid;
属性 和
public function setUserid(\Acme\CoreBundle\Entity\Apply userid = null)
{
$this->userid = $userid;
return $this;
}
public function getUserid()
{
return $this->userid;
}
您的 User.php 文件中的方法。 yml文件中的配置看起来没问题。 Apply 实体中 userId 字段的 getter 和 setter 方法应如下所示
/**
* Set user
*
* @param Your\NameSpace\User $user
*/
public function setUserid(Your\NameSpace\User $user = null)
{
$this->userid = $user;
}
/**
* Get user
*
* @return Your\NameSpace\User
*/
public function getUserid()
{
return $this->userid;
}
我最近在 Symfony2 中也注意到了这种行为。 我不得不通过 运行 它的 getter 方法之一强制对空对象进行初始化。不过很奇怪。