在CakePHP模型中实现关系

时间:2015-02-26 03:03:51

标签: mysql cakephp

我是Cake的新手,我正试图通过使用bake脚本尽可能地实现Model层。

我的数据库关系的实现让我有点担心。

CREATE TABLE IF NOT EXISTS `model_base_test`.`entrys` (
  `id` INT NOT NULL AUTO_INCREMENT,
  `entry_year` INT(2) NOT NULL,
  `contact_id` INT NOT NULL,
  `division_id` INT NOT NULL,
  `category_id` INT NOT NULL,
  `entry_name` VARCHAR(45) NULL DEFAULT NULL,
  `scale` VARCHAR(10) NULL DEFAULT NULL,
  `ootb` TINYINT(1) NULL DEFAULT NULL,
  `scratch` TINYINT(1) NULL DEFAULT NULL,
  `theme` TINYINT(1) NULL DEFAULT NULL,
  `score` DECIMAL(4,2) NULL DEFAULT NULL,
  `medal_id` INT NULL DEFAULT NULL,
  `image_id` INT NULL DEFAULT NULL,
  `created` DATETIME NULL DEFAULT NULL,
  `modified` DATETIME NULL DEFAULT NULL,
  PRIMARY KEY (`id`),
  INDEX `fk_entries_contacts_idx` (`contact_id` ASC),
  INDEX `fk_entries_groups_idx` (`category_id` ASC),
  INDEX `fk_entries_medals_idx` (`medal_id` ASC),
  UNIQUE INDEX `entry_id_UNIQUE` (`id` ASC),
  INDEX `fk_entrys_divisions_idx` (`division_id` ASC),
  CONSTRAINT `fk_entries_contacts`
    FOREIGN KEY (`contact_id`)
    REFERENCES `model_base_test`.`contacts` (`id`)
    ON DELETE NO ACTION
    ON UPDATE CASCADE,
  CONSTRAINT `fk_entries_groups`
    FOREIGN KEY (`category_id`)
    REFERENCES `model_base_test`.`categorys` (`id`)
    ON DELETE NO ACTION
    ON UPDATE CASCADE,
  CONSTRAINT `fk_entries_medals`
    FOREIGN KEY (`medal_id`)
    REFERENCES `model_base_test`.`medals` (`id`)
    ON DELETE NO ACTION
    ON UPDATE CASCADE,
  CONSTRAINT `fk_entrys_divisions`
    FOREIGN KEY (`division_id`)
    REFERENCES `model_base_test`.`divisions` (`id`)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION)
ENGINE = InnoDB;

CREATE TABLE IF NOT EXISTS `model_base_test`.`scores` (
  `id` INT NOT NULL AUTO_INCREMENT,
  `judge_id` INT NOT NULL,
  `entry_id` INT NOT NULL,
  `criteria` VARCHAR(15) NULL DEFAULT NULL,
  `possible_points` INT NULL DEFAULT NULL,
  `entry_possible` TINYINT(1) NULL DEFAULT NULL,
  `raw_score` INT NULL DEFAULT NULL,
  `score_adjustment` INT NULL DEFAULT NULL,
  `weighted_score` DECIMAL(5,2) NULL DEFAULT NULL,
  `created` DATETIME NULL DEFAULT NULL,
  `modified` DATETIME NULL DEFAULT NULL,
  PRIMARY KEY (`id`),
  INDEX `fk_scores_entries_idx` (`entry_id` ASC),
  UNIQUE INDEX `id_UNIQUE` (`id` ASC),
  CONSTRAINT `fk_scores_entries1`
    FOREIGN KEY (`entry_id`)
    REFERENCES `model_base_test`.`entrys` (`id`)
    ON DELETE CASCADE
    ON UPDATE CASCADE)
ENGINE = InnoDB;

以上面的表和约束为例,我如何才能最好地实现Cake?以上是可选的一对多关系,即强制Entry行不一定有Score行。

0 个答案:

没有答案