如何在原则中的多对多连接表上添加索引定义?



我有两个实体和一个用于多对多关系的连接表:

实体/产品.php

namespace AppEntities;
use DoctrineORMMapping as ORM;
use DoctrineCommonCollectionsArrayCollection;
/**
* @ORMTable(name="product")
* @ORMEntity(repositoryClass="AppRepositoriesProductRepository")
*/
class Product
{
/**
* @ORMId
* @ORMGeneratedValue(strategy="AUTO")
* @ORMColumn(type="integer")
*
* @var int
*/
private $id;
/**
* Many Products have Many Processes
*
* @ORMManyToMany(targetEntity="Process", inversedBy="products")
* @ORMJoinTable(name="product_process")
*
* @var DoctrineCommonCollectionsArrayCollection
*/
private $processes;
public function __construct()
{
$this->processes = new ArrayCollection();
}
}

实体/流程.php

namespace AppEntities;
use DoctrineORMMapping as ORM;
use DoctrineCommonCollectionsArrayCollection;
/**
* @ORMTable(name="process")
* @ORMEntity(repositoryClass="AppRepositoriesProcessRepository")
*/
class Process
{
/**
* @ORMId
* @ORMGeneratedValue(strategy="AUTO")
* @ORMColumn(type="integer")
*
* @var int
*/
private $id;
/**
* Many Processes have Many Products
*
* @ORMManyToMany(targetEntity="Product", mappedBy="processes")
*
* @var DoctrineCommonCollectionsArrayCollection
*/
private $products;
public function __construct()
{
$this->products = new ArrayCollection();
}
}

当运行 doctrine:migrations:diff 命令时,它会创建productprocessproduct_process表。

我想在product_process连接表上创建索引和外键。

这可以通过对另外两个实体之一(ProductProcess(的教义ORM注释来实现吗?

您的示例应生成如下所示的 SQL:

CREATE TABLE product (
id INT AUTO_INCREMENT NOT NULL,
PRIMARY KEY(id)
) ENGINE = InnoDB;
CREATE TABLE product_process (
product_id INT NOT NULL,
process_id INT NOT NULL,
PRIMARY KEY(product_id, process_id)
) ENGINE = InnoDB;
CREATE TABLE process (
id INT AUTO_INCREMENT NOT NULL,
PRIMARY KEY(id)
) ENGINE = InnoDB;
ALTER TABLE product_process ADD FOREIGN KEY (product_id) REFERENCES Product(id);
ALTER TABLE product_process ADD FOREIGN KEY (process_id) REFERENCES Process(id);

生成的product_process表已具有关联映射中声明的必要主键索引和外键。

此处记录了此行为。

如果需要其他任何内容,则需要创建一个单独的实体ProductProcess,并在该实体上声明两个OneToMany关联(如果需要,还可以在关联的实体上声明反向ManyToOne关系(。

可以直接在该实体上声明其他映射信息。