MongoDBException: Invalid find by call

262 Views Asked by At

I have a MongoDBException:

"Invalid find by call Bundle\Document\Property::$fieldName (criteriaWith)".

I don't understand what is wrong here. Can someone help me please?

Here's the Stack Trace:

1 . in vendor/doctrine/mongodb-odm/lib/Doctrine/ODM/MongoDB/MongoDBException.php at line 38

public static function invalidFindByCall($documentName, $fieldName, $method) {
    return new self(sprintf('Invalid find by call %s::$fieldName (%s)', $documentName, $fieldName, $method));
}
  1. at

MongoDBException ::invalidFindByCall ('\Bundle\Document\Property', 'criteriaWith', 'findByCriteriaWith')

in vendor/doctrine/mongodb-odm/lib/Doctrine/ODM/MongoDB/DocumentRepository.php at line 222

if ($this->class->hasField($fieldName)) {
    return $this->$method(array($fieldName => $arguments[0]));
} else {
    throw MongoDBException::invalidFindByCall($this->documentName, $fieldName, $method . $by);
}
  1. at DocumentRepository ->__call ('findByCriteriaWith', array(array('name' => 'ho'))) in src/Bundle/Controller/PropertyController.php at line 286

    else {
         criteria['name'] = $name;
         $entities = $repository->findByCriteriaWith($criteria);
    }
    

The Log Messages:

CRITICAL request Uncaught PHP Exception Doctrine\ODM\MongoDB\MongoDBException: "Invalid find by call Bundle\Document\Property::$fieldName (criteriaWith)" at vendor/doctrine/mongodb-odm/lib/Doctrine/ODM/MongoDB/MongoDBException.php line 38 Context: { "exception": "Object(Doctrine\ODM\MongoDB\MongoDBException)" }

1

There are 1 best solutions below

0
On

Let's go through ODM's document repository magic __call method (as that's what is called since findByCriteriaWith does not exist) and highlight interesting parts.

First we see

 if (strpos($method, 'findBy') === 0) {
     $by = substr($method, 6, strlen($method));
     $method = 'findBy';
 }

Your method name starts with "findBy" so we will enter this block leaving us later with $method = 'findBy' and $by = 'CriteriaWith' Later on $by gets camelized, as that's standard for field/property name, and leaves us with $fieldName = 'criteriaWith'. Now next thing we see is $this->class->hasField($fieldName) check which results in exception you got because your class does not have a criteriaWith field.

Summing up $repository->findBySomething($value); is equivalent to $repository->findBy(['something' => $value]) and throws exception if field does not exist in your doocument.