我的文档是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: []
}
}
]
答案 0 :(得分:0)
使用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;
}