复合标识符,但使用除手动分配+ Symfony2之外的ID生成器

时间:2014-02-18 19:03:47

标签: php mysql sql symfony doctrine-orm

我有三张桌子。

带有字段的

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

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

我总是收到这个错误:

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

更新
删除我的类型变量并修复discriminatormap后,我收到此错误:

Property Beachteam\BeachteamBundle\Entity\Item::$type does not exist

这是我更新的物品实体:

<?php

namespace Beachteam\BeachteamBundle\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * Item
 *
 * @ORM\Table(name="item", indexes={@ORM\Index(name="fk_item_user1_idx", columns={"user_id"})})
 * @ORM\Entity
 * @ORM\InheritanceType("JOINED")
 * @ORM\DiscriminatorColumn(name="type", type="string")
 * @ORM\DiscriminatorMap({
 *     "ITEM"="Beachteam\BeachteamBundle\Entity\Item",
 *     "ARTICLE"="Beachteam\BeachteamBundle\Entity\Article",
 *     "IMAGE"="Beachteam\BeachteamBundle\Entity\Media"
 * })
 */
class Item
{
    /**
     * @var integer
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

    /**
     * @var string
     *
     * @ORM\Column(name="title", type="string", length=255, nullable=false)
     */
    private $title;

    /**
     * @var string
     *
     * @ORM\Column(name="description", type="text", nullable=true)
     */
    private $description;

    /**
     * @var \DateTime
     *
     * @ORM\Column(name="created", type="datetime", nullable=false)
     */
    private $created;

    /**
     * @var \DateTime
     *
     * @ORM\Column(name="deleted", type="datetime", nullable=true)
     */
    private $deleted;

    /**
     * @var \Beachteam\BeachteamBundle\Entity\User
     *
     * @ORM\ManyToOne(targetEntity="Beachteam\BeachteamBundle\Entity\User")
     * @ORM\JoinColumns({
     *   @ORM\JoinColumn(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 \Beachteam\BeachteamBundle\Entity\User $user
     * @return Item
     */
    public function setUser(User $user)
    {
        $this->user = $user;

        return $this;
    }

    /**
     * Get user
     *
     * @return \Beachteam\BeachteamBundle\Entity\User 
     */
    public function getUser()
    {
        return $this->user;
    }
}

这是我更新的文章实体:

<?php

namespace Beachteam\BeachteamBundle\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * Item
 *
 * @ORM\Table(name="article")
 * @ORM\Entity
 */
class Article extends Item
{
    /**
     * @var string
     *
     * @ORM\Column(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;
    }
}

任何人都可以解释我的错误(互联网对我没有多大帮助)..为什么我会收到此错误?

P.S。:我正在使用Symfony 2.4.1和PHP 5.4.20

2 个答案:

答案 0 :(得分:5)

我认为这一行(Item):

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

应该是:

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

我不太确定,但可能是鉴别器地图需要完全合格的类名:

/**
 * @ORM\DiscriminatorMap({
 *     "ITEM"="Beachteam\BeachteamBundle\Entity\Item",
 *     "ARTICLE"="Beachteam\BeachteamBundle\Entity\Article",
 *     "IMAGE"="Beachteam\BeachteamBundle\Entity\Media"
 * })
 */

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

您可能还想使用控制台验证映射:

app/console doctrine:schema:validate

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

<强>更新

  

属性Beachteam \ BeachteamBundle \ Entity \ Item :: $ type不存在

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

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

答案 1 :(得分:1)

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

@ORM\DiscriminatorColumn(name="type", type="string")

您拥有一个具有相同名称的表键和字段的一部分。尝试删除类属性(和getter,setter方法),然后更新模式

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

在此更改之后,您的课程就像我的盒子上的魅力一样

祝你好运!