Refactored to use existing doctrine2 functionality.