public function Caption()
 {
     //TODO: Make this more generic and not require a db query each time
     $controller = Controller::curr();
     $page = $controller->data();
     $joinObj = GalleryPage_Images::get()->where("\"GalleryPageID\" = '{$page->ID}' AND \"ImageID\" = '{$this->owner->ID}'")->first();
     return $joinObj->Caption;
 }
 protected function attachFile($file)
 {
     $record = $this->getRecord();
     $name = $this->getName();
     if ($record && $record->exists()) {
         if ($record->has_many($name) || $record->many_many($name)) {
             if (!$record->isInDB()) {
                 $record->write();
             }
             //Set the sort order first time image is attached
             $top = GalleryPage_Images::get()->where("\"GalleryPageID\" = '{$record->ID}'")->max('SortOrder');
             $top = is_numeric($top) ? $top + 1 : 1;
             $record->{$name}()->add($file, array('SortOrder' => $top));
         } elseif ($record->has_one($name)) {
             $record->{$name . 'ID'} = $file->ID;
             $record->write();
         }
     }
 }