Consider I have two database tables: images
and albums
. They have these schemas:
images:
id, orig_name, hash, filename, uploaded, views, album_id, user_id, server_id
albums:
id, hash, name, description, user_id
The album_id
field in the images
table points to the id
field in the albums
table. Pretty simple one-to-many stuff. An image can belong to one album and an album can have many images. What I'm confused about is how to get images from a specified album in my CakePHP Image
model. At the moment this is my model:
class Image extends AppModel {
public $name = 'Image';
public $belongsTo = array('Server', 'Album');
public function getImages($options) {
$params = array('conditions' => array());
// Specific user
if (!empty($options['userID'])) {
array_push($params['conditions'], array('Image.user_id' => $options['userID']));
}
// Specific album
if (!empty($options['albumHash'])) {
array_push($params['conditions'], array('Image.album_id' => $options['albumHash']));
}
// Order of images
$params['order'] = 'Image.uploaded DESC';
if (!empty($options['order'])) {
$params['order'] = $options['order'];
}
return $this->find('all', $params);
}
}
What I want it to do is return images from a specific album based on the album hash (the hash
field in the table). I know how I can make this work if I had the album ID, but I don't, I only have the hash.
I already have an album model that's just this:
class Album extends AppModel {
public $name = 'Album';
}
but I'm not even sure if that's relevant. :p
Thanks.