我的文件是
ItemsList
,有一个
Items
嵌入文档。
问题是Doctrine Mongo不是将嵌入的文档映射为项对象,而是将其映射为数组。这是对的吗?如何更新
Item
<?php
namespace App\Document;
use Doctrine\ODM\MongoDB\Mapping\Annotations as MongoDB;
class ItemsList implements \JsonSerializable {
private $id;
private $items;
public function getId() {
return $this->id;
}
public function setId($id): void {
$this->id = $id;
}
public function getItems() {
return $this->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;
class Item implements \JsonSerializable {
private $id;
private $imgPath;
private $description;
public function getId() {
return $this->id;
}
public function setId($id): void {
$this->id = $id;
}
public function getImgPath() {
return $this->imgPath;
}
public function setImgPath($imgPath): void {
$this->imgPath = $imgPath;
}
public function getDescription() {
return $this->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 {
-id: "5b63016b3faeb7e511d6d064"
-items: PersistentCollection {
-snapshot: []
-owner: ItemsList {
-mapping: array:21 [â¶]
-isDirty: false
-initialized: false
-coll: ArrayCollection {
-dm: DocumentManager {
-uow: UnitOfWork {
-mongoData: array:3 [â¼
0 => array:3 [â¼
"_id" => MongoId {
"img_path" => "/tmp/1.jpg"
"description" => "Una descripcion"
]
1 => array:3 [â¶]
2 => array:3 [â¶]
]
-hints: []
}
}
]