doctrine/mongodb-odm — Issue #250: DiscriminatorMap field values injected too often
Author: AD7six
Created: 2012-02-08 15:30:45+00:00
State: open

Using a document with the following annotations:

/**
* @MongoDB\Document(collection="users")
* @MongoDB\InheritanceType("SINGLE_COLLECTION")
* @MongoDB\DiscriminatorField(fieldName="type")
* @MongoDB\DiscriminatorMap({"user"="User", "brand"="Brand", "merchant"="Merchant"})
*/
class User {

And code like so:

$return = $repository->createQueryBuider()
->update()
->field('_id')->equals(new \MongoId($userId))
->field('type')->set($type)
->getQuery()
->execute();

Executes a query like so (profiler output):

{
"ts" : ISODate("2012-02-08T10:18:29.076Z"),
"op" : "update", "ns" : "mydb.users",
"query" : {
"_id" : ObjectId("4efa6a7a411e208429000007"),
"type" : { "$in" : [ "user", "brand", "merchant" ] }
},
"updateobj" : {
"$set" : { "type" : "brand" },
"type" : { "$in" : [ "user", "brand", "merchant" ] }
},
"exception" : "assertion db/ops/update.h:315",
"millis" : 1,
"client" : "127.0.0.1",
"user" : ""
}

expected results would be a query ala:

db.users.update(
{_id: ObjectId("4efa6a7a411e208429000007"), },
{$set: {type: "brand}}
)

The problem isn't restricted to updates, any query which does /not/ have the discriminatory field as a top-level array key will have it auto-injected generating invalid syntax, for example:

$return = $repository->createQueryBuider()
->addOr($qb->expr()->field('email')->equals($input))
->addOr($qb->expr()->field('username')->equals($input))
->addOr($qb->expr()->field('name')->equals($input))
->getQuery()
->getSingleResult();

Will execute a query like so (profiler output):

{
"ts" : ISODate("2012-02-08T15:09:31.452Z"),
"op" : "query",
"ns" : "mydb.users",
"query" : {
"$query" : {
"$or" : {
"0" : {
"email" : "foo@bar.com",
"type" : {
"$in" : [ "user", "brand", "merchant" ]
}
},
"1" : {
"username" : "foo@bar.com",
"type" : {
"$in" : [ "user", "brand", "merchant" ]
}
},
"2" : {
"name" : "foo@bar.com",
"type" : {
"$in" : [ "user", "brand", "merchant" ]
}
},
"type" : {
"$in" : [ "user", "brand", "merchant" ]
}
},
"type" : {
"$in" : [ "user", "brand", "merchant" ]
}
},
"$orderby" : [ ]
},
"ntoreturn" : 1,
"exception" : "$or requires nonempty array",
"exceptionCode" : 13262,
"responseLength" : 73,
"millis" : 0,
"client" : "127.0.0.1",
"user" : ""
}

expected query conditions would be:

{
$or: [{email: "foo@bar.com"}, {username: "foo@bar.com"}, {name: "foo@bar.com"}],

}