Php 具有复合主键的同态关系

Php 具有复合主键的同态关系,php,laravel,relationship,composite-primary-key,Php,Laravel,Relationship,Composite Primary Key,我有3个表,我试图建立订单产品和订单产品状态名称之间的关系。我有一个名为订单\产品\状态的转换/透视表。问题是我的主键,因为我在表中有3个主键,我不知道如何通过关系连接这3个表。 我的迁移是: 表订购产品: public function up() { Schema::create('order_products', function (Blueprint $table) { $table->integer('order_id')->unsigned();

我有3个表,我试图建立订单产品和订单产品状态名称之间的关系。我有一个名为订单\产品\状态的转换/透视表。问题是我的主键,因为我在表中有3个主键,我不知道如何通过关系连接这3个表。 我的迁移是:

表订购产品:

 public function up()
{
    Schema::create('order_products', function (Blueprint $table) {
        $table->integer('order_id')->unsigned();
        $table->integer('product_id')->unsigned();
        $table->integer('ordinal')->unsigned();
        $table->integer('size');

        $table->primary(['order_id', 'product_id', 'ordinal']);
        $table->foreign('order_id')->references('id')->on('orders');
        $table->foreign('product_id')->references('id')->on('products');
    });
}
表订单产品状态-这是我的订单产品和订单产品状态名称之间的转换/透视表

 public function up()
{
    Schema::create('order_product_statuses', function (Blueprint $table) {
        $table->integer('order_id')->unsigned();
        $table->integer('product_id')->unsigned();
        $table->integer('status_id')->unsigned();
        $table->integer('ordinal')->unsigned();
        $table->dateTime('created_at')->default(DB::raw('CURRENT_TIMESTAMP'));

        $table->foreign('order_id')->references('id')->on('orders');
        $table->foreign('status_id')->references('id')->on('order_product_status_names');
        $table->primary(['order_id', 'product_id', 'ordinal']);
    });
}
最后一个是订单产品状态名称

public function up()
{
    Schema::create('order_product_status_names', function (Blueprint $table) {
        $table->integer('id')->unsigned();
        $table->string('name');
        $table->string('code');
        $table->dateTime('created_at')->default(DB::raw('CURRENT_TIMESTAMP'));

        $table->primary('id');
    });
}

我知道这里有两种关系,但我不知道或者我可以声明这种关系(从订单\产品到订单\产品\状态\名称和反向)

好吧,我在这方面没有花太多时间,但我会这么做。正如@Devon提到的,我可能会在每个表中添加ID,因为Eloquent并不是为复合键设计的。正如我在一篇评论中提到的,我通常创建启动和更新脚本,因此语法可能不完全正确:

public function up() {
    Schema::create('order_products', function (Blueprint $table) {
        $table->bigIncrements('id')->unsigned();
        $table->integer('order_id')->unsigned();
        $table->integer('product_id')->unsigned();
        $table->integer('order_product_statuses_id')->unsigned();
        $table->integer('ordinal')->unsigned();
        $table->integer('size');

        $table->primary('id');
        $table->foreign('order_id')->references('id')->on('orders');
        $table->foreign('product_id')->references('id')->on('products');
        $table->foreign('order_product_statuses_id')->references('id')->on('order_product_statuses');
    });
}

public function up() {
    Schema::create('order_product_statuses', function (Blueprint $table) {
        $table->bigIncrements('id')->unsigned();
        $table->integer('product_id')->unsigned();
        $table->integer('status_id')->unsigned();
        $table->integer('ordinal')->unsigned();
        $table->dateTime('created_at')->default(DB::raw('CURRENT_TIMESTAMP'));

        $table->primary('id');
        $table->foreign('status_id')->references('id')->on('order_product_status_names');
    });
}

public function up() {
    Schema::create('order_product_status_names', function (Blueprint $table) {
        $table->bigIncrements('id')->unsigned();
        $table->string('name');
        $table->string('code');
        $table->dateTime('created_at')->default(DB::raw('CURRENT_TIMESTAMP'));

        $table->primary('id');
    });
}

HTH请稍等。

明天早上我们会更好地了解您的需求,看看我是否能帮您解决问题。简单地问一下,为什么您在订单产品和订单产品状态中都有订单id和产品id,这是出于性能原因吗?谢谢!我将等待并感激它!Eloquent并不是为复合键设计的。通过使db结构在每个表中都有一个
id
主键,您将省去很多麻烦。是的,因为我有3个主键(订单id、产品id和普通),当客户订购2个相同大小的相同产品时,我有2个不同的行,只有ordinal可以将它们与1个订单区分开来。当客户只从他们那里得到一个时,我可以在旁注上添加单独的状态。如果你想要一个关于db创建的好的入门课程,你应该在谷歌上搜索MongoDB大学并注册免费的M101N课程。我做数据库开发已经有一段时间了,这对我来说仍然是一门很棒的课程,我实际上学到了很多东西。你是对的,我不得不改变我的表格。。。Thnaks for Helping我也遇到了同样的问题,我不想让自动递增id作为主键,因为这确实限制了箭头的数量?正确的?当我们遇到这种问题,不想使用自动递增时,我们该怎么办?@Toinou我不太明白你在评论中所指的是什么“…这确实限制了箭头的数量?”?我通常会尝试找出集合/表/等中的唯一项,并将其用作主键。我认为使用自动增量作为主键的主要原因是因为它使分页更容易,而且是唯一的,尽管这只是我的想法&我可能完全偏离了轨道;-)。如果这不能回答您的问题,请提出一个新问题,并在此处提供问题的链接,以便社区/我可以尝试帮助您。希望有帮助(HTH)。