Symfony实体关系无法正常工作

时间:2015-06-29 03:20:18

标签: php symfony many-to-one

我是Symfony的新手,我正在尝试在实体manyToOne(包含用户数据)和实体User(包含用户应用程序)之间设置Apply关系。用户可以按照他/她想要的次数申请。

在我的User实体中,我有id,我希望将其与user实体中的Apply相匹配

我使用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表中得到很好的存储,所以我猜测关系设置正确。

然而,当我在我的控制器中并尝试获取应用程序时。

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

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

enter image description here

不应该显示User Enity的数据吗?我为此疯狂而无法弄清楚我做错了什么。任何帮助将非常感激。

2 个答案:

答案 0 :(得分:0)

首先,你不需要

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;
}

答案 1 :(得分:0)

我最近也注意到Symfony2的这种行为。 我不得不通过运行其中一个getter方法强制对空对象进行初始化。虽然很奇怪。