Reputation: 25
I have this query.
// build query to get their order details
$orderEntity = $em->createQueryBuilder()
->select('oh.orderHeaderId','oh.orderDate', 'oh.orderTotal', 'od.createdDate', 'od.productName', 'od.flashSalePrice', 'od.itemQuantity')
->from('FLOEcommerceBundle:OrderHeader','oh')
->leftJoin('FLOEcommerceBundle:OrderDetail', 'od' ,'WITH', 'oh.orderHeaderId = od.orderHeaderId')
->where('oh.memberId = ?1')
->orderby('oh.orderDate' , 'desc')
->setParameter(1, $memberId)
->getQuery()
->getArrayResult();
The result set dumped is.
array:3 [▼
0 => array:7 [▼
"orderHeaderId" => 25
"orderDate" => DateTime {#3906 ▼
+"date": "2015-07-15 11:27:59.000000"
+"timezone_type": 3
+"timezone": "America/Tegucigalpa"
}
"orderTotal" => "33.9600"
"createdDate" => DateTime {#3904 ▼
+"date": "2015-07-15 11:28:00.000000"
+"timezone_type": 3
+"timezone": "America/Tegucigalpa"
}
"productName" => "Plain Skirt"
"flashSalePrice" => "10.9900"
"itemQuantity" => 1
]
My questions are a) how do return just the date? and/or b) how do I loop through the "orderDate" key to get the "date"? I using a foreach to loop through the arrays to build an output variable. Thank you in advance.
foreach ( $orderEntity as $items ) {
foreach ( $items as $key => $value ) {
if( 'orderHeaderId' == $key) {
$orders .= ' OrderId: ' . $value . $br;
}
}
foreach ( $items as $key => $value ) {
if( 'orderDate' == $key ) {
$orders .= ' OrderDate: ' . $value . $br;
}
}
}
Upvotes: 1
Views: 2008
Reputation: 243
Using getScalarResult()
instead of getArrayResult()
will truncate any DateTime
objects to a string with just the date.
Upvotes: 4
Reputation: 1919
This answer by me should help. Basically you have two options, Either modify your entity class OrderHeader
or every time you come across a datetime object, extract the date from the variable. The code will go something like this.
In your entity class
/**
* @return \DateTime
*/
public function getcreatedDate()
{
$returnValue = $this->createdDate->format('y:m:d') //You can change it to your needed format.
return $returnValue
}
And if you want you can always modify it the same way in your controller. (I personally prefer modifying it in the entity itself if you don't care about other data except the date)
Upvotes: 3