Php ZF2/Doctrine不允许我保存收藏
必须处理一个名为Php ZF2/Doctrine不允许我保存收藏,php,doctrine-orm,zend-framework2,Php,Doctrine Orm,Zend Framework2,必须处理一个名为集合的实体,该实体有一个数组要另存为不同的实体 这是我在控制器中用于测试实体的内容: // setup items in a controller $i1 = new Item(); $i1->setInfo('test2'); $i2 = new Item(); $i2->setInfo('example'); // create the collection $pc = new Collection(); $pc->setId(1); // set som
集合
的实体,该实体有一个数组要另存为不同的实体
这是我在控制器中用于测试实体的内容:
// setup items in a controller
$i1 = new Item();
$i1->setInfo('test2');
$i2 = new Item();
$i2->setInfo('example');
// create the collection
$pc = new Collection();
$pc->setId(1);
// set some vars / members
$pc->setInfo('test123');
$pc->setOwner(1);
$pc->setGroup(1);
$pc->setConfig(1);
// add the items
$pc->add($pi1);
$pc->add($pi2);
// store everything
$em = $this->getEntityManager();
$em->persist($pc);
$em->flush();
这是我的项目
实体:
<?php
/**
* Item
* @package application\Entity
*
* @ORM\Entity
* @ORM\Table(name="items")
*/
class Item
{
/**
* @var int
*
* @ORM\Id
* @ORM\Column(name="id", type="integer")
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* @var string
* @ORM\Column(name="info", type="text")
*/
protected $info;
/**
* @var
* @ORM\ManyToOne(targetEntity="Collection", inversedBy="items")
* @ORM\JoinColumn(name="cid", referencedColumnName="id")
*/
protected $collection;
protected $collection;
/**
* @return string
*/
public function getInfo()
{
return $this->info;
}
/**
* @param string $info
*/
public function setInfo($info)
{
$this->info = $info;
}
}
public function setCollection(Collection $col = null)
{
$this->collection = $col;
return $this;
}
public function getCollection()
{
return $this->collection;
}
我希望它创建的项目在其“cid”列中具有集合的id
。但此列总是null
:
还有一个问题,它总是保存一个新的集合
。我希望设置id
会覆盖原来的设置。我强烈建议在使用双向关联之前阅读几遍,以充分理解两者之间的差异
项目
实体在此场景中是拥有方(从条令的角度来看)(1)。你坚持的是相反的一面(2)。条令将只检查关联的拥有方的变更(3)。此外,实体中缺少一些setter和getter,它们需要构建正确的双向关系(4)
将此方法添加到项
实体:
<?php
/**
* Item
* @package application\Entity
*
* @ORM\Entity
* @ORM\Table(name="items")
*/
class Item
{
/**
* @var int
*
* @ORM\Id
* @ORM\Column(name="id", type="integer")
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* @var string
* @ORM\Column(name="info", type="text")
*/
protected $info;
/**
* @var
* @ORM\ManyToOne(targetEntity="Collection", inversedBy="items")
* @ORM\JoinColumn(name="cid", referencedColumnName="id")
*/
protected $collection;
protected $collection;
/**
* @return string
*/
public function getInfo()
{
return $this->info;
}
/**
* @param string $info
*/
public function setInfo($info)
{
$this->info = $info;
}
}
public function setCollection(Collection $col = null)
{
$this->collection = $col;
return $this;
}
public function getCollection()
{
return $this->collection;
}
并在集合
实体中更改添加
方法,如下所示:
public function add(Item $oItem)
{
$this->items->add($oItem);
$oItem->setCollection($this); // Notice this line
}
当您想从集合中删除项时
public function removeItem(Item $oItem)
{
$this->items->removeElement($oItem);
$oItem->setCollection(null); // This is important too
}
在使用cascade=“all”
策略之前,您可能还需要阅读文档的相关部分
希望有帮助。+1请尊重花时间回答这样的问题。像@n00n这样的人只需花一些时间正确阅读文档即可。thx。这很有效。如何加载包含所有项的集合?只需向集合实体添加一个getter方法<代码>公共函数getItems(){return$this->items;}
这将返回一个项目集合,并且条令将自动加载该项目。执行了该操作,但它将不起作用。遍历图标会得到我:注意:未定义的索引:id在/第1768行的BasicEntityPersister.php返回所有现有项$result=$em->getRepository('..\Entity\Collection')->findOneById(1)$items=$result->getItems()$计数=计数(项目);它返回94个项目,但对于集合#1,只映射了2个项目