Say you have the following relations (altered for simplicity):
Dog:
columns:
id: { type: integer, notnull: true, primary: true }
name: { type: string, notnull: true }
AwardType:
columns:
id: { type: integer, notnull: true, primary: true }
name: { type: string, notnull: true }
Award:
columns:
dog_id: { type: integer, notnull: true, primary: true }
award_id: { type: integer, notnull: true, primary: true }
is_granted: { type: boolean, notnull: true, default: false }
relations:
Dog:
local: dog_id
foreign: id
type: one
foreignType: many
foreignAlias: Awards
AwardType:
local: award_id
foreign: id
type: one
foreignType: many
foreignAlias: Awards
With the above setup I can add a new dog and manually add Awards to that dog. I can also edit awards already granted to a dog.
When I call $myDog->getAwards() I want the set to include all granted awards (Award.is_granted==true && Award.dog_id==$myDog()->getId()) pluss all awards that have not been granted to that dog yet.
Is there an option that can be set in a model somewhere to make this happen? If not, what would be the best way to achieve it?
I’m using Symfony 1.4 and the bundled Doctrine 1.2 ORM.
[ Edit 1 ]
I realize I didn’t explain the whole award thing properly, so I’ll try to expand on it. Say you have the following AwardTypes:
- 1: Bone for not molesting postman
- 2: Collar for not biting Miss Molly
- 3: Bigger bone for chasing milkman (who we all know is evil)
Dog1 is already registered and has been granted AwardTypes 1 and 3. When editing that Dog the form should display AwardTypes 1 and 3 with a checked checkbox and AwardType 2 with an unchecked checkbox. This works great if Dog 1 has one entry in the Award table for each of the AwardTypes. Two with is_granted == true and one is_granted == false. So far so good.
When the user enters a new Dog the form should display all the AwardTypes, but with no checked checkboxes. When saving the new Dog a total of 3 rows should appear in the Awards table with the is_granted-flag set according to the checked-state of the checkboxes.
I know I can get all AwardTypes and check that against the AwardTypes a Dog has been granted already (showing all unchecked for new dogs, granted checked + not yet granted unchecked for existing dogs). What I am asking here is wether or not Doctrine has some magic that will give me compound sets as described above.
Ok you have a problem modeling your data. It should probably look like this:
I have used a many-to-many relations, and added some constraint that I think might be useful in your case.
Symfony should generate a form looking like what you want, tell us if it is not the case.