Fixed #381. Ported propelorm/propel#562

Fixed #381.
Ported propelorm/propel#562 and propelorm/propel#618