public static function getInstance($repository, $conditions)
    {
        if (!$conditions instanceof self) {
            if ($repository instanceof self) {
                $query = clone $repository;
                $repository = $query->getRepository();
            } else {
                $query = $repository->getQuery();
            }

            $identity_property = $repository->getDescription()->getIdentityProperty();

            if (!empty($conditions)) {
                if (is_numeric($conditions)) {
                    $conditions = array($conditions);
                }

                //if conditions is number of an array of numbers
                if (is_numeric($conditions) ||
                        (is_array($conditions) && is_numeric(key($conditions)))) {
                    $conditions = array($identity_property->getName() => $conditions);
                }

                $query->where($conditions);
            }
        } else {
            //clone the query to avoid changing the passed query
            $query = clone $conditions;
        }

        return $query;
    }