We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

Album-o-rama (display a vertical list of all the tags)

Hi everyone! Can anyone help) need to display a vertical list of all the tags .... and for each tag that is a list of artists with tag ... that's something like this: Code took away https://album-o-rama.phalcon.io/

Tag1 Artist1, Artist52, Artist 70

Tag2 Artist9, Artist3, Artist 1, Artist 109

Tag3 Artist41, Artist22 **

Thank you so much for your help!



43.9k
edited Jul '14

Hi,

Tags and Artists have a many to many relation through table ArtistsTags Model Class. For achieving what you want, you have to complete the relations in Tags Model:

$this->hasMany('id', 'AlbumOrama\Models\ArtistsTags', 'tags_id',array('alias'=>'TaggedArtists'));

This relation will give you all the related artists for a given tag. Now, you can do an

$tags=Tags::find(); //get all the tags

loop through this tag resultset with a "foreach":

foreach($tags as $tag) 
{ // now you have to loop through all the artist tagged with $tag
    foreach($tag->TaggedArtists as $artist) // you can pass $tag->TaggedArtists to the view and make this second loop in the view file
    {
        echo $artist->name;
    }
}