php - 使用 symfony2 在 doctrine 中创建子类实体

标签 php symfony doctrine

<分区>

我正在尝试在我的 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

我还没有尝试自己实现这些方法、创建模式和持久化买家对象。我想如果该工具说有问题,那就是我做错了什么。

最佳答案

您在买家注释中忘记了“ORM”之前的“@”

关于php - 使用 symfony2 在 doctrine 中创建子类实体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14090069/

相关文章:

php - 以某种形式随机挑战?

php - Symfony2 中间件

logging - Symfony 2 : Log into a specific file

php - 使用 Symfony 和 Doctrine 按月和年检索项目

php - Laravel Doctrine 仅从 findAll() 查询返回单个属性

mysql - DQL 查询失败

php - 过滤器不会在我的自定义 Twig 扩展中退出

php - 使用 php 将图像插入 mysql 不起作用

php - 这个 SQL 查询有什么问题?

join - Doctrine dql 自定义加入 on