Doctrine2: Best way to handle many-to-many with extra columns in reference table

后端 未结 14 2192
灰色年华
灰色年华 2020-11-22 10:44

I\'m wondering what\'s the best, the cleanest and the most simply way to work with many-to-many relations in Doctrine2.

Let\'s assume that we\'ve got an album like

14条回答
  •  旧时难觅i
    2020-11-22 11:33

    You may be able to achieve what you want with Class Table Inheritance where you change AlbumTrackReference to AlbumTrack:

    class AlbumTrack extends Track { /* ... */ }
    

    And getTrackList() would contain AlbumTrack objects which you could then use like you want:

    foreach($album->getTrackList() as $albumTrack)
    {
        echo sprintf("\t#%d - %-20s (%s) %s\n", 
            $albumTrack->getPosition(),
            $albumTrack->getTitle(),
            $albumTrack->getDuration()->format('H:i:s'),
            $albumTrack->isPromoted() ? ' - PROMOTED!' : ''
        );
    }
    

    You will need to examine this throughly to ensure you don't suffer performance-wise.

    Your current set-up is simple, efficient, and easy to understand even if some of the semantics don't quite sit right with you.

提交回复
热议问题