@Boyz26 - You are very welcome!
should that work then for photos that have no album? or only exists in a profile?.. will many relationships on an item like a photo create performance issues?
This should work fine. I’m doing almost the exact same thing with notes. My notes can be attached to multiple items, but are assigned to only one at a time (unlike how you have multiple items sharing a photo).
Thist is pretty simple to model, you just need separate tables for each relationship.
Also, for certain items, that say has one file, i can specify that in the has_one array, but i’m getting lost when trying to store a label for that (or config data), when producing a form for instance.. because the photos are generic but i want to be able to give it a specific purpose or name, depending on the item it’s attached to. I’m attempting to put that information in the model validation array, but i think i’m just adding too much to the problem.
Well, I don’t know how to store a label based on which item is selected, but you can easily determine which type of owner a photo has. Sadly, it requires one DB check for each type, but I’m currently just doing a lookup on each association, and then checking to see if it found anything — ie: if(empty($this->user->id)). If it’s empty, you move on to the next type. Ugly, but functional.
If you are going the other way ($user->photo), you can check the related field from within your photo object. Put together, it might look like this:
public static $type_labels = array(
'user' => 'User Photo',
'group' => 'Group Photo',
'userprofile' => 'User Photo'
);
function get_owner_type() {
$type = NULL;
if(isset($this->related) && isset($this->related['model']) {
$type = $this->related['model'];
} else {
// check each model type
foreach(self::$type_labels $k => $t) {
$this->{$k}->get();
if( ! empty($this->{$k}->id) ) {
$type = $k;
break;
}
}
}
return $type;
}
function get_type_label($type => NULL) {
if(is_null($type)) {
$type = $this->get_owner_type();
}
if(array_key_exists($type, self::$type_labels)) {
return self::$type_labels[$type];
} else {
return 'ERROR';
}
}Hopefully that all makes sense. I just typed it up here in the browser, so I don’t know how many errors it contains 😛
SMALL UPDATE: I tweaked my example to use a static array, instead of a class member array. This prevents the array from being loaded into memory for each photo. If using PHP4, you have to remove the public I believe.