繁体   English   中英

Doctrine MongoDB如何获取PersistentArray中的嵌入式文档作为其实际类而不是Array

[英]Doctrine MongoDB how to get Embedded Document in PersistentArray as its actual class instead of Array

我的文档是一个ItemsList ,其中包含一个Items嵌入式文档。

问题在于,Doctrine Mongo并未将嵌入式文档映射为Item对象,而是映射为数组。 这个对吗? 如何以OOP方式更新Item

ItemsList.php

<?php

namespace App\Document;

use Doctrine\ODM\MongoDB\Mapping\Annotations as MongoDB;

/**
 * @MongoDB\Document(collection="itemslists")
 */
class ItemsList implements \JsonSerializable {

    /**
     * @MongoDB\Id
     */
    private $id;

    /**
     * @MongoDB\EmbedMany(targetDocument="Item")
     */
    private $items;

    /**
     * @return mixed
     */
    public function getId() {
        return $this->id;
    }

    /**
     * @param mixed $id
     */
    public function setId($id): void {
        $this->id = $id;
    }

    /**
     * @return mixed
     */
    public function getItems() {
        return $this->items;
    }

    /**
     * @param mixed $items
     */
    public function setItems($items): void {
        $this->items = $items;
    }

    public function jsonSerialize() {
        return [
            'id' => $this->getId(),
            'items' => json_encode($this->getItems()),
        ];
    }

}

Item.php

<?php

namespace App\Document;

use Doctrine\ODM\MongoDB\Mapping\Annotations as MongoDB;

/**
 * @MongoDB\EmbeddedDocument
 */
class Item implements \JsonSerializable {

    /**
     * @MongoDB\Id
     */
    private $id;

    /**
     * @MongoDB\Field(type="string")
     */
    private $imgPath;

    /**
     * @MongoDB\Field(type="string")
     */
    private $description;

    /**
     * @return mixed
     */
    public function getId() {
        return $this->id;
    }

    /**
     * @param mixed $id
     */
    public function setId($id): void {
        $this->id = $id;
    }

    /**
     * @return mixed
     */
    public function getImgPath() {
        return $this->imgPath;
    }

    /**
     * @param mixed $imgPath
     */
    public function setImgPath($imgPath): void {
        $this->imgPath = $imgPath;
    }

    /**
     * @return mixed
     */
    public function getDescription() {
        return $this->description;
    }

    /**
     * @param mixed $description
     */
    public function setDescription($description): void {
        $this->description = $description;
    }

    public function jsonSerialize() {
        return [
            'id' => $this->getId(),
            'img_path' => $this->getImgPath(),
            'description' => $this->getDescription(),
        ];
    }
}

$itemsList = $itemsListRepository->findBy([], null, 1);转储

ItemsListController.php on line 23:
array:1 [▼
  0 => ItemsList {#579 ▼
    -id: "5b63016b3faeb7e511d6d064"
    -items: PersistentCollection {#584 ▼
      -snapshot: []
      -owner: ItemsList {#579}
      -mapping: array:21 [▶]
      -isDirty: false
      -initialized: false
      -coll: ArrayCollection {#583 ▶}
      -dm: DocumentManager {#483 …13}
      -uow: UnitOfWork {#486 ▶}
      -mongoData: array:3 [▼
        0 => array:3 [▼
          "_id" => MongoId {#572 ▶}
          "img_path" => "/tmp/1.jpg"
          "description" => "Una descripcion"
        ]
        1 => array:3 [▶]
        2 => array:3 [▶]
      ]
      -hints: []
    }
  }
]

使用PersistentCollection的方法时似乎将它们转换

public function getFirstList() {
        /** @var Repository $itemsListRepository */
        $itemsListRepository = $this->get('doctrine_mongodb')->getRepository('App:ItemsList');
        /** @var ItemsList $itemsList */
        $itemsLists = $itemsListRepository->findBy([], null, 1);
        $itemsList = $itemsLists[0];
        /** @var PersistentCollection $items */
        $items = $itemsList->getItems();
        dump($items->first(), json_encode($itemsList));
        exit;
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM