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 8309075
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T18:59:52+00:00 2026-06-08T18:59:52+00:00

I can’t seem to get the following query to work. Basically, I am trying

  • 0

I can’t seem to get the following query to work. Basically, I am trying to add a message document to a conversation document as illustrated below:

public function reply($conversationId, Message $message, $flush = true)
{            
    $this->dm->createQueryBuilder($this->class)
        ->field('archivers')->unsetField()
        ->field('repliedBy')->set($message->getUserId())
        ->field('repliedBody')->set($message->getBody())
        ->field('repliedAt')->set(new \DateTime())
        ->field('modifiedAt')->set(new \DateTime())
        ->field('messages')->push($message)
        ->field('id')->equals(new \MongoId($conversationId))
        ->getQuery()
        ->execute();

    if ($flush) {
        $this->dm->flush();
    }
}

This reply method gets called in two ways. First by a user posting a message via a html form, and second by a REST call made by an Android application. The form works but the REST call fails (the rest implementation uses the JMSSerializerBundle with FOSRestBundle btw)…

I have verified that the code gets called and the parameters passed to the method are valid in both cases, but for some reason the commit() call inside UnitOfWork.php ignores the changes to the document. See line 413 to see what I mean.

Does anybody have an idea why this might be happening?

Below are the other approaches that I have tried:

First, I added an update() call which fails with “Catchable Fatal Error: Object of class … could not be converted to string in /vendor/bundles/Symfony/Bundle/DoctrineMongoDBBundle/Logger/DoctrineMongoDBLogger.php line 280”.

public function reply($conversationId, Message $message, $flush = true)
{            
    $this->dm->createQueryBuilder($this->class)
        ->update()
        ->field('archivers')->unsetField()
        ->field('repliedBy')->set($message->getUserId())
        ->field('repliedBody')->set($message->getBody())
        ->field('repliedAt')->set(new \DateTime())
        ->field('modifiedAt')->set(new \DateTime())
        ->field('messages')->push($message)
        ->field('id')->equals(new \MongoId($conversationId))
        ->getQuery()
        ->execute();

    if ($flush) {
        $this->dm->flush();
    }
}

Second approach I tried was pushing an array instead of an object:

public function reply($conversationId, Message $message)
{            
    $this->dm->createQueryBuilder($this->class)
        ->update()
        ->field('archivers')->unsetField()
        ->field('repliedBy')->set($message->getUserId())
        ->field('repliedBody')->set($message->getBody())
        ->field('repliedAt')->set(new \DateTime())
        ->field('modifiedAt')->set(new \DateTime())
        ->field('messages')->push(array(
            '_id' => new \MongoId(),
            'userId' => $message->getuserId(),
            'body' => $message->getBody(),
            'createdAt' => new \DateTime(),
            'modifiedAt' => new \DateTime(),
        ))
        ->field('id')->equals(new \MongoId($conversationId))
        ->getQuery() 
        ->execute();

    $this->dm->flush();
}

Which works fine until the flush() method gets called. The flush() causes duplicate objects to be pushed. So I get two copies of the same message in the conversation (commenting the flush() solves the issue but the application has multiple flush() calls in other classes).

Another push query that fails with an object:

public function archive($conversationId, $userId)
{

    $userStamp = new UserStamp();
    $userStamp->setUserId($userId);

    $this->dm->createQueryBuilder($this->class)
        ->update()
        ->field('archivers')->push($userStamp)
        ->field('modifiedAt')->set(new \DateTime())
        ->field('id')->equals(new \MongoId($conversationId))
        ->getQuery()
        ->execute();
}

If the push() call is removed, everything works fine.

Still stuck at this point.

  • 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-08T18:59:54+00:00Added an answer on June 8, 2026 at 6:59 pm

    The query builder is typically used to execute multi-document or command queries against MongoDB, which bypass document management. The only exception is if you are performing a hydrated find query, which is described in the query builder documentation. Your example above is equivalent to:

    $collection->update(
        ['_id' => new \MongoId($conversationId)],
        [
            '$set' => [
                'repliedBy' => $message->getUserId(),
                'repliedBody' => $message->getbody(),
                'repliedAt' => new \MongoDate(),
                'modifiedAt' => new \MongoDate(),
            ],
            '$push' => ['messages' => $message],
        ],
        ['multiple' => false]
    );
    

    Note that your field mappings will be utilized (e.g. Datetime will become a MongoDate), but there is no document to manage with this query.

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

Sidebar

Related Questions

Can any one tell, how to get the result of LINQ query contains group
Can't seem to figure out what's wrong with the simple getJSON call below. It's
Can anyone help me trying to find out why this doesn't work. The brushes
Can't seem to get the Back Button to appear in a UINavigationController flow. I
Can this query below be optimized? select max(date), sysdate - max(date) from table; Query
Can I change the field public virtual ClassOne ClassOne { get; set; } to
Can someone thoroughly explain the last line of the following code: def myMethod(self): #
Basically, what I'm trying to create is a page of div tags, each has
Can anyone give me an example of how to return the following json simply
Can anybody tell what is the best(easy) way to sort the following string 'index'

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.