我的ManyToMany
关系定义如下:
OrderHasMedia.php
/**
* @ORM\Entity
* @ORM\Table(name="order_has_media__media")
*/
class OrderHasMedia {
/**
* @ORM\Id
* @ORM\ManyToOne(targetEntity="PL\OrderBundle\Entity\Order", inversedBy="order_document", cascade={"all"})
*/
protected $order;
/**
* @ORM\Id
* @ORM\ManyToOne(targetEntity="Application\Sonata\MediaBundle\Entity\Media", inversedBy="order_document", cascade={"all"})
*/
protected $media;
}
Order.php
/**
* @ORM\Entity
* @ORM\Table(name="tb_order")
* @Gedmo\Loggable
*/
class Order {
/**
* @ORM\OneToMany(targetEntity="PL\OrderBundle\Entity\OrderHasMedia", mappedBy="order" , cascade={"all"})
*/
protected $order_document;
}
Media.php
class Media extends BaseMedia {
/**
* @var Collection
* @ORM\OneToMany(targetEntity="PL\OrderBundle\Entity\OrderHasMedia", mappedBy="media", cascade={"all"})
*
*/
protected $order_document;
}
如果我尝试添加新订单,我会收到此错误:
ContextErrorException:注意:未定义索引: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' => 'PL\OrderBundle\Entity\Order', 'fetch' => '2', 'type' => '2', 'mappedBy' => null, 'isOwningSide' => true, 'sourceEntity' => 'PL\OrderBundle\Entity\OrderHasMedia', '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
错误在哪里?我的映射有什么问题?
答案 0 :(得分:7)
经过几个小时的头痛我终于找到了我的错误所在,我希望分享以防其他人遇到同样的问题。解决方案是将@ORM\JoinColumn()
添加到注释中,因此我的代码现在只适用于OrderHasMedia
实体,如下所示:
/**
* @var Order
* @ORM\Id
* @ORM\ManyToOne(targetEntity="PL\OrderBundle\Entity\Order", inversedBy="order_document", cascade={"all"})
* @ORM\JoinColumn(name="order_no_order", referencedColumnName="no_order")
*/
protected $order;
/**
* @var Media
* @ORM\Id
* @ORM\ManyToOne(targetEntity="Application\Sonata\MediaBundle\Entity\Media", inversedBy="order_document", cascade={"all"})
* @ORM\JoinColumn(name="media__media_id", referencedColumnName="id")
*/
protected $media;
解决了这个问题。希望这会有助于其他人