Skip to content

Handling internal CakePHP queries #2

Description

@miftygusein

Here's an example: https://api.cakephp.org/5.0/class-Cake.ORM.Table.html#exists()

Checks uniqueness of a given value in a given column

A case to handle would be a Users table rule checking username uniqueness before signing up.

public function buildRules(RulesChecker $rules): RulesChecker
{
    $rules->add($rules->isUnique(['username']), ['errorField' => 'username']);
}

The built-in CakePHP validator needs to be able to check whether username was taken across the whole table, without scoping search to a specific account. Therefore, TenantScopeBehavior needs to recognise and ignore such queries.

Here's the current source code for Cake\ORM\Table::exists().

public function exists(QueryExpression|Closure|array|string|null $conditions): bool
{
    return (bool)count(
        $this->find('all')
        ->select(['existing' => 1])
        ->where($conditions)
        ->limit(1)
        ->disableHydration()
        ->toArray()
    );
}

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions