【问题标题】:The class 'Doctrine\ORM\PersistentCollection' was not found in the chain configured namespaces在链配置的命名空间中找不到类“Doctrine\ORM\PersistentCollection”
【发布时间】:2013-12-18 16:29:59
【问题描述】:

我在 Zend Framework 2 项目中使用 Doctrine 2 ORM。 我试图坚持多对多的关系。我遵循了描述here(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) - 返回一个持久集合。

【问题讨论】:

  • 如果你能把代码给我们看,给你一个合理的答案会更容易。
  • 我添加了一些代码。如果还不够,请告诉我,我会添加更多。
  • 发布您的完整实体,错误只是说明您的名称空间没有提到包含的类,这有点含糊。

标签: orm doctrine-orm zend-framework2


【解决方案1】:

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

【讨论】:

    【解决方案2】:

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

    use Doctrine\Common\Collections\ArrayCollection;
    

    【讨论】:

    • 嗯,您需要坚持的是您的客户实体,而不是数据。因此获取实体$client = new Client(); 检查表单是否已提交,如果表单有效则获取数据$data = $form->getData(); 在客户端实体上设置值并将reportSetting 实体添加到客户端实体,最后持久化客户端实体$em->persist($client);希望对您有所帮助!!!
    • 我按照本教程进行操作 - github.com/doctrine/DoctrineModule/blob/master/docs/hydrator.md。因此,通过本教程,如果正确定义了表单并且使用集合定义了学说实体,则不需要设置 dat。有没有多对多的好例子——ZF2 和 Doctrine 2?
    • 我遵循了一些不同的东西,而不是一个具体的例子,我在实体中使用了 setter 和 getter 来设置和获取值。
    • 确实已经有一段时间了,但是您是否设法使其正常工作?如果可以,请您提供解决方案。它对其他人有用
    猜你喜欢
    • 2016-10-31
    • 1970-01-01
    • 2012-09-14
    • 1970-01-01
    • 2023-03-02
    • 2015-11-12
    • 1970-01-01
    • 2019-08-17
    • 2019-08-10
    相关资源
    最近更新 更多