symfony2,doctrine2,类继承

时间:2015-09-24 07:18:19

标签: symfony doctrine-orm

我在学说和symfony上玩了一点,我遇到了这个模型的问题。 我设置了一个基类(实体),然后是一个子类(实体),到目前为止,一切顺利,之后我想在我的表中添加更多字段,我使用了doctrine:generate:entities BundleName。这就是问题所在。如果我没有将变量从基类设置为受保护的symfony / doctrine说它无法访问该变量,但如果我将其设置为protected并再次生成实体,则会在我的子类中为基类创建受保护变量的getter和setter。我发现它很奇怪,所以我用谷歌搜索了一点,但我只发现了一个关于这个问题的话题并没有得到答案。 Base Class设置了注释MappedSuperclass但是..没有对它产生任何影响。

实现AdvancedUserInterface的基类

<?php
namespace AppBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Security\Core\User\AdvancedUserInterface;

/**
 * @ORM\MappedSuperclass
 */
class BaseUser implements AdvancedUserInterface
{
    /**
     * @var string
     * @ORM\Column(name="username", type="string", length=255, nullable=true)
     */
    protected $username;

    /**
     * @var string
     * @ORM\Column(name="password", type="string", length=255)
     */
    protected $password;

    /**
     * @var bool
     * @ORM\Column(name="isActive", type="boolean")
     */
    protected $isActive = '0';

    /**
     * [$isAccountNonExpired description].
     *
     * @var bool
     * @ORM\Column(name="isAccountNonExpired", type="boolean")
     */
    protected $isAccountNonExpired = '1';

    /**
     * [$isAccountNonLocked description].
     *
     * @var bool
     * @ORM\Column(name="isAccountNonLocked", type="boolean")
     */
    protected $isAccountNonLocked = '0';

    /**
     * [$isCredentialsNonExpired description].
     *
     * @var bool
     * @ORM\Column(name="isCredentialsNonExpired", type="boolean")
     */
    protected $isCredentialsNonExpired = '0';
    /**
     * 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 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;
    }

    /**
     * USER INTERFACE IMPLEMENTATION.
     */
    public function getSalt()
    {
        // you *may* need a real salt depending on your encoder
        // see section on salt below
    }

    public function getRoles()
    {
        return array('ROLE_ADMIN');
    }

    public function eraseCredentials()
    {
        return $this->plainPassword = null;
    }

    /**
     * ADVANCED USER INTERFACE IMPLEMENTATION.
     */

    /**
     * Checks whether the user's account has expired.
     *
     * Internally, if this method returns false, the authentication system
     * will throw an AccountExpiredException and prevent login.
     *
     * @return bool true if the user's account is non expired, false otherwise
     *
     * @see AccountExpiredException
     */
    public function isAccountNonExpired()
    {
        return $this->isAccountNonExpired;
    }

    /**
     * Checks whether the user is locked.
     *
     * Internally, if this method returns false, the authentication system
     * will throw a LockedException and prevent login.
     *
     * @return bool true if the user is not locked, false otherwise
     *
     * @see LockedException
     */
    public function isAccountNonLocked()
    {
        return $this->isAccountNonLocked;
    }

    /**
     * Checks whether the user's credentials (password) has expired.
     *
     * Internally, if this method returns false, the authentication system
     * will throw a CredentialsExpiredException and prevent login.
     *
     * @return bool true if the user's credentials are non expired, false otherwise
     *
     * @see CredentialsExpiredException
     */
    public function isCredentialsNonExpired()
    {
        return $this->isCredentialsNonExpired;
    }

    /**
     * Checks whether the user is enabled.
     *
     * Internally, if this method returns false, the authentication system
     * will throw a DisabledException and prevent login.
     *
     * @return bool true if the user is enabled, false otherwise
     *
     * @see DisabledException
     */
    public function isEnabled()
    {
        return $this->isActive;
    }

    /**
     * Set isActive
     *
     * @param boolean $isActive
     * @return BaseUser
     */
    public function setIsActive($isActive)
    {
        $this->isActive = $isActive;

        return $this;
    }

    /**
     * Get isActive
     *
     * @return boolean
     */
    public function getIsActive()
    {
        return $this->isActive;
    }

    /**
     * Set isAccountNonExpired
     *
     * @param boolean $isAccountNonExpired
     * @return BaseUser
     */
    public function setIsAccountNonExpired($isAccountNonExpired)
    {
        $this->isAccountNonExpired = $isAccountNonExpired;

        return $this;
    }

    /**
     * Get isAccountNonExpired
     *
     * @return boolean
     */
    public function getIsAccountNonExpired()
    {
        return $this->isAccountNonExpired;
    }

    /**
     * Set isAccountNonLocked
     *
     * @param boolean $isAccountNonLocked
     * @return BaseUser
     */
    public function setIsAccountNonLocked($isAccountNonLocked)
    {
        $this->isAccountNonLocked = $isAccountNonLocked;

        return $this;
    }

    /**
     * Get isAccountNonLocked
     *
     * @return boolean
     */
    public function getIsAccountNonLocked()
    {
        return $this->isAccountNonLocked;
    }

    /**
     * Set isCredentialsNonExpired
     *
     * @param boolean $isCredentialsNonExpired
     * @return BaseUser
     */
    public function setIsCredentialsNonExpired($isCredentialsNonExpired)
    {
        $this->isCredentialsNonExpired = $isCredentialsNonExpired;

        return $this;
    }

    /**
     * Get isCredentialsNonExpired
     *
     * @return boolean
     */
    public function getIsCredentialsNonExpired()
    {
        return $this->isCredentialsNonExpired;
    }
}

和儿童班

<?php

namespace AppBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
use Symfony\Component\Security\Core\User\AdvancedUserInterface;
use Gedmo\Mapping\Annotation as Gedmo;
use AppBundle\Entity\BaseUser;

/**
 * User
 *
 * @ORM\Table()
 * @ORM\Entity(repositoryClass="Zenith\UserBundle\Entity\UserRepository")
 * @UniqueEntity(fields="email", message="Email already taken")
 * @UniqueEntity(fields="username", message="Username already taken")
 */
class User extends BaseUser
{
    /**
     * @var int
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

    /**
     * @var string
     *
     * @ORM\Column(name="email", type="string", length=255)
     * @Assert\Email()
     * @Assert\NotBlank()
     */
    private $email;

    /**
     * @var  string
     * @Assert\NotBlank()
     * @Assert\Length(max = 4096)
     */
    protected $plainPassword;

    /**
     * Date when the user needs to change his password
     * for security reasons.
     *
     * @var datetime
     *
     * @ORM\Column(name="credentialsExpireAt", type="datetime", nullable=true)
     */
    private $credentialsExpireAt;

    /**
     * Date when the account expires
     * This can be used for temporary accounts.
     *
     * @var datetime
     *
     * @ORM\Column(name="accountExpireAt", type="datetime", nullable=true)
     */
    private $accountExpireAt;

    /**
     * Saves last login date of the user.
     *
     * @var datetime
     * @ORM\Column(name="lastLogin", type="datetime", nullable=true)
     */
    private $lastLogin;

    /**
     *
     *
     * @ORM\Column(name="createdAt", type="datetime")
     * @Gedmo\Timestampable(on="create")
     *
     * @var datetime
     */
    private $createdAt;

    /**
     * @ORM\Column(name="editedAt", type="datetime")
     * @Gedmo\Timestampable(on="update")
     *
     * @var datetime
     */
    private $editedAt;

    /**
     * Get id
     *
     * @return integer
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * 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 credentialsExpireAt
     *
     * @param \DateTime $credentialsExpireAt
     * @return User
     */
    public function setCredentialsExpireAt($credentialsExpireAt)
    {
        $this->credentialsExpireAt = $credentialsExpireAt;

        return $this;
    }

    /**
     * Get credentialsExpireAt
     *
     * @return \DateTime
     */
    public function getCredentialsExpireAt()
    {
        return $this->credentialsExpireAt;
    }

    /**
     * Set accountExpireAt
     *
     * @param \DateTime $accountExpireAt
     * @return User
     */
    public function setAccountExpireAt($accountExpireAt)
    {
        $this->accountExpireAt = $accountExpireAt;

        return $this;
    }

    /**
     * Get accountExpireAt
     *
     * @return \DateTime
     */
    public function getAccountExpireAt()
    {
        return $this->accountExpireAt;
    }

    /**
     * Set lastLogin
     *
     * @param \DateTime $lastLogin
     * @return User
     */
    public function setLastLogin($lastLogin)
    {
        $this->lastLogin = $lastLogin;

        return $this;
    }

    /**
     * Get lastLogin
     *
     * @return \DateTime
     */
    public function getLastLogin()
    {
        return $this->lastLogin;
    }

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

    /**
     * Set editedAt
     *
     * @param \DateTime $editedAt
     * @return User
     */
    public function setEditedAt($editedAt)
    {
        $this->editedAt = $editedAt;

        return $this;
    }

    /**
     * Get editedAt
     *
     * @return \DateTime
     */
    public function getEditedAt()
    {
        return $this->editedAt;
    }

}

1 个答案:

答案 0 :(得分:0)

那么问题是什么?命令为什么这样做?或者在这种情况下如何使用它?

doctrine:generate:实体从它的映射生成实体类。映射可以用不同的格式描述,PHP注释只是其中之一(与xml和yml一起)。我认为逻辑以这种方式工作 - 首先从注释映射创建,然后从映射(无论如何描述此映射,如果属性是否继承),生成一个类。

所以不要担心,删除不必要的代码并继续前进。这只是节省一些时间的工具。