3

我在 Zend Framework 2 项目中使用 Doctrine 2 ORM。我试图坚持多对多的关系。我遵循了此处描述的文档(manytomany)。在尝试持久化数据时:$em->persist($form->getData());我收到了错误:

"The class 'Doctrine\ORM\PersistentCollection' was not found in the chain configured namespaces".

有什么建议么 ?

为了更清楚,我在下面添加了一些代码:

首先,对于多对多关系,我注释了文档中所说的实体:

 /**
 * @ORM\ManyToMany(targetEntity="\User\Entity\Client", mappedBy="reportSettings")
 */
private $client;

public function __construct() {
    $this->client = new ArrayCollection();
}

/**
 * @ORM\ManyToMany(targetEntity="\Statistics\Entity\ReportSettings", inversedBy="client")
 * @ORM\JoinTable(name="report_client_settings",
 *      joinColumns={@ORM\JoinColumn(name="client_id", referencedColumnName="id")},
 *      inverseJoinColumns={@ORM\JoinColumn(name="report_setting_id", referencedColumnName="id")})
 */
private $reportSettings;

public function __construct() {
    $this->reportSettings = new ArrayCollection();
}

并且在控制器中

$form = new UpdateReportSettingsForm();

    $form->bind($reportSettings);

    $request = new Request();

    if ($request->isPost()) {
        $form->setData($request->getPost());

        if ($form->isValid()) {
         $data = $form->getData();
          $em->persist($data); // here I got the error - The class 'Doctrine\ORM\PersistentCollection' was not found in the chain configured namespaces
          $em->flush();

}

我也以 DoctrineModule\Form\Element\ObjectMultiCheckbox 的形式使用。一个简单的var_dump($data)- 返回一个持久的集合。

4

2 回答 2

0

出现错误是因为未正确定义表单。我在这里建立的如何建立多对多关系的正确方法 - http://laundry.unixslayer.pl/2013/zf2-quest-zendform-many-to-many/

于 2013-12-18T08:29:59.500 回答
-1

您是否在要映射的实体的开头添加了这段代码

use Doctrine\Common\Collections\ArrayCollection;
于 2013-09-09T13:39:29.543 回答