【问题标题】:How to add an index definition on Many-To-Many JoinTable in Doctrine?如何在 Doctrine 中的多对多 JoinTable 上添加索引定义?
【发布时间】:2019-12-22 14:50:36
【问题描述】:

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

Entities/Product.php

namespace App\Entities;

use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;

/**
 * @ORM\Table(name="product")
 * @ORM\Entity(repositoryClass="App\Repositories\ProductRepository")
 */
class Product
{
    /**
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     * @ORM\Column(type="integer")
     *
     * @var int
     */
    private $id;

    /**
     * Many Products have Many Processes
     *
     * @ORM\ManyToMany(targetEntity="Process", inversedBy="products")
     * @ORM\JoinTable(name="product_process")
     *
     * @var \Doctrine\Common\Collections\ArrayCollection
     */
    private $processes;

    public function __construct()
    {
        $this->processes = new ArrayCollection();
    }

}

Entities/Process.php

namespace App\Entities;

use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;

/**
 * @ORM\Table(name="process")
 * @ORM\Entity(repositoryClass="App\Repositories\ProcessRepository")
 */
class Process
{
    /**
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     * @ORM\Column(type="integer")
     *
     * @var int
     */
    private $id;

    /**
     * Many Processes have Many Products
     *
     * @ORM\ManyToMany(targetEntity="Product", mappedBy="processes")
     *
     * @var \Doctrine\Common\Collections\ArrayCollection
     */
    private $products;

    public function __construct()
    {
        $this->products = new ArrayCollection();
    }

}

在运行 dictionary:migrations:diff 命令时,它创建了 productprocessproduct_process 表。

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

这是否可以通过其他两个实体之一(ProductProcess)上的 Doctrine ORM 注释来实现?

【问题讨论】:

    标签: php symfony doctrine


    【解决方案1】:

    您的示例应该生成这样的 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 表已经具有关联映射中声明的必要主键索引和外键。

    此行为已记录在 here

    如果您需要其他任何东西,您需要创建一个单独的实体 ProductProcess,并在该实体上声明两个 OneToMany 关联(如果需要,在关联实体上反转 ManyToOne 关系)。

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

    【讨论】:

    • 是的,它生成了那些 SQL 迁移,但问题是我向它添加了一些索引,例如:“CREATE TABLE product_process (product_id INT NOT NULL, process_id INT NOT NULL, INDEX IDX_product_process_product_id (product_id ), INDEX IDX_product_process_process_id (process_id), PRIMARY KEY(product_id, process_id))"
    • 当我运行“migrations:diff”命令时,它会生成如下内容:ALTER TABLE product_process RENAME INDEX idx_product_process_product_id TO IDX_57C403264584665A
    • 您不需要为主键添加索引。 Primary keys are always indexed.
    • 嗯,那么 Doctrine 中存在某种错误。因为当我粘贴您的简单 SQL 时,它会生成以下索引键:“fk_product_process_process_id”。我不知道为什么,这种索引在提供的 SQL 迁移中是没有的。当我运行 migrations:diff 之后,它会生成以下 SQL:“ALTER TABLE product_process RENAME INDEX fk_product_process_process_id TO IDX_57C403267EC2F574”
    • 没有错误。您无法使用 Doctrine 控制自动生成的索引的名称,doctrine 会自动管理这些名称。如果您手动设置索引,Doctrine 会将它们重命名为它想要生成的任何名称。
    猜你喜欢
    • 2015-07-04
    • 1970-01-01
    • 2016-02-24
    • 2012-02-12
    • 1970-01-01
    • 1970-01-01
    • 2023-04-08
    • 2011-05-28
    • 1970-01-01
    相关资源
    最近更新 更多