我必须表
preset_item id
和
preset_item_element preset_item_id -> 对 prese_item element_type element_id 的引用
在 PresetItemElement 实体中有:
/**
* @var \GGG\ManagerBundle\Entity\PresetItem
*
* @ORM\ManyToOne(targetEntity="GGG\ManagerBundle\Entity\PresetItem", inversedBy="elements")
* @ORM\JoinColumns({
* @ORM\JoinColumn(name="preset_item_id", referencedColumnName="id")
* })
*/
private $presetItem;
并在 PresetItem
/**
* @ORM\OneToMany(targetEntity="PresetItemElement", mappedBy="presetItem")
*/
private $elements;
public function getElements()
{
return $this->elements;
}
并且预设项具有自定义存储库类:
/**
* PresetItem
*
* @ORM\Table(name="preset_item")
* @ORM\Entity(repositoryClass="GGG\ManagerBundle\Entity\PresetItemRepository")
*/
class PresetItem
它看起来像这样:
<?php
namespace GGG\ManagerBundle\Entity;
use Doctrine\ORM\EntityRepository;
use Doctrine\DBAL\LockMode;
class PresetItemElementRepository extends EntityRepository
{
public function findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null) {
return $this->decorate(parent::findBy($criteria, $orderBy, $limit, $offset));
}
public function find($id, $lockMode = LockMode::NONE, $lockVersion = null) {
return $this->decorateElement(parent::find($id, $lockMode, $lockVersion));
}
public function findOneBy(array $criteria, array $orderBy = null) {
return $this->decorateElement(parent::findOneBy($criteria, $orderBy));
}
private function decorateElement($element) {
$object = $this->getEntityManager()
->getRepository(
'GGGManagerBundle:'.$element
->getPresetItemElementType()
->getRepresentationObject()
)->findOneBy(array('id' => $element->getElementId()));
$element->setObject($object);
}
private function decorate($elements) {
foreach($elements as $element) {
$this->decorateElement($element);
}
return $elements;
}
}
所以我用一些额外的数据来装饰每个 PresetItemElement
当我得到单个 PresetItemElement 对象但是当我得到 PresetItem 并尝试迭代 getElements()
$entity = $em->getRepository('GGGManagerBundle:PresetItem')->find($id);
foreach($entity->getElements() as $a) {
var_dump($a->getObject());
}
我这里有空,看起来自定义 PresetItemElementRepository 没有执行
我做错了什么?