I have a table A which references table B.
edit: Database engine used is MyISAM.
Doctrine mapping works like a charm, except when I have non valid case in the DB where referenced ID in table A does not really exist in table B.
So when you execute this code:
$objectB = $objectA->getObjectB();//with lazy load
you actually get $objectB proxy object which is not null. So !empty($objectB) will pass.
And when you try to access any property of $objectB, like:
$objectB->getName();
you get Entity not found exception. There is not way you can predict in your code that $objectB actually does not exist and that there’s no Name property for $objectB.
$objectB should actually be set to null but that is not happening.
Hibernate has actually mapping property not-found=ignore which sets missing object to NULL instead of setting it to Proxy object. Does Doctrine has anything similar?
PS. Of course you can always catch the Entity not found exception, and play around with that. Or you can map the actual objectB_ID field at the table A, but those are not 100% clean solutions.
I hope someone has an answer.
Thank You
IMO this is a case of garbage in, garbage out. If you have schema where TableA may or may not have a row in TableB, you should implement a FK constraint on TableB, so that if a row is deleted from TableB, any rows in TableA referencing deleted rows will have their values changed to null.
If you really wanted to move forward on your proposed schema implementation, you could try doing:
This of course assumes you have an id column on tableB and that it is unsigned.
— Update —