I'm fairly new to CakePHP, and have run into an issue that I can't seem to find a good solution for. My application has the following associations:
- Member hasMany Vote
- Roll hasMany Vote
- Vote belongsTo Member, Roll
I'd like to retrieve all of the votes cast by a member and the roll associated with each vote. The following code produces the perfect result:
$this->Member->id = $id;
$this->Member->contain(array('Vote' => 'Roll'));
$this->set('member_data', $this->Member->read());
Unfortunately, this code is also very inefficient. Instead of just doing something like SELECT * FROM votes JOIN rolls ON votes.roll_id = rolls.id WHERE votes.member_id = 100
, Cake does the following:
SELECT * FROM votes WHERE member_id = 100
SELECT * FROM rolls WHERE roll_id = 1
SELECT * FROM rolls WHERE roll_id = 2
SELECT * FROM rolls WHERE roll_id = 3
SELECT * FROM rolls WHERE roll_id = 4
- And so on--a big problem when over 500 rolls match!
How would I make this more efficient while still using the read() method?