Advertisement
Omnikron13

Two Alternative Label->getTrackIDs() methods...

Sep 14th, 2014
216
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.23 KB | None | 0 0
  1. Label::add_method('getTrackIDs', function() {
  2.     $db = static::getDB();
  3.     return array_reduce(
  4.         array_map(function($id) use($db) {
  5.             $query = $db->prepare('SELECT id FROM tracks WHERE releaseID=:id;');
  6.             $query->bindValue(':id', $id, PDO::PARAM_INT);
  7.             $query->execute();
  8.             return array_map(function($id) {
  9.                 return intval($id);
  10.             }, $query->fetchAll(PDO::FETCH_COLUMN, 0));
  11.         }, $this->getReleaseIDs()),
  12.         function($a, $b) {
  13.             return array_merge($a, $b);
  14.         },
  15.         []
  16.     );
  17. });
  18.  
  19. Label::add_method('getTrackIDs', function() {
  20.     $rids = $this->getReleaseIDs();
  21.     $condition = implode(
  22.         ' OR ',
  23.         array_map(function($index, $id) {
  24.             return "releaseID=:id$index";
  25.         }, array_keys($rids), $rids)
  26.     );
  27.     if(!$condition)
  28.         return [];
  29.     $db = static::getDB();
  30.     $query = $db->prepare("SELECT id FROM tracks WHERE $condition;");
  31.     foreach($rids as $index => $id) {
  32.         $query->bindValue(":id$index", $id, PDO::PARAM_INT);
  33.     }
  34.     $query->execute();
  35.     return array_map(function($id) {
  36.         return intval($id);
  37.     }, $query->fetchAll(PDO::FETCH_COLUMN, 0));
  38. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement