我有一些包含产品的类别,而您可以将产品添加到类别中。这是多对多关系,而产品不知道它们与类别的关系(因为它只保存在一个类别中)。我将首先发布我的设置,然后发布出现问题的查询。
/** @ODM\Document */
class Category
{
/** @ODM\Id */
private $id;
/** @ODM\String */
private $name;
/** @ODM\ReferenceMany(targetDocument="Product", inversedBy="category") */
private $products;
public function setProducts($products)
{
$this->products = $products;
}
}
/** @ODM\Document */
class Product
{
/** @ODM\Id */
private $id;
/** @ODM\String */
private $name;
/** @ODM\Float */
private $price;
/** @ODM\ReferenceMany(targetDocument="Category", mappedBy="products") */
private $category;
}
为了有一些数据,我这样创建它:
$p1 = new Documents\Product();
$p1->setName('p1');
$p1->setPrice(1.99);
$p2 = new Documents\Product();
$p2->setName('p2');
$p2->setPrice(3.99);
$c1 = new Documents\Category();
$c1->setName('category1');
$c1->setProducts(array($p1, $p2));
$c2 = new Documents\Category();
$c2->setName('category2');
$c2->setProducts(array($p1, $p2));
$dm->persist($p1);
$dm->persist($p2);
$dm->persist($c1);
$dm->persist($c2);
$dm->flush();
调查 MongoDB,我现在有以下数据:
db.Category.find()
{ "_id" : ObjectId("53e3d8d3e2afec2303d63afa"), "name" : "category1", "products" : [ DBRef("Product", ObjectId("53e3d8d3e2afec2303d63af8")), DBRef("Product", ObjectId("53e3d8d3e2afec2303d63af9")) ] }
{ "_id" : ObjectId("53e3d8d3e2afec2303d63afb"), "name" : "category2", "products" : [ DBRef("Product", ObjectId("53e3d8d3e2afec2303d63af8")), DBRef("Product", ObjectId("53e3d8d3e2afec2303d63af9")) ] }
db.Product.find()
{ "_id" : ObjectId("53e3d8d3e2afec2303d63af8"), "name" : "p1", "price" : 1.99 }
{ "_id" : ObjectId("53e3d8d3e2afec2303d63af9"), "name" : "p2", "price" : 3.99 }
我现在要查询这个数据:Getting a product by id and getting all its categories,它属于:
$product = $dm->find('Documents\Product', '53e3d8d3e2afec2303d63af8');
var_export($product->getName());
$category = $product->getCategory();
var_export(sizeof($category));
foreach($category as $c){
var_export($c->getName());
}
// Outputs: 'p1' 2 'category1' 'category2'
但是如果我想反过来查询数据:获取一个类别并获取其所有产品,该类别具有:
$category = $dm->find('Documents\Category', '53e3d8d3e2afec2303d63afa');
var_export($category->getName());
$products = $category->getProducts();
var_export(sizeof($products));
foreach($products as $p){
var_export($p->getName());
}
// Outputs: 'category1' 2
没有产品显示,我收到一个致命错误:
Fatal error: Uncaught exception 'MongoCursorException' with message 'localhost:27017: Can't canonicalize query: BadValue $in needs an array' in /private/var/www/mongo/vendor/doctrine/mongodb/lib/Doctrine/MongoDB/Cursor.php on line 288
如果我查看 MongoDB 日志,查询如下:
assertion 17287 Can't canonicalize query: BadValue $in needs an array ns:shop.Product query:{ $query: { _id: { $in: { 53e3d8d3e2afec2303d63af8: ObjectId('53e3d8d3e2afec2303d63af8'), 53e3d8d3e2afec2303d63af9: ObjectId('53e3d8d3e2afec2303d63af9') } } }, $orderby: [] }
我的意思是,问题似乎很清楚:$in
需要一个数组,但没有给出 JSON 数组。但我不知道如何修复它。我究竟做错了什么?如果您无法回答这个特定问题,您能否为我提供一个带有 Doctrine 的 MongoDB 的工作示例,其中您有这样的多对多关系并且可以通过双方查询数据?
更新:我可能应该提到,我使用哪个版本的学说。我使用了这个设置:
{
"require":{
"doctrine/common":"2.3.*",
"doctrine/dbal":"2.3.*",
"doctrine/orm":"*",
"doctrine/mongodb-odm": "1.0.0-BETA9"
}
}
然后我想更新到可用的最新版本并使用此设置:
{
"require":{
"doctrine/common":"2.4.*",
"doctrine/dbal":"2.3.*",
"doctrine/orm":"*",
"doctrine/mongodb-odm": "dev-master"
}
}
瞧:一切正常...... OMG。但是仍然欢迎对此进行任何解释,我想知道这里发生了什么。非常感谢。