Liauchuk Ivan
Liauchuk Ivan

Reputation: 2003

Can't delete entity

I have a table called parameters.

It has fields: id, project_id, phase_id, company_id, etc.

I have 2 rows there:

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

I want to delete one row

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

But I face error message:

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}

So, as I can suppose the problem is that phase_id has Null instead of valid id, because when I try to delete second row it works fine.

In class Parameters I have next mapping with phase:

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

In the class Phase I have following:

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

I can't find where is the problem, maybe I do something wrong?

Upvotes: 0

Views: 111

Answers (1)

SylarBg
SylarBg

Reputation: 121

A proxy object is created, when the value on the foreign key is != NULL. If you are having 0 thhere for example, this problem will happen

Upvotes: 1

Related Questions