Question

I'm working on a project but all the time I'm seeing this mappings problems:

The association PL\OrderBundle\Entity\Order#company refers to the inverse side field PL\CompanyBundle\Entity\Company#id which is not defined as association.

The association PL\OrderBundle\Entity\Order#company refers to the inverse side field PL\CompanyBundle\Entity\Company#id which does not exist.

The association PL\OrderBundle\Entity\Order#medias refers to the owning side field Application\Sonata\MediaBundle\Entity\Media#orders which does not exist.

For more than I read and read Doctrine Docs I don't know how to fix them, what is wrong? any help? Here are the related entities:

PL\OrderBundle\Entity\Order.php

<?php

namespace PL\OrderBundle\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * @ORM\Entity
 * @ORM\Table(name="tb_order")
 */
class Order {

    /**
     * @ORM\Id
     * @ORM\Column(type="string", length=15, unique=true, nullable=false)
     */
    protected $no_order;

    /**
     * @ORM\ManyToOne(targetEntity="PL\CompanyBundle\Entity\Company", inversedBy="id")
     */
    protected $company;

    /**
     * @ORM\Column(type="string", length=15, unique=true)
     */
    protected $business_case;

    /**
     * @ORM\Column(type="integer", length=1)
     */
    protected $charge_status;

    /**
     * @ORM\Column(type="datetime")
     */
    protected $eta;

    /**
     * @ORM\Column(type="datetime")
     */
    protected $etd;

    /**
     * @ORM\Column(type="integer", length=1)
     */
    protected $transport_media;

    /**
     * @ORM\Column(type="integer", length=1)
     */
    protected $incoterm;

    /**
     * @ORM\Column(type="string", length=250)
     */
    protected $comments;

    /**
     * @ORM\ManyToMany(targetEntity="Application\Sonata\MediaBundle\Entity\Media", mappedBy="orders")
     */
    protected $medias;

    public function __construct() {
        $this->medias = new \Doctrine\Common\Collections\ArrayCollection();
    }

    public function setNoOrder($no_order) {
        $this->no_order = $no_order;
    }

    public function getNoOrder() {
        return $this->no_order;
    }

    public function setCompany(\PL\CompanyBundle\Entity\Company $company) {
        $this->company = $company;
    }

    public function getCompany() {
        return $this->company;
    }

    public function setBusinessCase($business_case) {
        $this->business_case = $business_case;
    }

    public function getBusinessCase() {
        return $this->business_case;
    }

    public function setChargeStatus($charge_status) {
        $this->charge_status = $charge_status;
    }

    public function getChargeStatus() {
        return $this->charge_status;
    }

    public function setETA($eta) {
        $this->eta = $eta;
    }

    public function getETA() {
        return $this->eta;
    }

    public function setETD($etd) {
        $this->etd = $etd;
    }

    public function getETD() {
        return $this->etd;
    }

    public function setTransportMedia($transport_media) {
        $this->transport_media = $transport_media;
    }

    public function getTransportMedia() {
        return $this->transport_media;
    }
    
    public function setIncoterm($incoterm) {
        $this->incoterm = $incoterm;
    }
    
    public function getIncoterm() {
        return $this->incoterm;
    }

    public function setComments($comments) {
        $this->comments = $comments;
    }

    public function getComments() {
        return $this->comments;
    }
    
    public function setMedias(\Application\Sonata\MediaBundle\Entity\Media $media) {
        $this->medias[] = $media;
    }
    
    public function addMedia(\Application\Sonata\MediaBundle\Entity\Media $media) {
        $this->medias[] = $media;
    }

    public function getMedias() {
        return $this->medias;
    }

}

\PL\CompanyBundle\Entity\Company.php

<?php

namespace PL\CompanyBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use Gedmo\Mapping\Annotation as Gedmo;

/**
 * @ORM\Entity
 * @ORM\Table(name="company")
 */
class Company {

    /**
     * @ORM\Id
     * @ORM\Column(type="integer")
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $id;

    /**
     * @ORM\Column(type="string", length=30, unique=true, nullable=false)
     */
    protected $name;

    /**
     * @Gedmo\Timestampable(on="create")
     * @ORM\Column(name="register_date", type="datetime")
     */
    protected $created_on;

    /**
     * @ORM\Column(type="string", length=45)
     */
    protected $country;

    /**
     * @ORM\ManyToMany(targetEntity="Application\Sonata\UserBundle\Entity\User", mappedBy="companies")
     */
    protected $users;
    
    public function __construct() {
        $this->users = new \Doctrine\Common\Collections\ArrayCollection();
    }
    
    public function getId() {
        return $this->id;
    }

    public function setName($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }

    public function setCountry($country) {
        $this->country = $country;
    }

    public function getCountry() {
        return $this->country;
    }

    public function setCreatedOn($created_on) {
        $this->created_on = $created_on;
    }

    public function getCreatedOn() {
        return $this->created_on;
    }

    public function __toString() {
        return $this->name;
    }

}

\Application\Sonata\MediaBundle\Entity\Media.php

<?php

namespace Application\Sonata\MediaBundle\Entity;

use Sonata\MediaBundle\Entity\BaseMedia as BaseMedia;
use Doctrine\ORM\Mapping as ORM;

class Media extends BaseMedia {

    /**
     * @var integer $id
     */
    protected $id;

    /**
     * @ORM\ManyToMany(targetEntity="PL\OrderBundle\Entity\Order", inversedBy="medias")
     * @ORM\JoinTable(name="order_has_media__media",
     *      joinColumns={@ORM\JoinColumn(name="media__media_id", referencedColumnName="id")},
     *      inverseJoinColumns={@ORM\JoinColumn(name="order_no_order", referencedColumnName="no_order")}
     * )
     */
    protected $orders;

    public function __construct() {
        $this->orders = new \Doctrine\Common\Collections\ArrayCollection();
    }

    /**
     * Get id
     *
     * @return integer $id
     */
    public function getId() {
        return $this->id;
    }

    public function setOrders(\PL\OrderBundle\Entity\Order $order) {
        $this->orders[] = $order;
    }

    public function getOrders() {
        return $this->orders;
    }

}
Was it helpful?

Solution

for starters, When using bi-directional ManyToOnes and OneToManys you must have inversedBy and a matching mappedBy. in your example:

/**
 * @ORM\ManyToOne(targetEntity="PL\CompanyBundle\Entity\Company", inversedBy="id")
 */
protected $company;

the inversedBy you have is basically telling doctrine that the id field (which is your primary key) of your company is a foreign key to your order entity, which is incorrect. you need to have in orders:

/**
 * @ORM\ManyToOne(targetEntity="PL\CompanyBundle\Entity\Company", inversedBy="orders")
 */
protected $company;

in company:

/**
 * @ORM\OneToMany(targetEntity="PL\OrderBundle\Entity\Order", mappedBy="company")
 */
private $orders;

just follow the docs: http://docs.doctrine-project.org/en/2.0.x/reference/association-mapping.html

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top