使用Symfony2和Doctrine2处理此模型中的复合键

时间:2013-09-16 19:01:35

标签: symfony doctrine-orm doctrine symfony-2.3

我有一张表stock

CREATE TABLE IF NOT EXISTS `stock` (
  `product` VARCHAR(20) NOT NULL,
  `kcondition` TINYINT NOT NULL,
  `company` INT(11) NOT NULL,
  `status` INT(11) NOT NULL,
  `sku` VARCHAR(255) NULL,
  `unit` INT(11) NOT NULL,
  `width` DECIMAL(4,4) NULL,
  `height` DECIMAL(4,4) NULL,
  `weigth` DECIMAL(4,4) NULL,
  `length` DECIMAL(4,4) NULL,
  `created`  NOT NULL,
  `modified`  NULL DEFAULT NULL,
  `weigth_class` INT(11) NOT NULL,
  `length_class` INT(11) NOT NULL,
  PRIMARY KEY (`product`, `kcondition`, `company`),
  INDEX `stock_sku_fkey_idx` (`product` ASC),
  INDEX `fk_stock_n_stock_status1_idx` (`status` ASC),
  INDEX `fk_stock_company1_idx` (`company` ASC),
  INDEX `fk_stock_n_condition1_idx` (`kcondition` ASC),
  UNIQUE INDEX `sku_UNIQUE` (`sku` ASC, `product` ASC, `kcondition` ASC, `company` ASC, `status` ASC),
  INDEX `fk_stock_n_unit1_idx` (`unit` ASC),
  INDEX `fk_stock_n_weigth_class1_idx` (`weigth_class` ASC),
  INDEX `fk_stock_n_length_class1_idx` (`length_class` ASC),
  CONSTRAINT `fk_stock_company1`
    FOREIGN KEY (`company`)
    REFERENCES `company` (`id`)
    ON UPDATE CASCADE,
  CONSTRAINT `fk_stock_n_stock_status1`
    FOREIGN KEY (`status`)
    REFERENCES `n_stock_status` (`id`)
    ON UPDATE CASCADE,
  CONSTRAINT `stock_sku_fkey`
    FOREIGN KEY (`product`)
    REFERENCES `product` (`upc`)
    ON UPDATE CASCADE,
  CONSTRAINT `fk_stock_n_condition1`
    FOREIGN KEY (`kcondition`)
    REFERENCES `n_condition` (`id`)
    ON DELETE RESTRICT
    ON UPDATE RESTRICT,
  CONSTRAINT `fk_stock_n_unit1`
    FOREIGN KEY (`unit`)
    REFERENCES `n_unit` (`id`)
    ON DELETE RESTRICT
    ON UPDATE RESTRICT,
  CONSTRAINT `fk_stock_n_weigth_class1`
    FOREIGN KEY (`weigth_class`)
    REFERENCES `n_weigth_class` (`id`)
    ON DELETE RESTRICT
    ON UPDATE RESTRICT,
  CONSTRAINT `fk_stock_n_length_class1`
    FOREIGN KEY (`length_class`)
    REFERENCES `n_length_class` (`id`)
    ON DELETE RESTRICT
    ON UPDATE RESTRICT)
ENGINE = InnoDB;

然后我有一张表stock_detail与您之前可能看到的stock之前的关系:

CREATE TABLE IF NOT EXISTS `stock_detail` (
  `id` INT NOT NULL AUTO_INCREMENT,
  `product` VARCHAR(20) NOT NULL,
  `kcondition` TINYINT NOT NULL,
  `company` INT(11) NOT NULL,
  `price` DECIMAL(19,4) NOT NULL,
  `amount` INT NULL,
  `availability` INT NULL,
  PRIMARY KEY (`id`, `product`, `kcondition`, `company`),
  CONSTRAINT `fk_stock_detail_stock1`
    FOREIGN KEY (`product` , `kcondition` , `company`)
    REFERENCES `stock` (`product` , `kcondition` , `company`)
    ON DELETE RESTRICT
    ON UPDATE RESTRICT)
ENGINE = InnoDB;

我正在尝试为stock_detail构建正确的实体。因为我需要在那里使用AUTO_INCREMENT字段,我不知道如何启动,任何帮助?您是如何处理正确生成ID(PK)的?

PS:我读过docs here,但不清楚这种情况如何处理

1 个答案:

答案 0 :(得分:1)

Doctrine不允许在数据库端实现此目的。 但是,这样做的一种方法是获取最后一个id值并增加它。 这可以通过设置股票详细信息库来完成,如下所示:

class stockDetailRepository extends EntityRepository
{
    public function findLastEntry()
    {
        $limit = 1;

        return $this->getEntityManager()
                ->createQuery(
                    'SELECT s FROM YourBundle:StockDetail s 
                     ORDER BY s.id DESC'
                )
                ->setMaxResults($limit)
                ->getResult();
     }
...

然后在调用findLastEntry

之前在控制器中使用persist
class stockDetailController extends Controller
{
    public function updateStaticDetailAction()
    {
        ...
        $em = $this->getDoctrine()->getManager();
        $lastEntry = $em
         ->getRepository('YourBundle:stockDetail')
         ->findLastEntry();
        $lastId = $lastEntry[0]->getId()+1;

        $staticDetail = new StaticDetail();
        $staticDetail->set...
        ...
        $staticDetail->setId($lastId);
        $em->persist($staticDetail);
        $em->flush();
        ...
    }
}

当然这是一个粗略的概述,但希望能够了解这个想法。