MongoException: keys with zero length are not allowed, did you use $ with double quotes?

I use symfony2 and mongodb, everything is fine until today, but I am creating a new document, and suddenly this error appears:

"MongoException: keys with zero length are not allowed, did you use $ with double quotes?"

$dm = $this->get('doctrine.odm.mongodb.document_manager'); $_repo = $dm->getRepository('CantaoCustomerBundle:CustomerTags'); $_repo->findOneByCustomer($customer); 

$customer OK, the repository is empty, and my document class is as follows:

  /** * @MongoDB\ID **/ private $id; /** * @MongoDB\ReferenceOne(targetDocument="Tapronto\Mats\ProductBundle\Document\Tag", cascade={"persist"}) **/ private $tag; /** * @MongoDB\ReferenceOne(targetDocument="Tapronto\Mats\CustomerBundle\Document\Customer", cascade={"persist"}) **/ private $customer; /** * @MongoDB\Float **/ private $points; /** * @MongoDB\Int **/ private $viewed; /** * @MongoDB\Int **/ private $brought; /** * @MongoDB\Int **/ private $favorited; /** * @MongoDB\Date * @Gedmo\Timestampable(on="create") **/ private $createdAt; /** * @MongoDB\Date * @Gedmo\Timestampable(on="update") **/ private $updatedAt; 

Can someone help me, have an idea, I tried everything, and nothing works

+6
source share
2 answers

Perhaps you are trying to save a private attribute of an object.

If this is not the case, a good way to debug is to disable the zero-length key check so that you can actually debug, checking that it is written in mongo.

keys with zero length are not allowed, did you use $ with double quotes?

Code: 1

You tried to save "as a key. You should not do this at all." "can spoil access to the subobject and is used by MongoDB internally. However, if you really want to, you can set mongo.allow_empty_keys to true in the php.ini file to override this health check. If you override this, it is highly recommended that you set the error check to strict to avoid string interpolation errors.

http://php.net/manual/en/mongo.configuration.php#ini.mongo.allow-empty-keys

+5
source

I just fixed this using the object reference identifier instead of the link object itself as my search query.

 $_repo->findOneByCustomer($customer->getId()); 

EDIT: This is not an exception, but actually it returns nothing. I tried using the new MongoId ($ id) as several places were suggested ( Doctrine MongoDB find by id ), but that didn't work either. Finally, I found something in the full query builder that searches for links (note: this uses the object instead of the object identifier).

 $dm->createQueryBuilder()->find('CantaoCustomerBundle:CustomerTags') ->field('customer')->references($customer) ->getQuery()->execute(); 

It seems to me that this should be done more simply (as you did initially), but this fix works for me.

+5
source

Source: https://habr.com/ru/post/946954/


All Articles