title: How to handle one to many relationships in an object orientated way?
body: <p>I'm building a database of artists, I would like to update/create the genres played by the artist in an object oriented way. I currently have 3 tables:</p>
  
  <p><strong>Table name:artists</strong></p>
  
  <p><a href="https://i.stack.imgur.com/C3Ofb.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/C3Ofb.png" alt="enter image description here"></a></p>
  
  <p>I created a genres table which consists of 4 possible genres that artists can perform: Classical, Hiphop, Jazz and Other. </p>
  
  <p><strong>Table name: genres</strong></p>
  
  <p><a href="https://i.stack.imgur.com/cqXZm.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/cqXZm.png" alt="enter image description here"></a></p>
  
  <p>Each artist has a one-to-many relationship with genres:</p>
  
  <p><strong>Table name: artist_genres</strong></p>
  
  <p><a href="https://i.stack.imgur.com/cqXZm.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/1R30y.png" alt="enter image description here"></a></p>
  
  <p>What I want is to output the genres played by the artist in the following form:
  <code>$artist-&gt;genres</code> will output all genres, for I'd have a function in the artist class like:</p>
  
  <pre><code>public static function find_genres_by_id($id=0){
    }
  </code></pre>
  
  <p>The above will only give me ids which I will need to look up in the genres table. The problem is that the above is not an array and I can't use it directly. However, I came up with the following code to do what I want:</p>
  
  <pre><code>global $database;
  $sql = "SELECT genre_id FROM artists JOIN artist_genres ON artists.id=artist_genres.artist_id WHERE artist_id=8";
  $answer = $database-&gt;query($sql);
  while ($row = $answer-&gt;fetch_assoc()) {
  $genresql = "SELECT genre_name FROM genres WHERE id=".$row['genre_id'];
  $genrelist = $database-&gt;query($genresql);
  while ($genrerow = $genrelist-&gt;fetch_assoc()){
    echo $genrerow['genre_name']."&lt;br&gt;";
  }
  </code></pre>
  
  <p>This simply outputs the genres, but I want to be able to be able to instantiate and use a simpler <code>echo $artist-&gt;genres</code> to output the genres. I can currently do this for <code>$artist-&gt;id</code> , <code>$artist-&gt;first_name</code> etc. after using <code>$artist=Artist::find_by_id($id)</code> </p>
  
  <hr>
  
  <hr>
  
  <hr>
  
  <hr>
  
  <hr>
  
  <hr>
  
  <p>EXTRA(I HAVE THE FOLLOWING FUNCTIONS):</p>
  
  <pre><code>class DatabaseObject {
  
    public static function find_available_artists() {
      return static::find_by_sql("SELECT * FROM artists WHERE availability=1");
    }
    public static function find_genres_by_id($id=0){
      return static::find_by_sql("SELECT genre_id FROM allartists JOIN artist_genres ON allartists.id=artist_genres.artist_id WHERE artist_id={$id}");
    }
  
    public static function find_all() {
      return static::find_by_sql("SELECT * FROM ".static::$table_name);
    }
  
    public static function find_by_id($id=0) {
      $result_array = static::find_by_sql("SELECT * FROM ".static::$table_name." WHERE id={$id} LIMIT 1");
      return !empty($result_array) ? array_shift($result_array) : false;
    }
     public static function find_by_sql($sql=""){
  
       global $database;
       $result_set = $database-&gt;query($sql);
       $object_array = array();
       while ($row = $database-&gt;fetch_array($result_set)) {
         $object_array[]=static::instantiate($row);
       }
       return $object_array;
     }
  
  
     private static function instantiate($record){
     $class_name = get_called_class();
     $object = new $class_name;
  
     foreach($record as $attribute=&gt;$value) {
      if($object-&gt;has_attribute($attribute)) {
        $object-&gt;$attribute = $value;
      }
  
     }
     return $object;
     }
     private function has_attribute($attribute) {
       $object_vars = get_object_vars($this);
       return array_key_exists($attribute, $object_vars);
  
     }
  </code></pre>