/** * Update or create the database entries for each element of the given videos array * @param $videos: Array with all videos to update */ protected function updateVideoEntries($videos) { // Holder for all updated or newly created entries $processed = ArrayList::create(); // Loop over the given videos fetched from the API foreach ($videos as $video) { // Get the entry or create new one if (!($yv = YoutubeVideo::get()->filter('PlaylistItemID', $video->id)->first())) { $yv = new YoutubeVideo(); $yv->Title = $video->snippet->title; $yv->Description = $video->snippet->description; } $yv->PlaylistItemID = $video->id; $yv->VideoID = $video->contentDetails->videoId; $thumbs = $video->snippet->thumbnails->toMap(); $yv->ThumbnailURL = end($thumbs)->url; // Write to the db and store as processed item $yv->write(); $processed->push($yv); } /** * Loop over all video entries from the database * delete all entries, which are found in the db but not processed above */ foreach (YoutubeVideo::get() as $video) { if (!$processed->find('ID', $video->ID)) { $video->delete(); } } }
public function getYoutubeVideos() { return YoutubeVideo::get()->filter('Visible', 1); }