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:
Table name:artists
I created a genres table which consists of 4 possible genres that artists can perform: Classical, Hiphop, Jazz and Other.
Table name: genres
Each artist has a one-to-many relationship with genres:
Table name: artist_genres
What I want is to output the genres played by the artist in the following form: $artist->genres
will output all genres, for I'd have a function in the artist class like:
public static function find_genres_by_id($id=0){
}
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:
global $database;
$sql = "SELECT genre_id FROM artists JOIN artist_genres ON artists.id=artist_genres.artist_id WHERE artist_id=8";
$answer = $database->query($sql);
while ($row = $answer->fetch_assoc()) {
$genresql = "SELECT genre_name FROM genres WHERE id=".$row['genre_id'];
$genrelist = $database->query($genresql);
while ($genrerow = $genrelist->fetch_assoc()){
echo $genrerow['genre_name']."<br>";
}
This simply outputs the genres, but I want to be able to be able to instantiate and use a simpler echo $artist->genres
to output the genres. I can currently do this for $artist->id
, $artist->first_name
etc. after using $artist=Artist::find_by_id($id)
EXTRA(I HAVE THE FOLLOWING FUNCTIONS):
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->query($sql);
$object_array = array();
while ($row = $database->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=>$value) {
if($object->has_attribute($attribute)) {
$object->$attribute = $value;
}
}
return $object;
}
private function has_attribute($attribute) {
$object_vars = get_object_vars($this);
return array_key_exists($attribute, $object_vars);
}
Does your artist entity class have a "genres" field, an array, which you can add to?
<?php
class Artist
{
private $genres;
public function addToGenres($genre)
{
$this->genres[] = $genre;
}
}