Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8218121
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T12:40:54+00:00 2026-06-07T12:40:54+00:00

Here is my DQL Query in MessageRepository : public function getLastByIdAndByType($id_employee, $type) { $query

  • 0

Here is my DQL Query in MessageRepository :

public function getLastByIdAndByType($id_employee, $type)
{
    $query = $this->_em->createQuery('SELECT MAX(m.sentAt), m.recipient,   m.author, m.subject, m.body, m.type FROM MyBundle:Message m JOIN m.employee e WHERE e.id = :id_employee AND m.type = :type')
    ->setParameter('id_employee', $id_employee)
    ->setParameter('type', $type)
    return $query->getSingleResult();
}    </code>

In my controller :
$last_message=$em->getRepository('MyBundle:Message')->getLastByIdAndByType($id, $type);

In my view html.twig {{ last_message.sentAt }}

returns the error : Item "sentAt" for "Array" does not exist

While

{{ last_message.recipient }} works but it’s true that for all my messages the recipient is the same.

What should I do to render the most recent date with {{ last_message.sentAt }} ?
Thanks a lot!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T12:40:56+00:00Added an answer on June 7, 2026 at 12:40 pm

    It looks like you’re using the wrong approach, if I’m understanding what you want to achieve.

    Going by the function name, I’m assuming that you want to retrieve information about the most recent message that was sent by an employee.
    For a case like that, what you want to do is order the messages by sentAt and select just the first one.

    What you’re actually doing is using an aggregate function, which will give you a computed value that isn’t actually a part of your Message entity.

    Here’s an example of how to do what you want (note that because I’m retrieving the whole entity, you can use whatever properties on it that you like in your view – for performance reasons you could still choose to select the individual properties in the query):

    public function getLastByIdAndByType($id_employee, $type)
    {
        $query = $this->_em->createQuery('SELECT m FROM MyBundle:Message m JOIN m.employee e WHERE e.id = :id_employee AND m.type = :type ORDER BY m.sentAt DESC')
                           ->setMaxResults(1)
                           ->setParameter('id_employee', $id_employee)
                           ->setParameter('type', $type);
        return $query->getOneOrNullResult();
    }
    

    Also worth noting is that you can use getOneOrNullResult as I did here instead of just getSingleResult to avoid throwing an exception if there was no record found. Purely a matter of choice, but it means that you have that possibility in mind while you’re coding.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

here is code: <script type=text/javascript> function doit(){ $('table td').each(function () { if ($(this).text().trim() !=
Here is the code in a function I'm trying to revise. This example works
Here is the Javascript I currently have <script type=text/javascript> $(function() { $('.slideshow').hover( function() {
I'm trying to use DQL to create a query between a ManyToMany relation, here
Here is my query: SELECT * FROM [GeoName] WHERE ((-26.3665122100029-Lat)*(-26.3665122100029-Lat))+((27.5978928658078-Long)*(27.5978928658078-Long)) < 0.005 ORDER BY
Here's a query that works fine: SELECT rowid as msg_rowid, a, b, c FROM
Here's my test function (c#, visual studio 2010): [TestMethod()] public void TestGetRelevantWeeks() { List<sbyte>
I am trying to query documentum server using DQL query. Using DATEDIFF function to
Here is a simplefied code: Class Bar { static $foo; public function __construct(Foo $foo)
Here's the view: @if (stream.StreamSourceId == 1) { <img class=source src=@Url.Content(~/Public/assets/images/own3dlogo.png) alt= /> }

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.