0

私は次のドキュメントを持っています:EmbedManyOrderlinesで注文するEmbedOne製品で注文する

新しいオーダーラインを追加したい既存のオーダーがありますが、既存のオーダーをフェッチしてそれに新しいオーダーラインを追加すると、2つの新しいオーダーラインが追加されます。

私のコントローラーからのコード:

$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がそれを生成するのはなぜですか?

4

1 に答える 1

0

問題が解決しました。OrderlineドキュメントにEmbeddedDocumentアノテーションがありませんでしたが、それを追加すると問題が修正されました。

/**
* @MongoDB\EmbeddedDocument
*/
class Orderline
{
...
于 2013-01-21T11:22:50.363 に答える