I've got a problem with Symfony2.0 BETA3 and MongoDB. I want create a Document, where a Field References to another Class, this might look like this:
namespace test\TestBundle\Document;
use test\TestBundle\Document\Location;
use Doctrine\ODM\MongoDB\Mapping\Annotations as ODM;
/**
* @ODM\Document(collection="locationcache", repositoryClass="test\TestBundle\Document\LocationCacheRepository")
*/
class LocationCache
{
// many more fields...
/**
* @var Location
* @ODM\ReferenceOne(targetDocument="test\TestBundle\Document\Location")
*/
protected $location;
/**
* @param Location
*/
public function setLocation(Location $location)
{
$this->location = $location;
}
/**
* @return Location
*/
public function getLocation()
{
return $this->location;
}
}
But if I want to find a location by $id like this
class LocationCacheRepository extends DocumentRepository
{
public function findByLocationID(MongoId $locationID)
{
return $this->createQueryBuilder()
->field('location.$id')->equals($locationID)
->sort('year', 'asc')
->sort('month', 'asc')
->getQuery()
->execute();
}
}
I will get this error
No mapping found for field 'location' in class 'test\TestBundle\Document\LocationCache'.
UPDATE
Here is a Document
Array
(
[_id] => 4dd637e706936bbcc0ac012d
[days] => Array
(
[1] => Array
(
[money] => 9
)
[2] => Array
(
[money] => 21
)
[3] => Array
(
[money] => 38
)
[4] => Array
开发者_JAVA技巧 (
[money] => 6
)
[18] => Array
(
[money] => 6
)
[19] => Array
(
[money] => 3
)
[31] => Array
(
[money] => 11
)
)
[location] => Array
(
[$ref] => location
[$id] => 4dd554c91c911a6606000000
[$db] => test
)
[money] => 94
[month] => 1
[year] => 2011
)
I don't know what's the problem with the class. Could please someone help?
Thanks in advance!
- Monty
If you want search on any location field you need to use 'embedOne' instead of 'referenceOne'. ReferenceOne not copy location fields into parent document, it just data like this (i don't remember exactly):
{
refId: '1',
refColl: 'locations',
refDb: 'location_database'
}
But in general if you need query only by location id you just need take a look how location reference looks like in mongodb using mongoshell or some other tool.
So you query will be like this:
->field('location.$refId')->equals($locationID)
精彩评论