Unmachable criteria #5466

Open
opened 2026-01-22 15:08:27 +01:00 by admin · 1 comment
Owner

Originally created by @Isinlor on GitHub (Mar 20, 2017).

Hello,

I'm trying to replicate this logic with criteria (because database filtering is faster):

    /**
     * Checks whether the fragment makes a placement visible to the user.
     *
     * @param User $user
     *
     * @return bool Whether a placement in the fragment should be visible to the user.
     */
    public function makesPlacementVisible(User $user)
    {
        if ($user->isAdminLevel()) {
            return true;
        }

        if ($user->isStudent() && $this->makesPlacementVisibleToStudents()) {
            return true;
        }

        if ($user->isExternalUser() && $this->makesPlacementVisibleToExternalUsers()) {
            return true;
        }

        return false;
    }

Version with criteria:

    /**
     * Returns criteria that check whether the fragment makes placements visible to the user.
     *
     * @param User   $user
     * @param string $internshipFragmentAlias
     *
     * @return Criteria
     */
    public static function makesPlacementVisibleToUserCriteria(User $user, $internshipFragmentAlias)
    {
        $criteria = Criteria::create();

        switch (true) {
            
            case $user->isAdminLevel():

                return $criteria; // access to everything

            // no break
            case $user->isStudent():

                $criteria->orWhere(
                    static::makesPlacementVisibleToStudentsCriteria($internshipFragmentAlias)->getWhereExpression()
                );

            // no break
            case $user->isExternalUser():

                $criteria->orWhere(
                    static::makesPlacementVisibleToExternalUsersCriteria($internshipFragmentAlias)->getWhereExpression()
                );

                break; // at least one user role was matched

            default:

                // no user role was matched, therefore no access
                // unmachable criteria (invalid code, showing what I want to achieve)
                $criteria = Criteria::create()
                    ->where(Criteria::expr()->eq(1, 0));


        }

        return $criteria;

    }

But I'm missing the default return false; case in version with criteria, because this is not a valid code:

                // unmachable criteria
                $criteria = Criteria::create()
                    ->where(Criteria::expr()->eq(1, 0));

Is there a way to create an "unmachable criteria"?

Thanks for help!

Originally created by @Isinlor on GitHub (Mar 20, 2017). Hello, I'm trying to replicate this logic with criteria (because database filtering is faster): ```php /** * Checks whether the fragment makes a placement visible to the user. * * @param User $user * * @return bool Whether a placement in the fragment should be visible to the user. */ public function makesPlacementVisible(User $user) { if ($user->isAdminLevel()) { return true; } if ($user->isStudent() && $this->makesPlacementVisibleToStudents()) { return true; } if ($user->isExternalUser() && $this->makesPlacementVisibleToExternalUsers()) { return true; } return false; } ``` Version with criteria: ```php /** * Returns criteria that check whether the fragment makes placements visible to the user. * * @param User $user * @param string $internshipFragmentAlias * * @return Criteria */ public static function makesPlacementVisibleToUserCriteria(User $user, $internshipFragmentAlias) { $criteria = Criteria::create(); switch (true) { case $user->isAdminLevel(): return $criteria; // access to everything // no break case $user->isStudent(): $criteria->orWhere( static::makesPlacementVisibleToStudentsCriteria($internshipFragmentAlias)->getWhereExpression() ); // no break case $user->isExternalUser(): $criteria->orWhere( static::makesPlacementVisibleToExternalUsersCriteria($internshipFragmentAlias)->getWhereExpression() ); break; // at least one user role was matched default: // no user role was matched, therefore no access // unmachable criteria (invalid code, showing what I want to achieve) $criteria = Criteria::create() ->where(Criteria::expr()->eq(1, 0)); } return $criteria; } ``` But I'm missing the default ` return false;` case in version with criteria, because this is not a valid code: ```php // unmachable criteria $criteria = Criteria::create() ->where(Criteria::expr()->eq(1, 0)); ``` Is there a way to create an "unmachable criteria"? Thanks for help!
Author
Owner

@porebskk commented on GitHub (Mar 22, 2017):

How about throwing an exception and catching it in the approriate level?

@porebskk commented on GitHub (Mar 22, 2017): How about throwing an exception and catching it in the approriate level?
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: doctrine/archived-orm#5466