我正在做一个项目,但我一直看到这个映射问题:
关联 PL\OrderBundle\Entity\Order#company 是指反转边字段 PL\公司捆绑\实体\公司#ID 不是定义为关联。
关联 PL\OrderBundle\Entity\Order#company 引用反向侧字段PL\CompanyBundle\Entity\Company#id 不存在。
关联 PL\OrderBundle\Entity\Order#medias 是指拥有侧字段 应用程序\奏鸣曲\媒体束\实体\媒体#订单不存在。
比我阅读和阅读教义文档更多,我不知道如何解决它们,有什么问题? 有什么帮助吗?以下是相关实体:
PLOrderBundleEntityOrder.php
<?php
namespace PLOrderBundleEntity;
use DoctrineORMMapping as ORM;
/**
* @ORMEntity
* @ORMTable(name="tb_order")
*/
class Order {
/**
* @ORMId
* @ORMColumn(type="string", length=15, unique=true, nullable=false)
*/
protected $no_order;
/**
* @ORMManyToOne(targetEntity="PLCompanyBundleEntityCompany", inversedBy="id")
*/
protected $company;
/**
* @ORMColumn(type="string", length=15, unique=true)
*/
protected $business_case;
/**
* @ORMColumn(type="integer", length=1)
*/
protected $charge_status;
/**
* @ORMColumn(type="datetime")
*/
protected $eta;
/**
* @ORMColumn(type="datetime")
*/
protected $etd;
/**
* @ORMColumn(type="integer", length=1)
*/
protected $transport_media;
/**
* @ORMColumn(type="integer", length=1)
*/
protected $incoterm;
/**
* @ORMColumn(type="string", length=250)
*/
protected $comments;
/**
* @ORMManyToMany(targetEntity="ApplicationSonataMediaBundleEntityMedia", mappedBy="orders")
*/
protected $medias;
public function __construct() {
$this->medias = new DoctrineCommonCollectionsArrayCollection();
}
public function setNoOrder($no_order) {
$this->no_order = $no_order;
}
public function getNoOrder() {
return $this->no_order;
}
public function setCompany(PLCompanyBundleEntityCompany $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(ApplicationSonataMediaBundleEntityMedia $media) {
$this->medias[] = $media;
}
public function addMedia(ApplicationSonataMediaBundleEntityMedia $media) {
$this->medias[] = $media;
}
public function getMedias() {
return $this->medias;
}
}
PLCompanyBundleEntityCompany.php
<?php
namespace PLCompanyBundleEntity;
use DoctrineORMMapping as ORM;
use GedmoMappingAnnotation as Gedmo;
/**
* @ORMEntity
* @ORMTable(name="company")
*/
class Company {
/**
* @ORMId
* @ORMColumn(type="integer")
* @ORMGeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* @ORMColumn(type="string", length=30, unique=true, nullable=false)
*/
protected $name;
/**
* @GedmoTimestampable(on="create")
* @ORMColumn(name="register_date", type="datetime")
*/
protected $created_on;
/**
* @ORMColumn(type="string", length=45)
*/
protected $country;
/**
* @ORMManyToMany(targetEntity="ApplicationSonataUserBundleEntityUser", mappedBy="companies")
*/
protected $users;
public function __construct() {
$this->users = new DoctrineCommonCollectionsArrayCollection();
}
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;
}
}
ApplicationSonataMediaBundleEntityMedia.php
<?php
namespace ApplicationSonataMediaBundleEntity;
use SonataMediaBundleEntityBaseMedia as BaseMedia;
use DoctrineORMMapping as ORM;
class Media extends BaseMedia {
/**
* @var integer $id
*/
protected $id;
/**
* @ORMManyToMany(targetEntity="PLOrderBundleEntityOrder", inversedBy="medias")
* @ORMJoinTable(name="order_has_media__media",
* joinColumns={@ORMJoinColumn(name="media__media_id", referencedColumnName="id")},
* inverseJoinColumns={@ORMJoinColumn(name="order_no_order", referencedColumnName="no_order")}
* )
*/
protected $orders;
public function __construct() {
$this->orders = new DoctrineCommonCollectionsArrayCollection();
}
/**
* Get id
*
* @return integer $id
*/
public function getId() {
return $this->id;
}
public function setOrders(PLOrderBundleEntityOrder $order) {
$this->orders[] = $order;
}
public function getOrders() {
return $this->orders;
}
}
对于初学者来说,当使用双向 ManyToOnes 和 OneToManys 时,您必须具有 inversedBy 和匹配的 mappedBy。 在您的示例中:
/**
* @ORMManyToOne(targetEntity="PLCompanyBundleEntityCompany", inversedBy="id")
*/
protected $company;
你拥有的反转基本上是告诉教义,你公司的id字段(这是你的主键)是你的订单实体的外键,这是不正确的。 您需要有以下订单:
/**
* @ORMManyToOne(targetEntity="PLCompanyBundleEntityCompany", inversedBy="orders")
*/
protected $company;
在公司:
/**
* @ORMOneToMany(targetEntity="PLOrderBundleEntityOrder", mappedBy="company")
*/
private $orders;
只需按照文档操作:http://docs.doctrine-project.org/en/2.0.x/reference/association-mapping.html