Reputation: 11348
I'm using doctrine Doctrine MongoDB ODM 1.0.3. When trying to update document using doctrine I'm getting the following error:
Class XXX is not a valid document or mapped super class.
I have the following class for the document:
<?php
namespace Documents;
use Doctrine\ODM\MongoDB\Mapping\Annotations as ODM;
/**
* @ODM\Document(collection="posts")
*/
class Posts
{
/** @ODM\Id */
private $id;
/** @ODM\Field(type="string") */
private $title;
/** @ODM\EmbedMany(targetDocument="Comment") */
private $comments = array();
public function setTitle($title)
{
$this->title = $title;
}
public function getTitle()
{
return $this->title;
}
public function addComment($comment)
{
$this->comments[] = $comment;
}
public function getComments()
{
return $this->comments;
}
}
The following code is used to add new document:
$post = new \Documents\Posts();
$post->setTitle( $_POST['title'] );
$dm->persist($post);
$dm->flush();
Later I want to update the added document to add new comment for example. I use the following code:
$comment = new \Documents\Comment($_POST['comment_text']);
$dm->createQueryBuilder('Posts')
->update()
->field('comments')->push($comment)
->field('_id')->equals(new \MongoId($_POST['id']))
->getQuery()
->execute();
but getting the above mentioned error.
Upvotes: 4
Views: 1860
Reputation: 4348
Based on the other varying answers here, it seems this error isn't super precise.
In my case the class was missing the EmbeddedDocument annotation.
namespace Foo;
use Doctrine\ODM\MongoDB\Mapping\Annotations as ODM;
/**
* @ODM\EmbeddedDocument
*/
class Bar { }
Upvotes: 0
Reputation: 116
For people getting this error in PHP 8 and above, check that you are not mixing annotations (e.g. /** @Entity */
) and attributes (e.g. #[Entity]
), and that you have indicated which method you are using in your config:
mappings:
App:
# pick one:
type: annotation
type: attribute
This always trips me up when I start a new project and the config defaults to annotations when I'm used to using attributes.
Upvotes: 4
Reputation: 7762
As you stated in your own answer you need to provide fully qualified name of class. Just wanted to add that better than to pass the string it is to use static class property like this instead: createQueryBuilder(\Documents\Posts::class);
It works much better with IDEs (autocompletion, refactoring etc...)
Upvotes: 1
Reputation: 11348
In case anyone else have a similar problem, you need to pass fully qualified class name to createQueryBuilder
. My document classes are all inside Documents
namespace so after passing it like this createQueryBuilder('\Documents\Posts')
the problem is solved.
Upvotes: 0