i具有SINGLE_TABLE
的InheritanceType
,类型基于FK,是针对另一个实体的。
当我获得附件实体的详细页面时,它会加载产品实体。但是,当我尝试通过项目附件访问产品实体时,产品为null。
视图的控制器使用'$ entity = $ em-> getRepository('projectTestBundle:item') -> find($ id);''$ entity = $ em-> getRepository('ProjectTestBundle:attactment') -> find($ id);'
下面转储和文件。
附件上的实体转储详细信息页
ImageAttachment {#361 ▼
#product: Product {#390 ▼
+__isInitialized__: false
#id: "IMAGE"
#name: null
…4
}
-id: 1
#name: "Test"
#item: Item {#372 ▶}
}
项目详细信息页面上的实体转储通过集合
ImageAttachment {#367 ▼
#product: null
-id: 1
#name: "Test"
#item: Item {#323 ▶}
}
attachment.php
namespace projectTestBundleEntity;
use DoctrineORMMapping as ORM;
/**
* Attachment
*
* @ORMTable(name="attachments")
* @ORMEntity
* @ORMInheritanceType("SINGLE_TABLE")
* @ORMDiscriminatorColumn(name="product", type="string")
* @ORMDiscriminatorMap({"IMAGE" = "projectTestBundleEntityImageAttachment", "TEXT" = "projectTestBundleEntityTextAttachment"})
* @ORMEntity(repositoryClass="projectTestBundleEntityAttachmentRepository")
*/
class Attachment
{
/**
* @var integer
*
* @ORMColumn(name="id", type="integer")
* @ORMId
* @ORMGeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @ORMColumn(type="string")
*/
protected $name;
/**
* @ORMManyToOne(
* targetEntity="Item",
* inversedBy="attachments"
* )
* @ORMJoinColumn(
* name="item_id",
* referencedColumnName="id",
* nullable=false
* )
*/
protected $item;
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set name
*
* @param string $name
* @return Attachment
*/
public function setName($name)
{
$this->name = $name;
return $this;
}
/**
* Get name
*
* @return string
*/
public function getName()
{
return $this->name;
}
/**
* Set item
*
* @param projectTestBundleEntityItem $item
* @return Attachment
*/
public function setItem(projectTestBundleEntityItem $item)
{
$this->item = $item;
return $this;
}
/**
* Get item
*
* @return projectTestBundleEntityItem
*/
public function getItem()
{
return $this->item;
}
}
?>
ImageAttachment.php(TextAttachemnT是同一类,目前为简单的文本更改。)
namespace projectTestBundleEntity;
use DoctrineORMMapping as ORM;
/**
* Attachment
*
* @ORMEntity
*/
class ImageAttachment extends Attachment
{
/**
* @ORMManyToOne(targetEntity="Product",cascade={"persist"},fetch="EXTRA_LAZY")
* @ORMJoinColumn(name="product", referencedColumnName="product", nullable=true)
*/
protected $product;
/**
* Set product
*
* @param projectTestBundleEntityProduct $product
* @return ImageAttachment
*/
public function setProduct(projectTestBundleEntityProduct $product = null)
{
$this->product = $product;
return $this;
}
/**
* Get product
*
* @return projectTestBundleEntityProduct
*/
public function getProduct()
{
return $this->product;
}
/**
* Set item
*
* @param projectTestBundleEntityItem $item
* @return ImageAttachment
*/
public function setItem(projectTestBundleEntityItem $item)
{
$this->item = $item;
return $this;
}
/**
* Get item
*
* @return projectTestBundleEntityItem
*/
public function getItem()
{
return $this->item;
}
}
item.php
namespace projectTestBundleEntity;
use DoctrineORMMapping as ORM;
/**
* Item
*
* @ORMTable(name="items")
* @ORMEntity
* @ORMEntity(repositoryClass="projectTestBundleEntityItemRepository")
*/
class Item
{
/**
* @var integer
*
* @ORMColumn(name="id", type="integer")
* @ORMId
* @ORMGeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @ORMColumn(type="string")
*/
protected $name;
/**
* @ORMOneToMany(
* targetEntity="Attachment",
* mappedBy="item",
* cascade={"persist", "remove"},
* orphanRemoval=true
* )
*/
protected $attachments;
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* Constructor
*/
public function __construct()
{
$this->attachments = new DoctrineCommonCollectionsArrayCollection();
}
/**
* Set name
*
* @param string $name
* @return Item
*/
public function setName($name)
{
$this->name = $name;
return $this;
}
/**
* Get name
*
* @return string
*/
public function getName()
{
return $this->name;
}
/**
* Add attachments
*
* @param projectTestBundleEntityAttachment $attachments
* @return Item
*/
public function addAttachment(projectTestBundleEntityAttachment $attachments)
{
$this->attachments[] = $attachments;
$attachments->setItem($this);
return $this;
}
/**
* Remove attachments
*
* @param projectTestBundleEntityAttachment $attachments
*/
public function removeAttachment(projectTestBundleEntityAttachment $attachments)
{
$this->attachments->removeElement($attachments);
}
/**
* Get attachments
*
* @return DoctrineCommonCollectionsCollection
*/
public function getAttachments()
{
return $this->attachments;
}
}
我认为您的问题是您的产品实体在附件实体中加载了懒惰。为了避免这种情况,您应该使用自定义方法查找您的实体,您离开的地方加入了您的产品:
:/* Controller*/
$entity = $em->getRepository('projectTestBundle:Item')->customFind($id);
/* ItemRepository */
$em = $this->getEntityManager();
$qb = $em->createQueryBuilder();
$qb
->select('Item','attachment')
->from('projetTestBundleEntityItem','Item')
->leftJoin('Project.attachment','attachment')
;
$q = $qb->getQuery();
return $q->getResult();
希望这有帮助