CakePHP 3:保存和检索嵌套的BelongsToMany关联

时间:2018-11-09 16:01:59

标签: php cakephp-3.0 has-and-belongs-to-many

我的数据库中有一个复杂的关联。

我正在尝试在targets中创建新记录。每个目标都有很多产品,每个产品都有很多选择。联接表都有一个joinData列(点)。 我可以轻松填充products_targets条记录

echo $this->Form->hidden("products.{$i}.id", ['value' => $Product->id]);
echo $this->Form->hidden("products.{$i}._joinData.product_id", ['value' => $Product->id]);
echo $this->Form->hidden("products.{$i}._joinData.target_id", ['value' => $Element->id]);
echo $this->Form->number("products.{$i}._joinData.points")

我找不到填充options_products_products_targets表(即options_productsproducts_targets之间的联接表)的方法。

我尝试使用以下代码:

echo $this->Form->number("products.{$i}.options.{$j}._joinData.points");

,但显然这不是正确的方法。 我如何在这样的BelongsToMany关联上保存BelongsToMany关联?

这是所涉及表的屏幕快照,随后将缩短SQL。 enter image description here

CREATE TABLE IF NOT EXISTS `mg`.`targets` (
  `id` INT NOT NULL AUTO_INCREMENT,
  `status_id` INT NOT NULL DEFAULT 1
  PRIMARY KEY (`id`),
  CONSTRAINT `fk_targets_1`
    FOREIGN KEY (`status_id`)
    REFERENCES `mg`.`statuses` (`id`)
    ON DELETE CASCADE
    ON UPDATE CASCADE)
ENGINE = InnoDB;

CREATE TABLE IF NOT EXISTS `mg`.`products_targets` (
  `id` INT NOT NULL AUTO_INCREMENT,
  `product_id` INT NOT NULL,
  `target_id` INT NOT NULL,
  `points` DOUBLE NOT NULL DEFAULT 0
  PRIMARY KEY (`id`),
  CONSTRAINT `fk_products_targets_1`
    FOREIGN KEY (`product_id`)
    REFERENCES `mg`.`products` (`id`)
    ON DELETE CASCADE
    ON UPDATE CASCADE,
  CONSTRAINT `fk_products_targets_2`
    FOREIGN KEY (`target_id`)
    REFERENCES `mg`.`targets` (`id`)
    ON DELETE CASCADE
    ON UPDATE CASCADE)
ENGINE = InnoDB;

CREATE TABLE IF NOT EXISTS `mg`.`products` (
  `id` INT NOT NULL AUTO_INCREMENT,
  `status_id` INT NOT NULL DEFAULT 1,
  `offer_id` INT NOT NULL,
  `technology_id` INT NOT NULL,
  `typology_id` INT NOT NULL
  PRIMARY KEY (`id`),
  CONSTRAINT `fk_products_1`
    FOREIGN KEY (`status_id`)
    REFERENCES `mg`.`statuses` (`id`)
    ON DELETE CASCADE
    ON UPDATE CASCADE,
  CONSTRAINT `fk_products_2`
    FOREIGN KEY (`offer_id`)
    REFERENCES `mg`.`offers` (`id`)
    ON DELETE CASCADE
    ON UPDATE CASCADE,
  CONSTRAINT `fk_products_3`
    FOREIGN KEY (`technology_id`)
    REFERENCES `mg`.`technologies` (`id`)
    ON DELETE CASCADE
    ON UPDATE CASCADE,
  CONSTRAINT `fk_products_4`
    FOREIGN KEY (`typology_id`)
    REFERENCES `mg`.`typologies` (`id`)
    ON DELETE CASCADE
    ON UPDATE CASCADE)
ENGINE = InnoDB;

CREATE TABLE IF NOT EXISTS `mg`.`options_products` (
  `id` INT NOT NULL AUTO_INCREMENT,
  `option_id` INT NOT NULL,
  `product_id` INT NOT NULL
  PRIMARY KEY (`id`),
  CONSTRAINT `fk_options_products_1`
    FOREIGN KEY (`option_id`)
    REFERENCES `mg`.`options` (`id`)
    ON DELETE CASCADE
    ON UPDATE CASCADE,
  CONSTRAINT `fk_options_products_2`
    FOREIGN KEY (`product_id`)
    REFERENCES `mg`.`products` (`id`)
    ON DELETE CASCADE
    ON UPDATE CASCADE)
ENGINE = InnoDB;

CREATE TABLE IF NOT EXISTS `mg`.`options_products_products_targets` (
  `id` INT NOT NULL AUTO_INCREMENT,
  `options_product_id` INT NOT NULL,
  `products_target_id` INT NOT NULL,
  `points` DOUBLE NOT NULL DEFAULT 0
  PRIMARY KEY (`id`),
  CONSTRAINT `fk_options_products_products_targets_1`
    FOREIGN KEY (`options_product_id`)
    REFERENCES `mg`.`options_products` (`id`)
    ON DELETE CASCADE
    ON UPDATE CASCADE,
  CONSTRAINT `fk_options_products_products_targets_2`
    FOREIGN KEY (`products_target_id`)
    REFERENCES `mg`.`products_targets` (`id`)
    ON DELETE CASCADE
    ON UPDATE CASCADE)
ENGINE = InnoDB;

0 个答案:

没有答案