3
votes

I have a ManyToMany relationship defined as follow:

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;

}

If I try to add a new order I get this error:

ContextErrorException: Notice: Undefined index: id in /var/www/html/apps/portal_de_logistica/vendor/doctrine/orm/lib/Doctrine/ORM/Persisters/BasicEntityPersister.php line 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

Where is the error? What's wrong in my mapping?

1
are you missing $id in Order entity ?guy_fawkes
@abbiya No, it is defined just it does not call $id instead it is called $no_order. Here is the definition I have ` /** * @ORM\Id * @ORM\Column(type="string", length=15, unique=true, nullable=false) */ protected $no_order;`ReynierPM

1 Answers

7
votes

After hours of headache I finally found where my mistake was, and I want to share in case anyone else has the same problem. The solution was to add @ORM\JoinColumn() to annotations, so my code now, and just for OrderHasMedia entity is as follows:

/**
 * @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;

That solves the problem. Hope this will help others