doctrine2 – Symfony2表单:如何持久化具有可空关联的实体?

doctrine2 – Symfony2表单:如何持久化具有可空关联的实体?,第1张

概述保存表单提交数据时,我无法持久保存新实体实例,其中实体与另一个实体具有可空的关联,并且我尝试将其设置为null.在为表单创建新的实体实例后,将提交的请求绑定到表单并持久化并刷新实体实例,具体取决于我如何填充关联实体的属性,我得到 > UnexpectedTypeException:类型为“object or array”的预期参数,给定“NULL”(如果设置为null),或者 > InvalidA 保存表单提交数据时,我无法持久保存新实体实例,其中实体与另一个实体具有可空的关联,并且我尝试将其设置为null.在为表单创建新的实体实例后,将提交的请求绑定到表单并持久化并刷新实体实例,具体取决于我如何填充关联实体的属性,我得到

> UnexpectedTypeException:类型为“object or array”的预期参数,给定“NulL”(如果设置为null),或者
> invalidargumentexception:通过“AccessLog#document”关系找到一个新实体,该关系未配置为级联实体的持久化 *** 作(如果设置为相关实体的新的空实例,我不想保留).

如果我设置了cascade persist,它会尝试在相关表中创建一条记录(db中的数据模型不允许),即使没有数据可以保留.如果设置级联持续是要走的路,我该如何阻止它尝试创建新记录?处理这个问题的最佳方法是什么?

注意,无论关联是设置为单向还是双向,行为都是相同的.

细节:

我有一个与另一个实体(缩写)有多对一关联的实体:

/** @Entity */class AccessLog{    /** @ID @Column(type="integer") */    private $access_log_ID;    /** @Column(type="integer",nullable=true) */    private $document_ID;    /**     * @ManyToOne(targetEntity="document",inversedBy="access_logs",cascade={"persist"})     * @JoinColumn(name="document_ID",referencedColumnname="document_ID")     */    private $document;    // plus other fIElds    // plus getters and setters for all of the above...}

相关实体没什么特别的:

/** @Entity */class document{    /** @ID @Column(type="integer") */    private $document_ID;    /** @Column(length=255) */    private $name;    /** @OnetoMany(targetEntity="AccessLog",mappedBy="document") */    private $access_logs;    // plus other fIElds    // plus getters and setters for all of the above...}

我有一个Symfony表单,用于输入新的AccessLog记录的数据:

class AccessLogFormType extends AbstractType{    public function getname()    {        return 'access_log_form';    }    public function getDefaultoptions(array $options)    {        return array('data_class' => 'AccessLog');    }    public function buildForm(FormBuilder $builder,array $options)    {        $builder->add('access_log_ID','hIDden');        $builder->add('document_ID','hIDden',array(            'required' => false        ));        $builder->add('document',new documentType(),array(            'label' => 'document','required' => false        ));        //...    }}

使用以下支持类型定义:

class documentType extends AbstractType{    public function getname()    {        return 'document';    }    public function getDefaultoptions(array $options)    {        return array('data_class' => 'document');    }    public function buildForm(FormBuilder $builder,array $options)    {        $builder->add('name','text',array(            'required' => false        ));    }}

我的控制器包括以下内容:

public function save_access_log_action(){    $request = $this->get('request');    $em = $this->get('doctrine.orm')->getEntityManager();    $access_log = null;    if ($request->getmethod() === 'POST') {        $data = $request->request->get('access_log_form');        if (is_numeric($data['access_log_ID'])) {            $access_log = $em->find('AccessLog',$data['access_log_ID']);        } else {            $access_log = new AccessLog();        }        if (is_numeric($data['document_ID'])) {            $document = $em->find('document',$data['document_ID']);            $access_log->set_document($document);        } else {            // Not calling set_document() since there shouldn't be a            // related document entity.        }        $form = $this->get('form.factory')            ->createBuilder(new AccessLogFormType(),$access_log)            ->getForm();        $form->bindRequest($request);        if ($form->isValID()) {            $em->persist($access_log);            $em->flush();        }    } else {        // ... (handle get request)    }    return $this->render('access_log_form.tpl',array(        'form' => $form->createVIEw()    ));}

上述代码在更新现有访问日志条目或创建新条目并在表单中选择文档时工作正常,但如果没有选择文档则不行.

假设无法更改数据模型,如何在不持久保存新document实体的情况下持久保存新的AccessLog实体?

解决方法 似乎解决方案是在persist *** 作之前将set_document(null)调用移到右边,因为将请求绑定到表单会导致将空document对象附加到AccessLog对象的$document属性.

删除级联持久性并使关联单向后,此解决方案也可以工作.

感谢@ greg0ire的帮助.

[更新]有必要将@ChangeTrackingPolicy(“DEFERRED_EXPliCIT”)添加到document实体定义,因为它继续尝试更新与AccessLog记录关联的document记录,例如在删除现有关联时(导致$name)要在document上设置为null的属性,然后在db中设置为null.当删除关联时,默认行为是删除/修改数据,这是非常令人沮丧的,即使未指定级联持久性也是如此.

总结

以上是内存溢出为你收集整理的doctrine2 – Symfony2表单:如何持久化具有可空关联的实体?全部内容,希望文章能够帮你解决doctrine2 – Symfony2表单:如何持久化具有可空关联的实体?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/1138966.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-31
下一篇2022-05-31

发表评论

登录后才能评论

评论列表(0条)

    保存