使用Symfony2和Doctrine创建子类实体

3

我正在尝试在我的Symfony2项目中创建一个用户系统,其中我有两种类型的用户; 买家和卖家。 他们都是用户,因此让他们扩展基本用户类是有意义的。

// Acme/UserBundle/Entity/BaseUser
/**
 * @ORM\MappedSuperclass
 */
abstract class BaseUser implements UserInterface, \Serializable {
    /**
     * @ORM\Column(type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $id;

    /**
     * @ORM\Column(type="string", length=255, unique=true)
     */
    protected $username;

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

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

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

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

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

    /**
     * @ORM\Column(name="is_validated", type="boolean")
     */
    protected $isValidated;

    public function __construct() {
        $this->isValidated = false;
        $this->salt = md5(uniqid(null, true));
    }

    /**
     * @inheritDoc
     * All users must return the role: ROLE_USER
     */
    public function getRoles() {
        return array('ROLE_USER');
    }

    /**
     * @inheritDoc
     */
    public function eraseCredentials() {

    }

    /**
     * @see \Serializable::serialize()
     * We only need to return the ID because you can use the ID to load the rest from the database.
     */
    public function serialize() {
        return serialize(array(
            $this->id,
        ));
    }

    public function unserialize($serialized) {
        list (
            $this->id,
        ) = unserialize($serialized);
    }

    // Getters and Setters removed
}

接下来是一个买家扩展该类:

// Acme/UserBundle/Entity/Buyer
/**
 * ORM\Entity
 * ORM\Table(name="buyer")
 */
class Buyer extends BaseUser {
    /**
     * @ORM\Column(name="first_name", type="string", length=255)
     */
    protected $firstName;

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

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

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

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

    /**
     * @ORM\Column(name="receive_newsletter", type="boolean", length=255)
     */
    protected $receiveNewsletter;

    /**
     * @inheritDoc
     * Buyers have their own buyer role
     */
    public function getRoles() {
        return array_push(parent::getRoles(), 'ROLE_BUYER');
    }
}

我正在尝试使用Doctrine工具在Buyer.php中自动生成getter/setter方法,但是出现了以下错误信息:

$ php app/console doctrine:generate:entities Acme/UserBundle/Entity/Buyer
Generating entity "Acme\UserBundle\Entity\Buyer"



  [Doctrine\ORM\Mapping\MappingException]
  Class "Acme\UserBundle\Entity\Buyer" sub class of "Acme\UserBundle\En
  tity\BaseUser" is not a valid entity or mapped super class.



doctrine:generate:entities [--path="..."] [--no-backup] name

我还没有亲自尝试实现这些方法,创建模式并持久化买家对象。如果工具显示出问题,那么我认为我做错了什么。


2
您在buyer注释中忘记了在ORM之前加上“@”符号。 - Cyprian
1
我简直不敢相信它是那么简单... 我确信实际上存在问题! - dantheman
1
Cyprain,请回答下面的问题以获得积分。:) - FabianCook
它会发生在我们任何人身上!注意保重! :) - Cyprian
1个回答

2

您在买家注释中忘记了在"ORM"之前加上"@"符号


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接