I have an object in PHP with a composition of objects like:
// Settlement (DB Fields: id, name)
id = 1, name = 'Meep', jobs = array()
-
// Job (DB Fields: id, action, settlement_id)
id = 1, action = 'build'
So I can do something like: $settlement->getJobs() and it’ll return an array of jobs
When it comes to processing these jobs though I need to know the Settlement as well. So I’m kind of confused as to how it should exist.
Should a Job contain a Settlement and then do something like: $job->getSettlement() or would there be an infinite loop of nesting/composition?
Or when I get the instance from the database to process the job, should I first get the Settlement object containing the Job that’s to be processed so it’s in the same as before?
Within my database the Job is attached to the Settlement via foreign key settlement_id so within my Job entity I wasn’t sure if I should be storing just settlement_id or the entire Settlement but then there’s the nesting/composition loop.
It’s perfectly acceptable for nodes to link to each other. You can have an instance of the
SettlementwhoseJobit is inside of theJobobject.