我的文档是一个ItemsList
,有一个Items
嵌入的文档。
问题是 Doctrine Mongo 没有将嵌入文档映射为 Item 对象,而是映射为数组。这个对吗?我将如何Item
以 OOP 方式更新?
项目列表.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()),
];
}
}
项目.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: []
}
}
]