private function updateTemplateOwner(EntityManager $em)
    {

        $em->getConnection()->executeUpdate('
            UPDATE Users
                INNER JOIN usr ON (
                    usr.usr_id = Users.id
                    AND usr.model_of IS NOT NULL
                    AND usr.model_of>0
                )
                SET Users.model_of = usr.model_of
        ');

        $em->getConnection()->executeUpdate('
            DELETE from Users
            WHERE id IN (
                SELECT id FROM (
                    SELECT templates.id
                    FROM Users users, Users templates
                    WHERE templates.model_of = users.id AND (users.deleted = 1 OR users.model_of IS NOT NULL)
                ) as temporaryTable
            )
        ');

        $em->getConnection()->executeUpdate('
            DELETE from Users
            WHERE id IN (
                SELECT id FROM (
                    SELECT templates.id
                    FROM Users templates
                    WHERE templates.model_of NOT IN (SELECT id FROM Users)
                ) as temporaryTable
            )
        ');

        $em->getConnection()->executeUpdate(' DELETE from Users WHERE deleted = 1 AND model_of IS NOT NULL');
    }