拥有方到底意味着什么?一些映射示例(一对多,一对一,多对一)的解释是什么?
以下文本摘自Java EE 6文档中对@OneToOne的描述.你可以看到这个概念拥有方在里面.
定义与具有一对一多重性的另一个实体的单值关联.通常不必明确指定关联的目标实体,因为它通常可以从被引用的对象的类型推断出来.如果关系是双向的,则非拥有方必须使用OneToOne批注的mappedBy元素来指定拥有方的关系字段或属性.
在这个关联映射页面上,有一个例子在manytomany部分.但我不明白哪个实体(组或用户)是拥有方.
我也把代码放在这里
<?php
/** @Entity */
class User
{
// ...
/**
* @ManyToMany(targetEntity="Group", inversedBy="users")
* @JoinTable(name="users_groups")
*/
private $groups;
public function __construct() {
$this->groups = new \Doctrine\Common\Collections\ArrayCollection();
}
// ...
}
/** @Entity */
class Group
{
// ...
/**
* @ManyToMany(targetEntity="User", mappedBy="groups")
*/
private $users;
public function __construct() {
$this->users = new \Doctrine\Common\Collections\ArrayCollection();
}
// ...
}
Run Code Online (Sandbox Code Playgroud)
我是否读过这样的注释:用户是映射组,所以组是进行连接管理的实体,因此是拥有方?
另外,我在文档中读到过这个:
For ManyToMany bidirectional relationships either side may be the owning side (the side that defines the @JoinTable and/or …Run Code Online (Sandbox Code Playgroud) 我阅读官方文档和大量的线程,但仍然找不到我的情况的解决方案.我的情况非常基本.我有2个实体:评论和关键字.一条评论可以包含许多关键字,但每个关键字仅适用于一条评论.关键字在关键字表格中不是唯一的.所以我认为这是一对多的关系.表结构简单如下:
关键字
id int(11)
comment_id int(11)
text varchar(30)
Run Code Online (Sandbox Code Playgroud)
评论
id int(11)
text text
Run Code Online (Sandbox Code Playgroud)
这是我如何映射它们:
/**
* @Entity
* @Table(name="comments")
**/
class Comments
{
/** @Id @Column(type="integer") */
private $id;
/** @Column(type="text") */
private $text;
/**
* @OneToMany(targetEntity="keywords", mappedBy="comment_id")
*/
private $keywords;
public function getText(){return $this->text;}
public function getId(){return $this->id;}
public function getKeywords(){return $this->keywords;}
}
/**
* @Entity
* @Table(name="keywords")
*/
class Keywords
{
/** @Id @Column(type="integer") */
private $id;
private $text;
public function getText(){return $this->text;}
public …Run Code Online (Sandbox Code Playgroud) 无法绕过它.我或多或少地从教程中复制了,但是分析器抛出了两个错误:
AppBundle\Entity\Brand关联AppBundle\Entity\Brand#devices是指不存在的拥有方字段AppBundle\Entity\Device#brands.
AppBundle\Entity\Device关联AppBundle\Entity\Device #brade是指不存在的反面字段AppBundle\Entity\Brand#brands.
class Brand {
/**
* @var int
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
...
/**
* @ORM\OneToMany(targetEntity="Device", mappedBy="brands")
*/
private $devices;
}
Run Code Online (Sandbox Code Playgroud)
和
class Device {
/**
* @var int
* @ORM\Id
* @ORM\Column(name="id", type="integer")
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
...
/**
* @ORM\ManyToOne(targetEntity="Brand", inversedBy="devices")
* @ORM\JoinColumn(name="brand_id", referencedColumnName="id", nullable=true)
*/
private $brand;
}
Run Code Online (Sandbox Code Playgroud) 我的实体之间有多对多的关系:PurchaseOrder和Supplier.当我想Supplier在我的Symfony项目中添加订单时,我总是会收到以下错误消息:
属性"供应商"在"Acme\AppBundle\Entity\PurchaseOrder"类中不公开.也许你应该创建方法"setSuppliers()"?
当我setSuppliers()在PurchaseOrder实体中自己创建一个函数时:
public function setSuppliers(\Acme\AppBundle\Entity\Supplier $suppliers )
{
$this->suppliers = $suppliers;
return $this;
}
Run Code Online (Sandbox Code Playgroud)
我收到此错误消息:
可捕获致命错误:传递给Doctrine\Common\Collections\ArrayCollection :: __ construct()的参数1必须是类型数组,给定对象,在/ var/www/symfony/vendor/doctrine/orm/lib/Doctrine/ORM中调用/UnitOfWork.php在第519行并在/var/www/symfony/vendor/doctrine/collections/lib/Doctrine/Common/Collections/ArrayCollection.php第47行中定义
有任何想法吗?
/**
* @Route("order/{id}/supplieradd", name="order_supplieradd")
* @Secure(roles="ROLE_ADMIN")
*/
public function newSupplierAction(Request $request, $id)
{
$purchaseOrder = $this->getDoctrine()
->getRepository('AcmeAppBundle:PurchaseOrder')
->find($id);
if (!$purchaseOrder) {
throw $this->createNotFoundException(
'No order found for id '.$id
);
}
$form = $this->createForm(new AddSupplierType(), $purchaseOrder);
// process the form on POST
if ($request->isMethod('POST')) {
$form->bind($request);
if ($form->isValid()) …Run Code Online (Sandbox Code Playgroud)