复合标识符,但使用ID生成器,而不是手动分配+ Symfony2



我有三个表。

item字段

    PK id
    title
    description
    type
    created
    delete
    fk user_id

article字段

    PK item_id (one-to-one with item table), 
    body

带字段media

    PK id
    FK item_id (many-to-one with item table)
    url
    type
    mimetype
    isexternal

表中article type字段是值为 ITEM, ARTICLE and IMAGE 的 ENUM 。
实体是自动生成的。因此,文章实体最初不会扩展项目实体。我必须改变这一点。

我总是收到此错误:

Entity 'BeachteamBeachteamBundleEntityArticle' has a composite identifier 
but uses an ID generator other than manually assigning (Identity, Sequence). 
This is not supported.

更新:
删除我的类型变量并修复鉴别器映射后,出现此错误:

Property BeachteamBeachteamBundleEntityItem::$type does not exist

这是我更新的项目实体:

<?php
namespace BeachteamBeachteamBundleEntity;
use DoctrineORMMapping as ORM;
/**
 * Item
 *
 * @ORMTable(name="item", indexes={@ORMIndex(name="fk_item_user1_idx", columns={"user_id"})})
 * @ORMEntity
 * @ORMInheritanceType("JOINED")
 * @ORMDiscriminatorColumn(name="type", type="string")
 * @ORMDiscriminatorMap({
 *     "ITEM"="BeachteamBeachteamBundleEntityItem",
 *     "ARTICLE"="BeachteamBeachteamBundleEntityArticle",
 *     "IMAGE"="BeachteamBeachteamBundleEntityMedia"
 * })
 */
class Item
{
    /**
     * @var integer
     *
     * @ORMColumn(name="id", type="integer")
     * @ORMId
     * @ORMGeneratedValue(strategy="AUTO")
     */
    private $id;
    /**
     * @var string
     *
     * @ORMColumn(name="title", type="string", length=255, nullable=false)
     */
    private $title;
    /**
     * @var string
     *
     * @ORMColumn(name="description", type="text", nullable=true)
     */
    private $description;
    /**
     * @var DateTime
     *
     * @ORMColumn(name="created", type="datetime", nullable=false)
     */
    private $created;
    /**
     * @var DateTime
     *
     * @ORMColumn(name="deleted", type="datetime", nullable=true)
     */
    private $deleted;
    /**
     * @var BeachteamBeachteamBundleEntityUser
     *
     * @ORMManyToOne(targetEntity="BeachteamBeachteamBundleEntityUser")
     * @ORMJoinColumns({
     *   @ORMJoinColumn(name="user_id", referencedColumnName="id")
     * })
     */
    private $user;
    /**
     * Get id
     *
     * @return integer
     */
    public function getId()
    {
        return $this->id;
    }
    /**
     * Set title
     *
     * @param string $title
     * @return Item
     */
    public function setTitle($title)
    {
        $this->title = $title;
        return $this;
    }
    /**
     * Get title
     *
     * @return string 
     */
    public function getTitle()
    {
        return $this->title;
    }
    /**
     * Set description
     *
     * @param string $description
     * @return Item
     */
    public function setDescription($description)
    {
        $this->description = $description;
        return $this;
    }
    /**
     * Get description
     *
     * @return string 
     */
    public function getDescription()
    {
        return $this->description;
    }
    /**
     * Set created
     *
     * @param DateTime $created
     * @return Item
     */
    public function setCreated($created)
    {
        $this->created = $created;
        return $this;
    }
    /**
     * Get created
     *
     * @return DateTime 
     */
    public function getCreated()
    {
        return $this->created;
    }
    /**
     * Set deleted
     *
     * @param DateTime $deleted
     * @return Item
     */
    public function setDeleted($deleted)
    {
        $this->deleted = $deleted;
        return $this;
    }
    /**
     * Get deleted
     *
     * @return DateTime 
     */
    public function getDeleted()
    {
        return $this->deleted;
    }
    /**
     * Set user
     *
     * @param BeachteamBeachteamBundleEntityUser $user
     * @return Item
     */
    public function setUser(User $user)
    {
        $this->user = $user;
        return $this;
    }
    /**
     * Get user
     *
     * @return BeachteamBeachteamBundleEntityUser 
     */
    public function getUser()
    {
        return $this->user;
    }
}

这是我更新的文章实体:

<?php
namespace BeachteamBeachteamBundleEntity;
use DoctrineORMMapping as ORM;
/**
 * Item
 *
 * @ORMTable(name="article")
 * @ORMEntity
 */
class Article extends Item
{
    /**
     * @var string
     *
     * @ORMColumn(name="body", type="text")
     */
    protected $body;
    /**
     * Set body
     *
     * @param string $body
     * @return Article
     */
    public function setBody($body)
    {
        $this->body = $body;
        return $this;
    }
    /**
     * Get body
     *
     * @return string 
     */
    public function getBody()
    {
        return $this->body;
    }
}

谁能解释我的错误(互联网对我没有多大帮助)。为什么我会收到此错误?

PS:我正在使用Symfony 2.4.1和PHP 5.4.20

我认为这一行(Item):

/**
 * @ORMDiscriminatorMap({"ITEM"="Item", "ARTICLE"="Article" "IMAGE"="Media"})
 */

应该是:

/**
 * @ORMDiscriminatorMap({"ITEM"="Item", "ARTICLE"="Article", "IMAGE"="Media"})
 *                                                          ^
 */

我不太确定,但可能是鉴别器映射需要完全限定的类名:

/**
 * @ORMDiscriminatorMap({
 *     "ITEM"="BeachteamBeachteamBundleEntityItem",
 *     "ARTICLE"="BeachteamBeachteamBundleEntityArticle",
 *     "IMAGE"="BeachteamBeachteamBundleEntityMedia"
 * })
 */

接下来,您需要从Item中删除属性$type,因为type已经用作鉴别器列。

您可能还希望使用控制台来验证映射:

app/console doctrine:schema:validate

修复可能报告的所有错误。

更新

属性 海滩团队\海滩团队捆绑\实体\项::$type 不存在

这意味着代码中的某个地方仍在使用类 Item 的属性$type(现在不存在)。你必须追踪到哪里。

  • 在代码中搜索->type
  • 清除缓存(删除app/cache文件夹的内容并运行app/console cache:clear

我认为您在生成学说作为继承属性时计划了 Item::$type 属性。但这也是通过注释创建的:

@ORMDiscriminatorColumn(name="type", type="string")

您有同名的表键和字段的一部分。尝试删除类属性(以及getter,setter方法),然后更新架构

php app/console doctrine:schema:update --force

在此更改之后,您的类就像我盒子上的魅力一样工作

祝你好运!

相关内容

  • 没有找到相关文章

最新更新