0

I have a table 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;

Then I have a table stock_detail which has a relation with previous stock as yours may see:

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;

I'm trying to build the right Entity for stock_detail. Since I need to use a AUTO_INCREMENT field there I don't know how to start, any help? How did yours handle with the right generation of ID's (PK)?

PS: I read docs here but isn't clear how this feet on those cases

4

1 回答 1

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();
        ...
    }
}

当然,这是一个粗略的大纲,但希望能传达这个想法。

于 2013-09-17T05:02:06.757 回答