Skip to content

Create "notReferences" query ? #1769

Open
@lucalbert

Description

@lucalbert

Hello everyone,

In my application, I have a reference (One) between 2 entities:

namespace App\Document

class Offer
{
  /**
   * @MongoDB\ReferenceOne(targetDocument="Customer")
   */
  protected $customer;
}

When I want to retrieve all the references, no problem:

$repository = $container->get('doctrine_mongodb')->getManager()->getRepository(Offer::class);
$offers = $repository->findBy (['customer' => $customer]);

// or
$dm = $container->get('doctrine_mongodb')->getManager();
$qb = $dm->createQueryBuilder(Offer::class);

$offers = $qb
            ->field('customer')->references($customer)
            ->getQuery()
            ;

// or

$offers = $qb
             ->field('customer')->equals($customer)
             ->getQuery()
         ;

In both cases, the query is the same:

db.offers.find({ "customer.$id": ObjectId("5abe0295b4bfd00008089c73") });

Now, I want to get all the documents that do not have the reference !
When i try this :

$dm = $container->get ('doctrine_mongodb')->getManager();
$qb = $dm->createQueryBuilder(Offer::class);

$offers = $qb
             ->field('customer') ->notEqual($customer)
             -> getQuery()
         ;

I get the following error: "Catchable Fatal Error: Object of class MongoDBODMProxies_CG_\App\Document\Customer could not be converted to string"

If i try this:

$offers = $qb
            ->field('customer.$id')->notEqual($customer->getId())
            ->getQuery()
        ;

I get the following query:

db.offers.find({ "customer.$id": { "$ne": "5abe0295b4bfd00008089c73" } }); // It's not a ObjectId

How to launch such a query?

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions