I have a schema for some custom tables:
(lots of rows removed for readability)
<?xml version="1.0" encoding="UTF-8"?>
<model package="extdata" baseClass="xPDOObject" platform="mysql" defaultEngine="MyISAM" version="1.1">
<!-- artist table definition related -->
<object class="ArtistAttributes" table="artist_attributes" extends="xPDOSimpleObject">
<field key="userkey" dbtype="int" precision="10" attributes="unsigned" phptype="integer" null="false" default="" />
<!-- a bunch of fields -->
<composite alias="ArtistDiscography" class="ArtistDiscography" local="id" foreign="artistkey" cardinality="many" owner="local" />
<composite alias="ArtistGenremap" class="ArtistGenremap" local="id" foreign="artistkey" cardinality="many" owner="local" />
<composite alias="ArtistGigs" class="ArtistGigs" local="id" foreign="artistkey" cardinality="many" owner="local" />
<composite alias="ArtistAudio" class="ArtistAudio" local="id" foreign="artistkey" cardinality="many" owner="local" />
<composite alias="ArtistMedia" class="ArtistMedia" local="id" foreign="artistkey" cardinality="many" owner="local" />
<composite alias="ArtistsManagers" class="ArtistsManagers" local="id" foreign="artistkey" cardinality="many" owner="local" />
</object>
<!-- an artist may belong to many genres -->
<object class="ArtistGenremap" table="artist_genremap" extends="xPDOSimpleObject">
<field key="artistkey" dbtype="int" precision="10" phptype="integer" null="false" default="0" />
<field key="genreskey" dbtype="int" precision="10" phptype="integer" null="false" default="0" />
<aggregate alias="ArtistAttributes" class="ArtistAttributes" local="artistkey" foreign="id" cardinality="one" owner="foreign" />
<aggregate alias="ArtistGenres" class="ArtistGenres" local="genreskey" foreign="id" cardinality="one" owner="foreign" />
</object>
<!-- list of genre names -->
<object class="ArtistGenres" table="artist_genres" extends="xPDOSimpleObject">
<field key="genre" dbtype="varchar" precision="100" phptype="string" null="false" default="" />
<aggregate alias="ArtistGenremap" class="ArtistGenremap" local="id" foreign="genreskey" cardinality="many" owner="local" />
</object>
<!-- artist mp3s an artist may have many mp3 instances -->
<object class="ArtistAudio" table="artist_audio" extends="xPDOSimpleObject">
<aggregate alias="ArtistAttributes" class="ArtistAttributes" local="artistkey" foreign="id" cardinality="one" owner="foreign" />
</object>
<!-- an artist may have many albums -->
<object class="ArtistDiscography" table="artist_discography" extends="xPDOSimpleObject">
<aggregate alias="ArtistAttributes" class="ArtistAttributes" local="artistkey" foreign="id" cardinality="one" owner="foreign" />
</object>
<!-- artist media an artist may have many media instances -->
<object class="ArtistMedia" table="artist_media" extends="xPDOSimpleObject">
<aggregate alias="ArtistAttributes" class="ArtistAttributes" local="artistkey" foreign="id" cardinality="one" owner="foreign" />
<aggregate alias="ArtistMediatypes" class="ArtistMediatypes" local="mediatypekey" foreign="id" cardinality="one" owner="foreign" />
</object>
<!-- list of media types -->
<object class="ArtistMediatypes" table="artist_mediatypes" extends="xPDOSimpleObject">
<aggregate alias="ArtistMedia" class="ArtistMedia" local="id" foreign="artistkey" cardinality="many" owner="local" />
</object>
<!-- artist gigs - an artist may have many gigs -->
<object class="ArtistGigs" table="artist_gigs" extends="xPDOSimpleObject">
<aggregate alias="ArtistAttributes" class="ArtistAttributes" local="artistkey" foreign="id" cardinality="one" owner="foreign" />
</object>
<!-- manager info a manager may belong to many artists -->
<object class="ArtistsManagers" table="artists_managers" extends="xPDOSimpleObject">
<aggregate alias="ArtistAttributes" class="ArtistAttributes" local="artistkey" foreign="id" cardinality="one" owner="foreign" />
</object>
</model>
And I am trying to get a list of genres that a particular artist belongs to, the genremap table just maps artist ids to genre ids in the genre's table [which just contains the genre name & an id]
So, it seems to me that I should be able to retrieve a list of genre names that a particular artist belongs to from the artist object:
$artist = $this->modx->getObject('ArtistAttributes', array('userkey'=>$id));
Is this possible? Once I grab the artist object, aren't the genre names that artist belongs to in that object because of the ay the tables are related?
If so, how?
If not, I was working on trying to query the tables and not getting far, how can I make this work? [how can I grab a list of genre names that particular artist belongs to?]
$query = $modx->newQuery('ArtistGenremap');
$query->select($modx->getSelectColumns('ArtistGenremap','ArtistGenremap','',array('id','genreskey','artistkey')));
$query->select(array(
'ArtistGenres.genreskey'
));
$query->where(array('artistkey' => $aid));
$query->leftJoin('ArtistGenres','ArtistGenres');
$genres = $modx->getCollection('ArtistGenremap',$query);
foreach($genres as $genre){
//echo $genre->get('id').'
';
echo $genre->get('genreskey').'
';
echo $genre->get('artistkey').'
';
echo $genre->get('genre').'
';
echo '
';
}
That does not work, what am I doing wrong?
-thanks
-sean