我有一个ManyToMany
关系,定义如下:
OrderHasMedia.php
/**
* @ORMEntity
* @ORMTable(name="order_has_media__media")
*/
class OrderHasMedia {
/**
* @ORMId
* @ORMManyToOne(targetEntity="PLOrderBundleEntityOrder", inversedBy="order_document", cascade={"all"})
*/
protected $order;
/**
* @ORMId
* @ORMManyToOne(targetEntity="ApplicationSonataMediaBundleEntityMedia", inversedBy="order_document", cascade={"all"})
*/
protected $media;
}
Order.php
/**
* @ORMEntity
* @ORMTable(name="tb_order")
* @GedmoLoggable
*/
class Order {
/**
* @ORMOneToMany(targetEntity="PLOrderBundleEntityOrderHasMedia", mappedBy="order" , cascade={"all"})
*/
protected $order_document;
}
Media.php
class Media extends BaseMedia {
/**
* @var Collection
* @ORMOneToMany(targetEntity="PLOrderBundleEntityOrderHasMedia", mappedBy="media", cascade={"all"})
*
*/
protected $order_document;
}
如果我尝试添加新订单,则会收到此错误:
上下文错误异常: 注意: 未定义的索引: id in /var/www/html/apps/portal_de_logistica/vendor/doctrine/orm/lib/Doctrine/ORM/Persisters/BasicEntityPersister.php 685路
in /var/www/html/apps/portal_de_logistica/vendor/doctrine/orm/lib/Doctrine/ORM/Persisters/BasicEntityPersister.php line 685
at ErrorHandler->handle('8', 'Undefined index: id', '/var/www/html/apps/portal_de_logistica/vendor/doctrine/orm/lib/Doctrine/ORM/Persisters/BasicEntityPersister.php', '685', array('entity' => object(OrderHasMedia), 'result' => array(), 'uow' => object(UnitOfWork), 'versioned' => null, 'field' => 'order', 'change' => array(null, object(Order)), 'newVal' => object(Order), 'assoc' => array('fieldName' => 'order', 'id' => true, 'joinColumns' => array(array('name' => 'order_id', 'referencedColumnName' => 'id')), 'cascade' => array('remove', 'persist', 'refresh', 'merge', 'detach'), 'inversedBy' => 'order_document', 'targetEntity' => 'PLOrderBundleEntityOrder', 'fetch' => '2', 'type' => '2', 'mappedBy' => null, 'isOwningSide' => true, 'sourceEntity' => 'PLOrderBundleEntityOrderHasMedia', 'isCascadeRemove' => true, 'isCascadePersist' => true, 'isCascadeRefresh' => true, 'isCascadeMerge' => true, 'isCascadeDetach' => true, 'sourceToTargetKeyColumns' => array('order_id' => 'id'), 'joinColumnFieldNames' => array('order_id' => 'order_id'), 'targetToSourceKeyColumns' => array('id' => 'order_id'), 'orphanRemoval' => false), 'oid' => '000000005c17f96200007f239e737d4c', 'newValId' => array('no_order' => '3424'), 'targetClass' => object(ClassMetadata), 'owningTable' => 'order_has_media__media', 'joinColumn' => array('name' => 'order_id', 'referencedColumnName' => 'id'), 'sourceColumn' => 'order_id', 'targetColumn' => 'id', 'quotedColumn' => 'order_id')) in /var/www/html/apps/portal_de_logistica/vendor/doctrine/orm/lib/Doctrine/ORM/Persisters/BasicEntityPersister.php line 685
错误在哪里?我的映射中出了什么问题?
经过几个小时的头痛,我终于找到了我的错误所在,我想分享以防其他人有同样的问题。解决方案是将@ORMJoinColumn()
添加到注释中,所以我现在的代码,仅用于OrderHasMedia
实体如下:
/**
* @var Order
* @ORMId
* @ORMManyToOne(targetEntity="PLOrderBundleEntityOrder", inversedBy="order_document", cascade={"all"})
* @ORMJoinColumn(name="order_no_order", referencedColumnName="no_order")
*/
protected $order;
/**
* @var Media
* @ORMId
* @ORMManyToOne(targetEntity="ApplicationSonataMediaBundleEntityMedia", inversedBy="order_document", cascade={"all"})
* @ORMJoinColumn(name="media__media_id", referencedColumnName="id")
*/
protected $media;
这就解决了问题。希望这对其他人有帮助