Laravel关系属于具有复合主键的ToMany

时间:2017-08-22 17:59:08

标签: php laravel relationship composite-primary-key

我有3个表,我正在尝试在order_products和order_products_status_names之间建立关系。我有名为order_product_statuses的transition / pivot表。问题是我的PK,因为我在表格订单3 Pk,我不知道如何通过关系连接这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');
    });
}

表格订单产品状态 - 这是我在order_products和order_product_status_names之间的转换/数据透视表

 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');
    });
}

我知道这是关系blengsToMany有两种方式,但我不知道或者我可以声明这种关系(从order_products到order_product_status_names和反向)?

1 个答案:

答案 0 :(得分:1)

好的避风港花了很多时间在这上面,但这就是我要做的事情。同样正如@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你有点。

相关问题