Php Symfony-多通关系的外来属性的未定义索引
这个案子有点复杂。我有一个控制器操作Php Symfony-多通关系的外来属性的未定义索引,php,symfony,doctrine-orm,mapping,entitymanager,Php,Symfony,Doctrine Orm,Mapping,Entitymanager,这个案子有点复杂。我有一个控制器操作editCreateFirstFormPart,它处理工作流对象的一些字段,并呈现两部分表单的适当第一部分 如果我使用存储对象的id调用此操作方法的路由,则窗体将加载所有字段。我将填充表单的数据库对象存储在当前会话中,以防用户决定取消第二个表单中的编辑: // Persist all changes of made in the first form part $manager->persist($workflow); $manager->flus
editCreateFirstFormPart
,它处理工作流
对象的一些字段,并呈现两部分表单的适当第一部分
如果我使用存储对象的id调用此操作方法的路由,则窗体将加载所有字段。我将填充表单的数据库对象存储在当前会话中,以防用户决定取消第二个表单中的编辑:
// Persist all changes of made in the first form part
$manager->persist($workflow);
$manager->flush();
// In case the wolkflow already exists store it in the session
if(!$newWorkflow) $this->get('session')->set($workflowSessionName, $workflowBeforeSubmit);
return $this->redirectToRoute('pec_test_pra_edit_workflow_second_part', array(
'project' => $project->getId(),
'workflow' => $workflow->getId(),
'newWorkflow' => $newWorkflow,
// Pass the name in the session of the stored workflow to the action method which handles the second form
'workflowSessionName' => $workflowSessionName,
));
这很有魅力。现在,如果(我检索)用户通过cancel
按钮提交第二个表单,我想将存储在数据库中的对象重置为提交第一个表单部分之前的状态。因此,我获取存储在会话中的对象,并希望持久化它:
if($sessionObject instanceof Workflow) {
$manager = $this->getObjectManager();
$sessionObject = $session->get($workflowSessionName);
$manager->persist($sessionObject);
$manager->flush();
}
这里我得到了以下错误:
注意:在vendor\doctor\orm\lib\doctor\orm\UnitOfWork.php(第2917行)中未定义索引:00000000 61675B3000000002DDB0A6
第2917行:return$this->entityIdentifiers[spl_object_hash($entity)]代码>
UnitOfWork->getEntityIdentifier(对象(燃料))//在prepareUpdateData之后调用
BasicEntityPersister->prepareUpdateData(对象(MProject))
我的实体工作流
与项目
类具有单向多通关系。MProject
类扩展了Project
,并具有classfuel
的一个fuel
属性
因此,一个燃料
有许多项目
s(一个项目)和许多项目
有一个燃料
(多个项目)。以下是.orm.yml
文件的相应部分:
Fuel.orm.yml
:
'ABundle\Entity\Fuel':
...
oneToMany:
projects:
targetEntity: 'ABundle\Entity\MProject'
mappedBy: fuel
'ABundle\Entity\MProject':
repositoryClass: 'ABundle\Repository\MProjectRepository'
type: entity
manyToOne:
fuel:
targetEntity: 'ABundle\Entity\Fuel'
inversedBy: projects
MyBundle\Entity\Workflow:
...
manyToOne:
project:
targetEntity: BBundle\Entity\Project
cascade: [persist]
...
内部燃油等级:
class Fuel {
/**
* The collection of projects using the fuel.
*
* @var \Doctrine\Common\Collections\Collection
*/
private $projects;
...
}
use BBundle\Entity\Project as BaseProject; // Origin of Project in my Entity
class MProject extends BaseProject implements SearchableTypeAlias {
/**
* The fuel used by the project.
*
* @var Fuel
*/
protected $fuel;
...
}
use BBundle\Entity\Project;
class Workflow {
/**
* @var Project
*/
private $project;
...
}
MProject.orm.yml
:
'ABundle\Entity\Fuel':
...
oneToMany:
projects:
targetEntity: 'ABundle\Entity\MProject'
mappedBy: fuel
'ABundle\Entity\MProject':
repositoryClass: 'ABundle\Repository\MProjectRepository'
type: entity
manyToOne:
fuel:
targetEntity: 'ABundle\Entity\Fuel'
inversedBy: projects
MyBundle\Entity\Workflow:
...
manyToOne:
project:
targetEntity: BBundle\Entity\Project
cascade: [persist]
...
项目内部MProject
类别:
class Fuel {
/**
* The collection of projects using the fuel.
*
* @var \Doctrine\Common\Collections\Collection
*/
private $projects;
...
}
use BBundle\Entity\Project as BaseProject; // Origin of Project in my Entity
class MProject extends BaseProject implements SearchableTypeAlias {
/**
* The fuel used by the project.
*
* @var Fuel
*/
protected $fuel;
...
}
use BBundle\Entity\Project;
class Workflow {
/**
* @var Project
*/
private $project;
...
}
Workflow.orm.yml
:
'ABundle\Entity\Fuel':
...
oneToMany:
projects:
targetEntity: 'ABundle\Entity\MProject'
mappedBy: fuel
'ABundle\Entity\MProject':
repositoryClass: 'ABundle\Repository\MProjectRepository'
type: entity
manyToOne:
fuel:
targetEntity: 'ABundle\Entity\Fuel'
inversedBy: projects
MyBundle\Entity\Workflow:
...
manyToOne:
project:
targetEntity: BBundle\Entity\Project
cascade: [persist]
...
内部工作流
类:
class Fuel {
/**
* The collection of projects using the fuel.
*
* @var \Doctrine\Common\Collections\Collection
*/
private $projects;
...
}
use BBundle\Entity\Project as BaseProject; // Origin of Project in my Entity
class MProject extends BaseProject implements SearchableTypeAlias {
/**
* The fuel used by the project.
*
* @var Fuel
*/
protected $fuel;
...
}
use BBundle\Entity\Project;
class Workflow {
/**
* @var Project
*/
private $project;
...
}
我真的不知道我的用例出了什么问题,特别是因为在第一个控制器操作方法中调用persist
存储$workflow
对象没有问题
如何修复此未定义索引的错误?由于您正在使用重定向到路由,因此正在创建一个新请求以加载表单的第二部分。此时,identityMap已重建,并且没有先前加载的会话对象(工作流)的映射(长id)的引用。请尝试再次从数据库检索对象,并使用会话对象的值设置属性
PS:可能有一种更干净的方法可以做到这一点,但我不知道。您的问题是您试图从EntityManager
保存实体<代码>工作流
,项目
实体被标记为新建
,并计划插入而不是更新。在探索会话的project->fuel
关联project
的过程中,找到另一个fuel
实体,该实体具有非空的id
值。但是它已分离,并且在EntityManager
中找不到id
,因此引发异常
应该在DB中有一个对应行,但不是由EntityManager
加载的实体(在您的示例中是从会话中未序列化的)应该放入EntityManager
。您应该合并所有工作流
,项目
,燃料
实例。最简单的方法是为级联合并cascade:[merge]
配置workflow->project
和project->fuel
关联,并仅合并会话工作流
$manager = $this->getObjectManager();
$sessionObject = $session->get($workflowSessionName);
$manager->merge($sessionObject);
$manager->flush();
请注意,这将自动将实体字段值替换为从会话中获取的值。确保您只有实体的yml文件。确保语法正确=>注意quotes
cascade:[“persist”]
yml文件总是被缓存!为开发人员和产品环境清除缓存。@V-Light感谢您的建议。你是对的,在清除缓存后,检测到持久化
,但是发生了另一个错误,因此我更改了整个问题,而不是打开另一个问题。你不知道的是:1。我在我的工作流
类中有一个属性状态
,它与另一个类状态
有一个manytone
关系。2.在我的editCreateFirstFormPartAction
方法中,可以编辑/添加状态
属性的所有元素。3.在myeditCreateFirstFormPartAction
中添加/编辑状态时,在提交参数对象之后,$workflow
将被表单值填充并持久化。4.当提交第二个表单并调用createEditWorkflowSecondPartAction
时,参数对象$workflowSecond
被填充。5。然后调用$manager->merge($sessionObject)
和flush
所有状态的编辑/添加都错误地保留了下来,我认为这是因为doctoriesUnitOfWork
类保存了前一个工作流对象的状态$workflow
和$workflowSecond
在DoctrinesUnitOfWork
类中具有相同的引用。这就是为什么我不能使用合并!不,问题是它是oneToMany
关联,这里的拥有方是状态
-它从未加载也从未删除,因此您应该尝试使用级联:[merge]
和孤立删除来配置此关系