views:

38

answers:

1

How should be implemented facultative one-to-one relation in Doctrine ORM and Symfony? Suppose there are some folders represented in database. Each folder can have a default icon or some custom icon represented in another table. How should this relation be described in the schema file? How can I tell that in case of given folder relation does or doesn't occur?

I myself have to guesses, but each seems to be not quite good:

1) Let's say I define folder_icon table with id column and folder_icon_id column in folder table and link these columns with foreign key. If folder_icon_id contains NULL, relation doesn't occur. If it contains some integer value it points to respective folder icon. When I implement it this way and try to obtain folder icon using something like $folder->getFolderIcon(), I get an instance of FolderIcon class with fields set to null (where I would rather excpect to get something like NULL, FALSE or Doctrine_Null). Why is it so? How should I check if the returned object is not 'real' folder icon?

2) Let's assume that I use method similar to previous but I define first row of folder_icon table to be the default icon, so that each folder that doesn't have any custom icon selected is related to this first row. In this case there is no problem with getting some dummy instances of FolderIcon class. But there is a problem if custom folder icon is removed form database, as there is no onDelete behaviour 'SET 1' to relate any folders using the deleted icon with the default icon.

How should this problem be solved? What is the proper way to define this kind of relation in schema file?

+1  A: 

The problem is with the magic methods getVariable

Use $folder->folder_icon and to test for an existence of that relationship use isset(). Have a read of the doctrine website docs about testing for the existence of a relationship, I'm currently mobile so unable to link to it.

johnwards
Great, that's exactly what I was looking for. So I should follow the first way of describing the schema and check if relationship exists for a given folder with isset($folder->FolderIcon). Thanks!
Mariusz Alef Bąk