ZF2 / Doctrine不允许我保存一个集合


ZF2 / Doctrine wont let me save a collection

必须处理一个名为Collection的实体,该实体有一个数组要保存为另一个实体。

这是我在控制器中使用的,用于测试我的实体:

// 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();
这是我的Item实体:
<?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;
    }
}

这是集合实体

中的代码
/**
 * Collection
 * @package Application'Entity
 *
 * @ORM'Entity
 * @ORM'Table(name="collections")
 */
class Collection
{
    /**
     * @var int
     *
     * @ORM'Id
     * @ORM'Column(name="id", type="integer")
     * @ORM'GeneratedValue(strategy="AUTO")
     */
    protected $id;
    /**
     * @var string
     *
     * @ORM'Column(name="usr", type="integer")
     */
    protected $owner;
    /**
     * @var string
     *
     * @ORM'Column(name="grp", type="integer")
     */
    protected $group;
    /**
     * @var string
     *
     * @ORM'Column(name="cnf", type="integer")
     */
    protected $config;
    /**
     * @var string
     *
     * @ORM'Column(name="info", type="text")
     */
    protected $info;
    /**
     * @var
     * @ORM'OneToMany(targetEntity="Item", mappedBy="items", cascade="all")
     */
    protected $items;
    public function __construct(){
        $this->items = new ArrayCollection();
    }
    public function add(Item $oItem){
        $this->items->add($oItem);
    }

我希望它创建的项目在其列"cid"中具有idCollection。但这一列始终是null:

另一个问题,它总是保存一个新的Collection。我希望设置id会覆盖原来的

我强烈建议在使用双向关联之前多读几遍以充分理解正侧和反侧的区别。

在这种情况下,Item实体是所有者方(从doctrine的角度来看)(1)。您正在持久化反面(2)。doctrine将只检查关联的所有者方以进行更改(3)。你的实体中缺少getter,这需要建立一个正确的双向关系(4)。

将这些方法添加到Item entity:

public function setCollection(Collection $col = null)
{
    $this->collection = $col;
    return $this;
}
public function getCollection()
{
    return $this->collection;
}

并在Collection实体中更改add方法,如下所示:

public function add(Item $oItem)
{
    $this->items->add($oItem);
    $oItem->setCollection($this); // Notice this line
}

当你想从Collection中删除Item时;

public function removeItem(Item $oItem)
{
    $this->items->removeElement($oItem);
    $oItem->setCollection(null); // This is important too
}

在使用cascade="all"策略之前,您可能还需要阅读文档中的传递持久性部分。

希望能有所帮助。