0

I'm working on an app with extensive database relationships and I need to return a specific data object. I'm having trouble with it. Here's what I need it to look like:

AccommodationGroup =>  
array(  
    ['name']=>'Group1',  
    ['AccommodationRoom']=> array(  
        [0]=> array(  
            ['id']=>1,  
            ['RoomIdentifier']=>'Cabin001',  
        ),  
        [1]=> array(  
            ['id']=>2,  
            ['RoomIdentifier']=>'Cabin002'  
        )  
    )  
)

AccommodationRoom is related to camp by camp_id. AccommodationGroup is related to AccommodationRoom by accommodation_group_id.

As you can see in the data object example, I need to retrieve all the groups for a particular camp with the rooms of each group as a nested array of the group, and all this restricted to a particular camp.

I've tried to get at it by selecting all the applicable groups using findAllByCampId() which, of course, doesn't work (knew it wouldn't but tried it anyway). Another table, AccommodationRoomsCamp is a transitional table between AccommodationRooms and Camps. One way to do it would be:

$this->AccommodationRoomsCamp->findAllByCampId($id, array('group'=>'AccommodationRoomsCamp.accommodation_group_id'))

but accommodation_group_id is not stored in AccommodationRoomsCamp because it is already being stored in the AccommodationRoom table. I think that I need to do more than one operation, but I'm baffled. Little bit of a newb. Ideas?

1 Answer 1

1

Have a little bit of a hard time following how your tables are related - but I think you can solve your problem using recursive finds. Something like this in your controller:

$this->AccomodationRoomsCamp->recursive = 2; $this->AccomodationRoomsCamp->find('all, array('conditions' => array(bla bla bla bla

See http://book.cakephp.org/view/1063/recursive

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, with your suggestion I was able to figure it out.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.