doctrine-orm - 表格已经存在

标签 doctrine-orm zend-framework2

我在zend Framework 2中使用准则2。下面是我的实体文件。问题是,当我尝试使用验证架构时,

./vendor/bin/doctrine-module orm:validate-schema

命令。

我遇到错误了
[Doctrine\DBAL\Schema\SchemaException]                               
The table with name 'database.opportunitycriteria' already exists.

我该怎么办?
namespace Administration\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * OpportunityCriteria
 *
 * @ORM\Table(name="OpportunityCriteria")
 * @ORM\Entity
 */
class Criteria
{
/**
 * @var integer
 * @ORM\Id
 * @ORM\Column(name="criteria_id", type="integer", nullable=false)
 */
private $criteria_id;

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

以及适当的getter和setter方法。

最佳答案

我终于弄明白了。 OP的用例可能有所不同,但在我的情况下,这是由于配置错误的双向多对多关系。

我有以下实体:

class Cuisine {
    /**
     * @ManyToMany(targetEntity="Dish")
     * @ORM\JoinTable(name="CuisineDish", ...)
     */
    protected $dishes;
}

class Dish {
    /**
     * @ORM\ManyToMany(targetEntity="Cuisine")
     * @ORM\JoinTable(name="CuisineDish", ...)
     */
    protected $cuisines;
}

缺少的是inversedBy批注的mappedBy@ManyToMany属性。仅当关联为双向时才需要这些。

所以现在正确映射的实体看起来像:

class Cuisine {
    /**
     * @ManyToMany(targetEntity="Dish", inversedBy="cuisines")
     * @ORM\JoinTable(name="CuisineDish", )
     */
    protected $dishes;
}

class Dish {
    /**
     * @ORM\ManyToMany(targetEntity="Cuisine", mappedBy="dishes")
     * @ORM\JoinTable(name="CuisineDish", ...)
     */
    protected $cuisines;
}

并且orm:validate-schema不再异常退出。

异常消息只是误导,因为此操作不会更改数据库。此外,仅在验证与数据库的同步时才发现此问题,而在仅验证映射(--skip-sync)时才发现该问题。

我只是reported this bug

关于doctrine-orm - 表格已经存在,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18341692/

相关文章:

php - Symfony2 外键验证约束

mysql - 命令不同步;您现在无法运行此命令 - EXT :indexed_search 的 TYPO3 后端模块上出现错误

php - 交响乐/主义 : Getting the first maximum value from database

php - 在 ZF2 中如何在 WHERE 子句中执行 OR 条件

php - 使用 zfcuser 和自定义模块选择字段

php - 如何从 View 模块获取 $_POST 请求?

postgresql - Symfony2 - 如何限制用户可以存储的记录数?

php - Symfony2 MongoDB 多连接错误

php - rackspace 子容器不上传文件

PHPUnit 模拟 View 助手 ZF2