Symfony2和Doctrine:名称为"blog.post"的表已经存在.

use*_*305 5 symfony doctrine-orm

我使用MySql使用Symfony2和Doctrine ORM.当我尝试使用时:

 php app/console doctrine:migration:diff       
Run Code Online (Sandbox Code Playgroud)

我有这个错误:

[Doctrine\DBAL\Schema\SchemaException]           
 The table with name 'blog.post' already exists.
Run Code Online (Sandbox Code Playgroud)

我在Post.php中的代码(我使用注释)是:

 namespace Blog\ModelBundle\Entity;

 use Doctrine\ORM\Mapping as ORM;
 use Symfony\Component\Validator\Constraints as Assert;

 /**
 * Post
 *
 * @ORM\Table()
 * @ORM\Entity
 */
 class Post extends Timestampable
 {
 /**
 * @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=150)
 * @Assert\NotBlank
 */
private $title;

/**
 * @var string
 *
 * @ORM\Column(name="body", type="text")
 * @Assert\NotBlank
 */
private $body;

/**
 * @var Author
 * @ORM\ManyToOne (targetEntity="Author", inversedBy="posts")
 * @ORM\JoinColumn (name="author_id", referencedColumnName="id", nullable=false)
 * @Assert\NotBlank
 */
private $author;


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

/**
 * Set title
 *
 * @param string $title
 * @return Post
 */
public function setTitle($title)
{
    $this->title = $title;

    return $this;
}

/**
 * Get title
 *
 * @return string 
 */
public function getTitle()
{
    return $this->title;
}

/**
 * Set body
 *
 * @param string $body
 * @return Post
 */
public function setBody($body)
{
    $this->body = $body;

    return $this;
}

/**
 * Get body
 *
 * @return string 
 */
public function getBody()
{
    return $this->body;
}


/**
 * Set author
 *
 * @param \Blog\ModelBundle\Entity\Author $author
 * @return Post
 */
public function setAuthor(Author $author)
{
    $this->author = $author;

    return $this;
}

/**
 * Get author
 *
 * @return \Blog\ModelBundle\Entity\Author 
 */
public function getAuthor()
{
    return $this->author;
}
 }
Run Code Online (Sandbox Code Playgroud)

我尝试定义*@ORM\Table(name ="Post").

你能帮我解决这类错误吗?对不起,我的英语不好.

Mic*_*son 1

您的映射看起来不正确。如果您不使用连接表,ManyToOne 声明不需要 inversedBy 属性。您也不需要指定 @var,因为它已经知道它正在使用该实体。尝试这个:

/**
 * @ORM\ManyToOne(targetEntity="Author")
 * @ORM\JoinColumn(name="author_id", referencedColumnName="id")
**/
private $author;
Run Code Online (Sandbox Code Playgroud)

要做的另一件事是检查您是否没有尝试在另一个包中声明相同的实体,这也会导致“表已存在”错误。

另外,为了避免在 getter 和 setter 中使用完整路径实体引用,只需将实体包含在类顶部的 use statemnets 中,然后只需要编写实体名称:

/**
 * set Author
 *
 * @param Author $author
 *
 * @return Post
/**
Run Code Online (Sandbox Code Playgroud)