I need to get the last Id from my table so i can use it in another function i created this function on my repository but i didn't work it show me an error:
[Syntax Error] line 0, col 60: Error: Expected end of string, got 'LIMIT'
the function i'am using :
public function LastIdCart()
{
$query = $this->getEntityManager()
->createQuery("select i.id from BoutiqueBundle:Panier i ORDER BY
i.id DESC LIMIT 1");
return $query->getResult();
}
class YourController extends Controller {
*********************************************************
public function yourAction( *** ) {
$youLastRecord = $this->getDoctrine()->getRepository(YourEntity::class)->findOneBy(
array(),
array('id' => 'DESC')
);
}
}
The correct way to use OFFSET and/or LIMIT with a DQL Query is using the following api on the $query object as follow:
Query::setMaxResults($maxResults)
Query::setFirstResult($offset)
As describe in the doc First and Max Result Items (DQL Query Only)
As example, your code should be like:
public function LastIdCart()
{
$query = $this->getEntityManager()
->createQuery("select i.id from BoutiqueBundle:Panier i ORDER BY
i.id");
$query->setMaxResults(1);
return $query->getResult(); // will return an arraycollection with only one element
}
Hope this clarify
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With