无法使用 Doctrine 和 Symfony2 在 MySQL 中创建表

发布于 2024-12-06 10:30:24 字数 862 浏览 0 评论 0原文

我正在使用 MySql 来使用 Symfony2 和 Doctrine ORM。

创建实体后,我无法创建表。它抛出异常。

anu@anu-bridge:/var/www/Symfony$ php app/console doctrine:schema:update --force --dump-sql

[Doctrine\DBAL\Schema\SchemaException]
The table with name 'product' already exists.  

doctrine:schema:update [--complete] [--dump-sql] [--force] [--em[="..."]]

我试图删除它,但它仍然抛出错误。

anu@anu-bridge:/var/www/Symfony$ php app/console doctrine:schema:drop --force
Dropping database schema...

[Doctrine\DBAL\Schema\SchemaException]         
The table with name 'product' already exists.  

doctrine:schema:drop [--dump-sql] [--force] [--full-database] [--em[="..."]]

[Doctrine\DBAL\Schema\SchemaException]         
The table with name 'product' already exists.

数据库中没有表。清除了symfony和doctrine的所有缓存,但仍然抛出错误。

Symfony2 版本是 2.0.1 。

I am working with Symfony2 and Doctrine ORM using MySql .

After creating an Entity, I am not able to create the table. It throws an exception.

anu@anu-bridge:/var/www/Symfony$ php app/console doctrine:schema:update --force --dump-sql

[Doctrine\DBAL\Schema\SchemaException]
The table with name 'product' already exists.  

doctrine:schema:update [--complete] [--dump-sql] [--force] [--em[="..."]]

I tried to drop it , but still it throws the error.

anu@anu-bridge:/var/www/Symfony$ php app/console doctrine:schema:drop --force
Dropping database schema...

[Doctrine\DBAL\Schema\SchemaException]         
The table with name 'product' already exists.  

doctrine:schema:drop [--dump-sql] [--force] [--full-database] [--em[="..."]]

[Doctrine\DBAL\Schema\SchemaException]         
The table with name 'product' already exists.

There is no tables in the database. Cleared all the cache for symfony and doctrine, but still the error is throwing.

Symfony2 version is 2.0.1 .

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

等风也等你 2024-12-13 10:30:24

我也遇到过类似的问题。您很可能在不同的 Bundle 中有两个名为 Category 的实体。例如:

src/Acme/SomeBundle/Entity/Product.php
src/Acme/OtherBundle/Entity/Product.php

注释这些文件之一并重试控制台命令。

I've had a similar problem. Most likely you have two entities named Category inside different Bundles. For instance:

src/Acme/SomeBundle/Entity/Product.php
src/Acme/OtherBundle/Entity/Product.php

Comment one of these files and retry the console command.

夏末染殇 2024-12-13 10:30:24

我之所以遇到这个问题,是因为关联类注释中定义的联接表和多对多注释中定义的联接表发生冲突。

具有直接多对多关系的两个实体中的映射定义似乎导致使用“joinTable”注释自动创建连接表。但是,连接表已由其基础实体类中的注释定义,我希望它使用此关联实体类自己的字段定义,以便使用其他自定义字段扩展连接表。

解释和解决方案归功于论坛'Doctrine Annotation Question中的这篇文章'。这篇文章提请注意有关 多对多单向关系。查看有关使用“关联实体类”的方法的注释,从而用主实体类中的一对多注释和两个“多对”直接替换两个主实体类之间的多对多注释映射。关联实体类中的“-one”注释。此论坛帖子具有额外字段的关联模型中提供了一个示例:

public class Person
{
    /** 
     * @OneToMany(targetEntity="AssignedItems", mappedBy="person")
     */
    private $assignedItems;
}

public class Items
{
    /**
     * @OneToMany(targetEntity="AssignedItems", mappedBy="item")
     */
    private $assignedPeople;
}

public class AssignedItems
{
    /** 
     * @ManyToOne(targetEntity="Person")
     * @JoinColumn(name="person_id", referencedColumnName="id")
     */
    private $person;

    /** 
     * @ManyToOne(targetEntity="Item")
     * @JoinColumn(name="item_id", referencedColumnName="id")
     */
    private $item;
}

I was getting this problem from a conflict with join table defined in an association class annotation and a join table defined in a ManyToMany annotation.

The mapping definitions in two entities with a direct ManytoMany relationship appeared to result in the automatic creation of the join table using the 'joinTable' annotation. However the join table was already defined by an annotation in its underlying entity class and I wanted it to use this association entity class's own field definitions so as to extend the join table with additional custom fields.

The explanation and solution was thanks to this post in the forum 'Doctrine Annotation Question'. This post draws attention to the Doctrine documentation regarding ManyToMany Uni-directional relationships. Look at the note regarding the approach of using an 'association entity class' thus replacing the many-to-many annotation mapping directly between two main entity classes with a one-to-many annotation in the main entity classes and two 'many-to-one' annotations in the Associative Entity class. There is an example provided in this forum post Association models with extra fields:

public class Person
{
    /** 
     * @OneToMany(targetEntity="AssignedItems", mappedBy="person")
     */
    private $assignedItems;
}

public class Items
{
    /**
     * @OneToMany(targetEntity="AssignedItems", mappedBy="item")
     */
    private $assignedPeople;
}

public class AssignedItems
{
    /** 
     * @ManyToOne(targetEntity="Person")
     * @JoinColumn(name="person_id", referencedColumnName="id")
     */
    private $person;

    /** 
     * @ManyToOne(targetEntity="Item")
     * @JoinColumn(name="item_id", referencedColumnName="id")
     */
    private $item;
}
远昼 2024-12-13 10:30:24

我在编辑 Product.orm.yml 文件时遇到此错误。

我添加了一个与 Category 实体的新的多对多关系,并在 joinTable 行上犯了一个错误:

manyToMany:
  categories:
    targetEntity: Acme\ProductBundle\Entity\Category
    inversedBy: products
    joinTable:
      name: Product  # My mistake: joinTable should be something like ProductCategory

    [...]

确实这是一个愚蠢的错误,无论如何我都会分享。

I got this error when editing my Product.orm.yml file.

I added a new manyToMany relation with a Category entity, and made a mistake on the joinTable line :

manyToMany:
  categories:
    targetEntity: Acme\ProductBundle\Entity\Category
    inversedBy: products
    joinTable:
      name: Product  # My mistake: joinTable should be something like ProductCategory

    [...]

Indeed it's a silly error, I share anyway.

夏天碎花小短裙 2024-12-13 10:30:24

如果可以的话,您可以这样做,因为这对我有用:

删除整个数据库:

app/consoledoctrine:schema:drop --force --full-database

运行所有数据库迁移:

应用程序/控制台学说:迁移:迁移

If you can, you can do this as this worked for me:

Drop the entire database:

app/console doctrine:schema:drop --force --full-database

Run all DB migrations:

app/console doctrine:migrations:migrate

孤星 2024-12-13 10:30:24

我想复制实体时经常会发生这种情况。就我而言,这是一个不幸重复的 ORM 表名称注释。

/**
 * @ORM\Entity()
 * @ORM\Table(name="category")
 * */
class Category {

I imagine It can happen quite often when copying entities. In my case it was a ORM table name annotation that was misfortunately duplicated.

/**
 * @ORM\Entity()
 * @ORM\Table(name="category")
 * */
class Category {
情魔剑神 2024-12-13 10:30:24

我遇到了一个一对多,单向连接表关系的问题,例如(参见学说文档)。我没有通过互联网或 stackoverflow 找到这种关系类型的错误案例,所以我在这里发布我的解决方案,以帮助其他遇到同样问题的人。

导致此问题的原因:

  1. 使用 ORM 对遗留数据库表进行逆向工程后(参见文档“如何从现有数据库生成实体”),所有表也仅连接表都收到实体 PHP 类。
  2. 通过类别实体上的注释,我描述了连接。结果如下代码:
/**
 * @ORM\ManyToMany(targetEntity="Category")
 * @ORM\JoinTable(name="category_child",
 *     joinColumns={@JoinColumn(name="category_id", referencedColumnName="id")},
 *     inverseJoinColumns={@JoinColumn(name="category_child_id", referencedColumnName="id")}
 * )
 */
public $children;
  1. @ORM\JoinTable(name="category_child" 导致该学说想要再次创建此表。一次是因为已经存在 Category_Child 实体,然后 @ORM指向同一个表的 \JoinTable 表达式

解决方案 解决

方案是删除通过逆向工程创建的 Category_Child 实体 如果您在某些 $em 查询中使用了 Category_Child 实体,则必须选择这些数据。否则,例如通过将这些子数据保存在 ArrayCollection 中的父级,或通过 DBAL。

I had this problem with an One-To-Many, Unidirectional with Join Table relation like (see doctrine doc). I did not find this error case with this relation type through the internet or stackoverflow, so i post here my solution for let help others with the same problem.

What caused this problem:

  1. After reverse engineering the legacy db tables with ORM (see doc "How to Generate Entities from an Existing Database"), all tables also only join tables recieved an entity PHP class.
  2. With annotations on a category entity i described the joining. Results this code:
/**
 * @ORM\ManyToMany(targetEntity="Category")
 * @ORM\JoinTable(name="category_child",
 *     joinColumns={@JoinColumn(name="category_id", referencedColumnName="id")},
 *     inverseJoinColumns={@JoinColumn(name="category_child_id", referencedColumnName="id")}
 * )
 */
public $children;
  1. The @ORM\JoinTable(name="category_child" caused that doctrine wants to create this table again. One time because of the already existing Category_Child entity, and then the @ORM\JoinTable expression that points to the same table.

Solution

Solution was to delete the Category_Child entity that was created from reverse engineering. If you used Category_Child entity in some $em queries, you have to select those datas othwerwise. E.g. Through the parent that holds those child datas in an ArrayCollection, or over DBAL.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文