我有以下文件: 使用EmbedMany Orderlines订购 使用EmbedOne产品的订单
我有一个现有订单,我想添加一个新的订单行,但是当我获取现有订单并向其添加新订单时,我会添加两个新的订单行。
来自我的控制器的代码:
$dm = $this->get('doctrine_mongodb')->getManager();
// Get the session id for current user
$sessionId = $this->get('session')->getId();
// Fetch an existing order
$order = $dm->getRepository('AcmeDemoBundle:Order')
->findOneBy(array('session_id' => $sessionId));
// Create a new product
$product = new Product();
$product->setTitle('Product title');
// Create an orderline
$orderline = new Orderline();
$orderline->setProduct($product);
$orderline->setQuantity(1);
// Add newly created orderline to the order
$order->addOrderlines($orderline);
$dm->persist($orderline);
$dm->flush();
Doctrine生成的MongoDB查询:
use test_database;
db.Order.find({ "session_id": "ransbtpa63cdbp5vp7fqs2pma5" });
db.Product.insert({ "00000000723450f000000000b20415bf": { "_id": ObjectId("50fc67348e97f4d119000002"), "title": "Product title" } });
db.Orderline.insert({ "000000007234500d00000000b20415bf": { "_id": ObjectId("50fc67348e97f4d119000003"), "product": { "_id": ObjectId("50fc67348e97f4d119000002"), "title": "Product title" }, "quantity": 1 } });
db.Order.update({ "_id": ObjectId("50fc62a18e97f44f1d000002") }, { "$set": { "orderlines.5.product.title": "Product title", "orderlines.5.quantity": 1 } });
db.Order.update({ "_id": ObjectId("50fc62a18e97f44f1d000002") }, { "$pushAll": { "orderlines": [ { "_id": ObjectId("50fc67348e97f4d119000003"), "product": { "_id": ObjectId("50fc67348e97f4d119000002"), "title": "Product title" }, "quantity": 1 } ] } });
从MongoDB订购文档(订单行1和2,由查询添加):
[_id] => MongoId Object (
[$id] => 50fc62a18e97f44f1d000002
)
[orderlines] => Array (
[0] => Array (
[_id] => MongoId Object (
[$id] => 50fc62a18e97f44f1d000001
)
[product] => Array (
[_id] => MongoId Object (
[$id] => 50fc62a18e97f44f1d000000
)
[title] => Product title
)
[quantity] => 1
)
[1] => Array (
[product] => Array (
[title] => Product title
)
[quantity] => 1
)
[2] => Array (
[_id] => MongoId Object (
[$id] => 50fc62d08e97f4f41d000002
)
[product] => Array (
[_id] => MongoId Object (
[$id] => 50fc62d08e97f4f41d000001
)
[title] => Product title
)
[quantity] => 1
)
)
[session_id] => ransbtpa63cdbp5vp7fqs2pma5
我认为问题是由第一个带有$ set的db.Order.insert查询引起的,后跟$ pushAll。 我怎样才能摆脱那个过多的问题?为什么Doctrine会产生它呢?
答案 0 :(得分:0)
问题解决了。我在Orderline文档中缺少EmbeddedDocument注释,并添加了修复问题。
/**
* @MongoDB\EmbeddedDocument
*/
class Orderline
{
...