代码之家  ›  专栏  ›  技术社区  ›  JorgeeFG

Doctrine MongoDB如何将嵌入的文档作为其实际类而不是数组

  •  0
  • JorgeeFG  · 技术社区  · 7 年前

    我的文件是 ItemsList ,有一个 Items 嵌入文档。

    问题是Doctrine Mongo不是将嵌入的文档映射为项对象,而是将其映射为数组。这是对的吗?如何更新 Item

    <?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: []
        }
      }
    ]
    
    1 回复  |  直到 7 年前
        1
  •  0
  •   JorgeeFG    7 年前

    在使用 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;
        }
    
    推荐文章