本文整理汇总了PHP中common\models\Order::getDb方法的典型用法代码示例。如果您正苦于以下问题:PHP Order::getDb方法的具体用法?PHP Order::getDb怎么用?PHP Order::getDb使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类common\models\Order
的用法示例。
在下文中一共展示了Order::getDb方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: actionOrder
public function actionOrder()
{
$order = new Order();
/** @var ShoppingCart $cart */
$cart = \Yii::$app->cart;
/* @var $products Product[] */
$products = $cart->getPositions();
if ($order->load(\Yii::$app->request->post()) && $order->validate()) {
$transaction = $order->getDb()->beginTransaction();
$order->save(false);
// part of the transaction;
foreach ($products as $product) {
$orderItem = new OrderItem();
$orderItem->order_id = $order->id;
$orderItem->product_id = $product->id;
$orderItem->title = $product->title;
$orderItem->price = $product->getPrice();
$orderItem->quantity = $product->getQuantity();
if (!$orderItem->save(false)) {
$transaction->rollBack();
Yii::$app->session->setFlash('error', 'Your order cannot be accepted. Please contact us!');
$this->goBack();
}
}
$transaction->commit();
Yii::$app->cart->removeAll();
Yii::$app->session->addFlash('message', 'Thanks for your order. We\'ll contact you soon!');
// $order->sendEmail();
$this->redirect('/');
}
return $this->render('order', ['order' => $order, 'products' => $products, 'total' => $cart->getCost()]);
}
示例2: actionOrder
public function actionOrder()
{
$order = new Order();
/* @var $cart ShoppingCart */
$cart = \Yii::$app->cart;
/* @var $products Product[] */
$products = $cart->getPositions();
$total = $cart->getCost();
if ($order->load(\Yii::$app->request->post()) && $order->validate()) {
$transaction = $order->getDb()->beginTransaction();
$order->save(false);
foreach ($products as $product) {
$orderItem = new OrderItem();
$orderItem->order_id = $order->id;
$orderItem->title = $product->title;
$orderItem->price = $product->getPrice();
$orderItem->product_id = $product->id;
$orderItem->quantity = $product->getQuantity();
if (!$orderItem->save(false)) {
$transaction->rollBack();
\Yii::$app->session->addFlash('error', 'Cannot place your order. Please contact us.');
return $this->redirect('catalog/list');
}
}
$transaction->commit();
\Yii::$app->cart->removeAll();
\Yii::$app->session->addFlash('success', 'Thanks for your order. We\'ll contact you soon.');
$order->sendEmail();
return $this->redirect('catalog/list');
}
return $this->render('order', ['order' => $order, 'products' => $products, 'total' => $total]);
}