public function create($data) { if (!isset($data) || empty($data)) { throw new ValidationException('Data invalid', 'saleRecord'); } if (!isset($data->saleProducts) || empty($data->saleProducts)) { throw new ValidationException('There are no product in the order', 'saleProducts'); } $now = new \DateTime(); $saleRecord = new Order(); $orderItem = new OrderCart(); if ($data->phoneNumber) { $customer = $this->objectManager->getRepository('Application\\Entity\\Customer')->findOneBy(array('phoneNumber' => $data->phoneNumber)); if (!isset($customer)) { $customer = new Customer(); $customer->setPhoneNumber($data->phoneNumber); } $saleRecord->setCustomer($customer); } $saleRecord->setTotalPrice($data->totalPrice); $saleRecord->setCreateTime($now); foreach ($data->saleProducts as $product) { /**@var $productEntity \Application\Entity\Product*/ $productEntity = $this->objectManager->getRepository('Application\\Entity\\Product')->findOneBy(array('sku' => $product->sku)); if ($productEntity == null) { // if sku is not exists,add it throw new ValidationException("该产品不存在于库存中,也许你需要先进货:)", 'sku'); } $item = clone $orderItem; $stock = intval($productEntity->getStock()); $productEntity->setStock($stock - intval($product->quantity)); $item->setProduct($productEntity); $item->setPrice($product->price); $item->setQuantity($product->quantity); $item->setCreateTime($now); $item->setOrder($saleRecord); $saleRecord->addOrderCart($item); //@todo not work } $this->objectManager->persist($saleRecord); $this->objectManager->flush(); return true; }