Yes. If it hasn't been persisted then the order is still floating about and hasn't really been placed.
Sep 27, 2014
7
Level 1
Laracasts Command Bus
Hello,
I am trying to build a Laravel e-commerce site, in which the "Command Bus" approach is used.
I have a question about handling placing an order.
I guess when user places an order, the followings things will happen:
- Charge user credit card
- Create an order object
- Raise an event of order has been created
- Persistent the order
- Send user "order confirmation" email
In the handler:
$data = [
'amount' => 10,
...
]
// Charge user
$this->payment->charge($data);
// Create an order
$order = Order::place($data)
// Save order to database
$this->orderRepo->persistent($order);
// Dispatch events
$this->dispatchEventsFor($order);
In the order model:
public static function place($data)
{
$order = new static($data);
$order->raise(new OrderWasPlaced($order));
return $order;
}
Then we will send the email to user in the event listener, I'd like to have order_id to query the database about order info, however by the time the event OrderWasPlaced was raised, we have not saved the order object to database yet. Should I persistent the order object first, then raise an OrderWasPlaced event?
Level 3
Please or to participate in this conversation.