0

我正在做一个项目,但我一直看到这个映射问题:

关联 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 指的是不存在的拥有方字段 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;
    }

}
4

1 回答 1

1

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

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

您拥有的 inversedBy 基本上是在告诉学说,您公司的 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

于 2014-02-15T22:35:22.707 回答