Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/symfony/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php Symfony实体关系不工作_Php_Symfony_Many To One - Fatal编程技术网

Php Symfony实体关系不工作

Php Symfony实体关系不工作,php,symfony,many-to-one,Php,Symfony,Many To One,我是Symfony的新手,我正在尝试设置实体User(包含用户数据)和实体Apply(包含用户应用程序)之间的manytone关系。用户可以根据需要多次申请 在我的用户实体中,我有一个id,我想将其与应用实体中的用户匹配 我使用yml格式来实现这一点 这是我的用户实体 namespace Acme\UserBundle\Entity; use Doctrine\ORM\Mapping as ORM; use Acme\CoreBundle\Entity\Apply; use Symfony\C

我是Symfony的新手,我正在尝试设置实体
User
(包含用户数据)和实体
Apply
(包含用户应用程序)之间的
manytone
关系。用户可以根据需要多次申请

在我的
用户
实体中,我有一个
id
,我想将其与
应用
实体中的
用户
匹配

我使用yml格式来实现这一点

这是我的
用户
实体

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);
    }
}
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;
    }
}
这是我的
Apply
实体

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);
    }
}
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: {  }
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: {  }
这是我的
Applu.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: {  }
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表中,所以我猜关系设置正确

但是,当我在控制器中尝试获取应用程序时

$applications=$this->getDoctrine()->getRepository('CoreBundle:Apply')->findAll()

使用dump()我可以很好地看到应用程序数据,但是用户对象都显示为null


这不应该显示用户的数据吗?我为这件事发疯了,不知道我做错了什么。任何帮助都将不胜感激。

首先,你不需要帮助

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方法来强制初始化空对象。但是很奇怪。

manyToOne:userid:应该是:manyToOne:user:和$application\u data->setUser($user),其中$user是Acme\UserBundle\Entity\user对象。用户数据将延迟加载,除非您指定其他方式。也就是说,要获取用户数据,您必须在apply实体上调用apply->getUser()。我实际上需要连接两个表/实体并将它们传递给我的视图。
apply
实体已经有了
getUser()
我尝试过的
$applications=$this->getdoctor()->getRepository('CoreBundle:apply')->findAll()$user=$applications->getUser()->getUsername()
但这会导致一个错误
错误:在视图中调用数组上的成员函数getUser()
,您可以调用apply.user来获取用户对象,在用户对象上,您可以调用user.username、user.mobile等。工作得很好,我不知道我可以这样做来访问数据,因此,我假设在延迟加载时,用户对象在JOIN中显示为null是正常的。