This is much more complicated than you might think, but it is something that I've been wanting to do for about a year now. As the Doctrine conversion makes progress, I fully intend to work on implementing something very similar to (if not the same as) this.