This is a data model from a site I built a few years back and am migrating to SS 3.1.8.
Artist.
Artwork. Has many Artists.
Exhibition. Has many Artworks.
There are two queries that are really slow because I’m a much better designer than I am a programmer. It gets very slow because the second method runs a loop, repeatedly calling the first method, which in turn runs its own loop… so the site is pretty fast apart from any page that uses the output of these functions.
Can these either be written to be faster (perhaps by getting rid of the ugly loops somehow), or could the results of the queries be cached somehow?
Get the artists in an exhibition by doing the following in Exhibition.php:
public function Artists() {
$artists = new ArrayList();
$artworks = $this->Artworks();
// loop through artworks and retrieve each related artist.
// return a dataobjectset that contains each artist only once.
foreach($artworks as $artwork) {
$next_artists = $artwork->Artists();
if($next_artists) {
foreach($next_artists as $check_artist) {
if(!$artists->byID($check_artist->ID)) {
$artists->push($check_artist);
}
}
}
}
return $artists;
}
Get every Exhibition an Artist has been in by doing this in Artist.php:
public function Exhibitions() {
$return = new ArrayList();
$exhibitions = DataObject::get('Exhibition');
foreach($exhibitions as $exhibition) {
$artists = $exhibition->Artists();
if($artists->byID($this->ID) && !$return->byID($exhibition->ID)) {
$return->add($exhibition);
}
}
return $return;
}