无法删除实体

时间:2014-08-26 08:22:16

标签: php database doctrine-orm entity

我有一张名为parameters的表。

它包含字段:idproject_idphase_idcompany_id等。

我有2行:

+----+------------+----------+------------+
| id | project_id | phase_id | company_id |
+----+------------+----------+------------+
|  1 |         10 |     NULL |       NULL |
|  2 |         10 |        2 |       NULL |
+----+------------+----------+------------+

我想删除一行

try {
    $parameter = $em->find("Parameters", 1);
    $em->remove($parameter);
    $em->flush();
} catch (Exception $e) {
    displayTree($e->getMessage());
    displayTree($e->getTraceAsString());
    exit();
}

但我面临错误信息:

Entity was not found.
#0 /home/www/html/.tmp/__CG__Phase.php(108): Doctrine\ORM\Proxy\ProxyFactory->Doctrine\ORM\Proxy\{closure}(Object(DoctrineProxies\__CG__\Phase), '__load', Array)
#1 /home/www/html/.tmp/__CG__Phase.php(108): Closure->__invoke(Object(DoctrineProxies\__CG__\Phase), '__load', Array)
#2 /home/www/html/v3/vendor/doctrine/orm/lib/Doctrine/ORM/UnitOfWork.php(2254): DoctrineProxies\__CG__\Phase->__load()
#3 /home/www/html/v3/vendor/doctrine/orm/lib/Doctrine/ORM/UnitOfWork.php(1688): Doctrine\ORM\UnitOfWork->cascadeRemove(Object(DoctrineProxies\__CG__\Phase), Array)
#4 /home/www/html/v3/vendor/doctrine/orm/lib/Doctrine/ORM/UnitOfWork.php(2278): Doctrine\ORM\UnitOfWork->doRemove(Object(DoctrineProxies\__CG__\Phase), Array)
#5 /home/www/html/v3/vendor/doctrine/orm/lib/Doctrine/ORM/UnitOfWork.php(1688): Doctrine\ORM\UnitOfWork->cascadeRemove(Object(DoctrineProxies\__CG__\Plan), Array)
#6 /home/www/html/v3/vendor/doctrine/orm/lib/Doctrine/ORM/UnitOfWork.php(2278): Doctrine\ORM\UnitOfWork->doRemove(Object(DoctrineProxies\__CG__\Plan), Array)
#7 /home/www/html/v3/vendor/doctrine/orm/lib/Doctrine/ORM/UnitOfWork.php(1688): Doctrine\ORM\UnitOfWork->cascadeRemove(Object(Phase), Array)
#8 /home/www/html/v3/vendor/doctrine/orm/lib/Doctrine/ORM/UnitOfWork.php(2278): Doctrine\ORM\UnitOfWork->doRemove(Object(Phase), Array)
#9 /home/www/html/v3/vendor/doctrine/orm/lib/Doctrine/ORM/UnitOfWork.php(1688): Doctrine\ORM\UnitOfWork->cascadeRemove(Object(DoctrineProxies\__CG__\Project), Array)
#10 /home/www/html/v3/vendor/doctrine/orm/lib/Doctrine/ORM/UnitOfWork.php(2278): Doctrine\ORM\UnitOfWork->doRemove(Object(DoctrineProxies\__CG__\Project), Array)
#11 /home/www/html/v3/vendor/doctrine/orm/lib/Doctrine/ORM/UnitOfWork.php(1688): Doctrine\ORM\UnitOfWork->cascadeRemove(Object(Parameters), Array)
#12 /home/www/html/v3/vendor/doctrine/orm/lib/Doctrine/ORM/UnitOfWork.php(1659): Doctrine\ORM\UnitOfWork->doRemove(Object(Parameters), Array)
#13 /home/www/html/v3/vendor/doctrine/orm/lib/Doctrine/ORM/EntityManager.php(647): Doctrine\ORM\UnitOfWork->remove(Object(Parameters))
#14 /home/www/html/v3/test.php(15): Doctrine\ORM\EntityManager->remove(Object(Parameters))
#15 {main}

所以,我可以假设问题是phase_id有Null而不是有效id,因为当我尝试删除第二行时它工作正常。

在课程Parameters中,我有下一个映射阶段:

/**
 * @ManyToOne(targetEntity="Phase", inversedBy="parameters")
 * @JoinColumn(name="phase_id", referencedColumnName="id")
 **/
protected $phase;

在课程Phase中,我有以下内容:

/**
 * @OneToMany(targetEntity="Parameters", mappedBy="phase")
 */
private $parameters;

我找不到问题出在哪里,也许我做错了什么?

1 个答案:

答案 0 :(得分:1)

当外键上的值为!= NULL时,将创建代理对象。例如,如果你有0,那么就会出现这个问题