关联原因的定义实体无效

时间:2014-02-15 19:11:12

标签: symfony doctrine-orm symfony-2.4

我正在开展一个项目但是我一直在看这个映射问题:

  

关联PL \ OrderBundle \ Entity \ Order#company指的是   反面字段PL \ CompanyBundle \ Entity \ Company #id不是   定义为关联。

     

关联PL \ OrderBundle \ Entity \ Order#company指的是反面字段   PL \ CompanyBundle \ Entity \ Company #id不存在。

     

关联PL \ OrderBundle \ Entity \ Order #medias是指拥有   side field Application \ Sonata \ MediaBundle \ Entity \ Media#orders   不存在。

对于我阅读和阅读Doctrine Docs的不仅仅是我不知道如何修复它们,还有什么问题?任何帮助?以下是相关实体:

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;
    }

}

1 个答案:

答案 0 :(得分:1)

对于初学者,当使用双向ManyToOnes和OneToManys时,您必须具有inversedBy和匹配的mappedBy。在你的例子中:

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

反转你有基本上是在告诉你的公司的id字段(这是你的主键)是你的订单实体的外键,这是不正确的。你需要订购:

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

在公司:

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

按照文档: http://docs.doctrine-project.org/en/2.0.x/reference/association-mapping.html