通过这种关系发现了一个新的实体。教义 [英] A new entity was found through the relationship. Doctrine
问题描述
Doctrine\ORM\ORMInvalidArgumentException
Doctrine\ORM\ORMInvalidArgumentException
通过关系Core\Model\Filter#category找不到新实体,该关系未配置为级联实体的持久性操作:Core\Model\Category @ 00000000314fc99200005639c0395a5e。要解决这个问题:在这个未知实体上显式调用EntityManager#persist()或配置级联在映射中保留这个关联,例如@ManyToOne(..,cascade = {persist})。如果您找不到哪个实体导致问题实现'Core\Model\Category #__ toString()'来获取线索。
A new entity was found through the relationship 'Core\Model\Filter#category' that was not configured to cascade persist operations for entity: Core\Model\Category@00000000314fc99200005639c0395a5e. To solve this issue: Either explicitly call EntityManager#persist() on this unknown entity or configure cascade persist this association in the mapping for example @ManyToOne(..,cascade={"persist"}). If you cannot find out which entity causes the problem implement 'Core\Model\Category#__toString()' to get a clue.
此函数导致异常。
代码中的一些comits。
它是ZendFramework 2。
This function cause an exception. Some comits in code. It is ZendFramework 2.
protected function handleCategory( $data, \Core\Model\Project $project )
{
$filter = $this->sl()->get( "Core\Filter\JSON\Category" );
$filter->setData( $data );
if( !$filter->isValid() ){
$id = @$data[ 'id' ];
$report = array(
'id' => $id,
'messages' => $filter->getMessages()
);
return $report;
}
$filteredData = $filter->getValues();
$repo = $this->em()->getRepository( "Core\Model\Category" );
$category = $repo->findOneBy(
array(
'id' => $filteredData[ 'id' ],
'project' => $project->getId()
)
);
$b = false;
if( !$category ){
$b = true;
$category = new \Core\Model\Category;
$category->setProject( $project )
->setCreatedAt( new \DateTime() );
}
$category->setUpdatedAt( new \DateTime() );
/* Hydrate Category */
$hydrator = $this->sl()->get( "Core\Hydrator\JSON\Category" );
$hydrator->hydrate( $filteredData, $category );
/* Persist Category */
$this->em()->persist($category);
if($b)
{
/* Return Filter Data */
$filterData = $hydrator->get();
/* Hydrate Filter */
$filterHydrator = $this->sl()->get( 'Core\Hydrator\JSON\Filter' );
//$filtersObj = array();
foreach($filterData as $i => $fdata)
{
$filterObj = new \Core\Model\Filter;
$filterObj->setProject($project)->setCategory($category);
$filterHydrator->hydrate($fdata, $filterObj);
$this->em()->persist($filterObj);
/*Breaks after any flush after second persist*/
$this->em()->flush();
}
}
return true;
}
我试图通过以下方式解决这个问题:
I tried to solve this problem by:
- MERGE,但另有例外,A managed dirty + entity [...]
- cascade = {persist},但是没有什么发生。
- 重写代码,你看到的是,我一个个坚持一个功能一个EntityManager(如果我错过了一些请告诉我)
从来没有使用ZendFramework工作。我喜欢Symfony与应用程序/控制台管理器,所有实体与表创建自动。
Never work before with ZendFramework. I love Symfony with app/console manager, all entities with tables creates authomatically.
推荐答案
解决这个问题添加
$this->em()->clear('Filter');
after
$this->em()->persist($filterObj);
我们可以删除
$this->em()->flush();
更快地做脚本。我把它粘贴到最后。
To do script faster. I paste it in the end.
我认为这不是一个好主意,但它的作品。
我还在等你的答案。
I think this is not good idea, but it works. I still waiting for your answers.
这篇关于通过这种关系发现了一个新的实体。教义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!