Doctrine Table继承 - 没有为实体

时间:2017-05-25 06:07:04

标签: postgresql symfony doctrine-orm

在Doctrine 2 / Symfony 3中,Postgres作为数据库我试图创建一个包含以下字段的表:

**actions-reviews**
id
action_id
action_task_id
document_id
review_to
review_date
review_description
review_by
is_effective

该表链接到doc_id,它是名为Document的实体的外键,或者是名为ActionTask的实体的action_task_id。为了实现这一点,我通过使用鉴别器来使用继承映射。在船长中,实体关系如下所示:Skipper Relationship

正如您所看到的,ActionTask和ActionReview都是Action实体的子集。

我的问题是在创建实体并运行php bin / console doctrine时:schema:update --force我收到以下错误:

[Doctrine\ORM\Mapping\MappingException]                                      
  No identifier/primary key specified for Entity "AppBundle\Entity\ActionTask  
  Review". Every Entity must have an identifier/primary key. 

阅读论坛我将@ ORM / Id添加到ActionTaskReview实体,然后更新数据库而没有任何错误。但是,当我查看postgres表时,document_id字段不存在action_task_id字段。 我做错了,因为当我排除@ ORM / Id时,document_id没有抛出相同的错误?

我的学说实体看起来像: 的 ActionReview.php     

/**
 * @ORM\Entity
 * @ORM\Table(name="actions_reviews")
 * @ORM\InheritanceType("SINGLE_TABLE")
 * @ORM\DiscriminatorColumn(name="owner", type="string")
 * @ORM\DiscriminatorMap({"document":"AppBundle\Entity\DocumentActionReview","action_task":"AppBundle\Entity\ActionTaskReview"})
 * @Discriminator(field = "owner", map = {"document": "AppBundle\Entity\DocumentActionReview", "action_task": "AppBundle\Entity\ActionTaskReview"})
 */
abstract class ActionReview
{
    /**
     * @ORM\Id
     * @ORM\Column(type="guid")
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

    /**
     * @ORM\Column(type="datetime", nullable=true)
     */
    private $review_date;

    /**
     * @ORM\Column(type="text", nullable=true)
     */
    private $review_description;

    /**
     * @ORM\Column(type="boolean", nullable=true)
     */
    private $is_effective;

    /**
     * @ORM\ManyToOne(targetEntity="AppBundle\Entity\Action", inversedBy="action_review")
     * @ORM\JoinColumn(name="action_id", referencedColumnName="id")
     */
    private $action;

    /**
     * @ORM\ManyToOne(targetEntity="AppBundle\Entity\Employee")
     * @ORM\JoinColumn(name="review_to", referencedColumnName="id")
     */
    private $review_to;

    /**
     * @ORM\ManyToOne(targetEntity="AppBundle\Entity\Employee")
     * @ORM\JoinColumn(name="review_by", referencedColumnName="id")
     */
    private $review_by;

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

    /**
     * Set reviewDate
     *
     * @param \DateTime $reviewDate
     *
     * @return ActionReview
     */
    public function setReviewDate($reviewDate)
    {
        $this->review_date = $reviewDate;

        return $this;
    }

    /**
     * Get reviewDate
     *
     * @return \DateTime
     */
    public function getReviewDate()
    {
        return $this->review_date;
    }

    /**
     * Set reviewDescription
     *
     * @param string $reviewDescription
     *
     * @return ActionReview
     */
    public function setReviewDescription($reviewDescription)
    {
        $this->review_description = $reviewDescription;

        return $this;
    }

    /**
     * Get reviewDescription
     *
     * @return string
     */
    public function getReviewDescription()
    {
        return $this->review_description;
    }

    /**
     * Set isEffective
     *
     * @param boolean $isEffective
     *
     * @return ActionReview
     */
    public function setIsEffective($isEffective)
    {
        $this->is_effective = $isEffective;

        return $this;
    }

    /**
     * Get isEffective
     *
     * @return boolean
     */
    public function getIsEffective()
    {
        return $this->is_effective;
    }

    /**
     * Set action
     *
     * @param \AppBundle\Entity\Action $action
     *
     * @return ActionReview
     */
    public function setAction(\AppBundle\Entity\Action $action = null)
    {
        $this->action = $action;

        return $this;
    }

    /**
     * Get action
     *
     * @return \AppBundle\Entity\Action
     */
    public function getAction()
    {
        return $this->action;
    }

    /**
     * Set reviewTo
     *
     * @param \AppBundle\Entity\Employee $reviewTo
     *
     * @return ActionReview
     */
    public function setReviewTo(\AppBundle\Entity\Employee $reviewTo = null)
    {
        $this->review_to = $reviewTo;

        return $this;
    }

    /**
     * Get reviewTo
     *
     * @return \AppBundle\Entity\Employee
     */
    public function getReviewTo()
    {
        return $this->review_to;
    }

    /**
     * Set reviewBy
     *
     * @param \AppBundle\Entity\Employee $reviewBy
     *
     * @return ActionReview
     */
    public function setReviewBy(\AppBundle\Entity\Employee $reviewBy = null)
    {
        $this->review_by = $reviewBy;

        return $this;
    }

    /**
     * Get reviewBy
     *
     * @return \AppBundle\Entity\Employee
     */
    public function getReviewBy()
    {
        return $this->review_by;
    }
}

ActionTaskReview.php     

namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;

use JMS\Serializer\Annotation\Exclude;

/**
 * ActionTaskReview
 * @ORM\Entity
 */
class ActionTaskReview
{
    /**      
     * @ORM\ManyToOne(targetEntity="AppBundle\Entity\ActionTask", inversedBy="action_review")
     * @ORM\JoinColumn(name="action_task_id", referencedColumnName="id") \AppBundle\Entity\ActionTask
     */
    private $action_task;


    /**
     * Set actionTask
     *
     * @param \AppBundle\Entity\ActionTask $actionTask
     *
     * @return ActionTaskReview
     */
    public function setActionTask(\AppBundle\Entity\ActionTask $actionTask = null)
    {
        $this->action_task = $actionTask;

        return $this;
    }

    /**
     * Get actionTask
     *
     * @return \AppBundle\Entity\ActionTask
     */
    public function getActionTask()
    {
        return $this->action_task;
    }
}

DocumentActionReview.php

<?php
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
 * @ORM\Entity
 * @ORM\Table(name="documents_actions_reviews")
 */
class DocumentActionReview extends \AppBundle\Entity\ActionReview
{
    /**
     * @ORM\ManyToOne(targetEntity="AppBundle\Entity\Document", inversedBy="action_review")
     * @ORM\JoinColumn(name="document_id", referencedColumnName="id")
     */
    private $document;
    /**
     * Set document
     *
     * @param \AppBundle\Entity\Document $document
     *
     * @return DocumentActionReview
     */
    public function setDocument(\AppBundle\Entity\Document $document = null)
    {
        $this->document = $document;
        return $this;
    }
    /**
     * Get document
     *
     * @return \AppBundle\Entity\Document
     */
    public function getDocument()
    {
        return $this->document;
    }
}

2 个答案:

答案 0 :(得分:1)

首先,您的ActionTaskReview应该扩展 ActionReview

namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;

use JMS\Serializer\Annotation\Exclude;

/**
 * ActionTaskReview
 * @ORM\Entity
 */
class ActionTaskReview extends ActionReview
{
...
}

我不确定您要实现的目标,因为您正在使用Single Table Inheritance,但正在 DocumentActionReview 类上设置@ORM\Table(name="documents_actions_reviews")注释。如果您需要为继承的实体使用自己的表或删除注释,请切换到Class Table Inheritance

答案 1 :(得分:-1)

每个实体类都必须具有标识符/主键。您可以使用@Id注释选择用作标识符的字段。

`/**
 * @ORM\Column(type="integer")
 * @ORM\Id
 * @GeneratedValue
 */
private $id;`