保存表单提交数据时,我无法持久保存新实体实例,其中实体与另一个实体具有可空的关联,并且我尝试将其设置为null.在为表单创建新的实体实例后,将提交的请求绑定到表单并持久化并刷新实体实例,具体取决于我如何填充关联实体的属性,我要么得到
UnexpectedTypeException: Expected argument of type "object or array", "NULL" given (如果设置为null),或InvalidArgumentException: A new entity was found through the relationship 'AccessLog#document' that was not configured to cascade persist operations for entity (如果设置为相关实体的新的空实例,我不想保留它).如果我设置了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 …Run Code Online (Sandbox Code Playgroud)