Select::WhereAdd

Add a field to filter the search

Sintaxe

namespace ProtocolLive\PhpLiveDb;
use ProtocolLive\PhpLiveDb\Enums\{
};

/**

 */
final class Select{
public function WhereAdd(
string|string[]|UnitEnum|UnitEnum[] $Field,
string $Value = null,
Types $Type = null,
Operators $Operator = Operators::Equal,
AndOr $AndOr = AndOr::And,
Parenthesis $Parenthesis = Parenthesis::None,
string $CustomPlaceholder = null,
string|int $Value2 = null,
bool $BlankIsNull = true,
bool $NoField = false,
bool $NoBind = false,
bool $Debug = false
):self|array|false;
}
Parameters

$Field - Field name. Can be null to only add Parenthesis;

$Value - The field value;

$Type - The field type;

$Operator - The comparing operator;

$AndOr - The relation with the previous field;

$Parenthesis - The parenthesis operator;

$CustomPlaceholder - String to use as placeholder;

$Value2 - A second value. Used with Operators::Between;

$BlankIsNull - Replace '' to null;

$NoField - To bind a placeholder previously indicated;

$NoBind - Don't bind in that call. Used with Operators::Sql or same value for different or same field. (Changed to true if Operators::Sql are used)

$Debug - Dump the command debug;

Notes

- When the $Value is null, the Type are changed to Types::Null;

- When the $Operator is Operators::Sql, the $NoBind are changed to true;

- When using a SQL expression in $Field, its mandatory to use the $CustomPlaceholder;

See more

- AndOr

- Operators

- Parenthesis

- Types

Examples

Select with one condition:
$consult = $Db->Select('users');
$consult->WhereAdd(
'email',
$_POST['email'],
Types::Str
);
$consult->Run();
//select * from users where email=:email
//select * from users where email='foo@bar.com'
Select with more conditions:
$consult = $Db->Select('users');
$consult->WhereAdd(
'email',
$_POST['email'],
Types::Str
);
$consult->WhereAdd(
'active',
1,
Types::Int
);
$consult->Run();
//select * from users where email=:email and active=:active
//select * from users where email='foo@bar.com' and active=1
Select with "Or" and "CustomPlaceholder":
$consult = $Db->Select('users');
$consult->WhereAdd(
'type',
1,
Types::Int
);
$consult->WhereAdd(
'type',
2,
Types::Int,
AndOr: AndOr::Or,
CustomPlaceholder: 'type2'
);
$consult->Run();
//select * from users where type=:type or type=:type2
//select * from users where type=1 or type=2

Operators, Parenthesis, CustomPlaceholder and NoBind:
$consult = $Db->Select('users');
$consult->WhereAdd(
'active',
true,
Types::Bool
);
$consult->WhereAdd(
'name',
'%' . $_POST['text'] . '%',
Types::Str,
Operator: Operators::Like,
Parenthesis: Parenthesis::Open,
);
$consult->WhereAdd(
'email',
Operator: Operators::Like,
AndOr: AndOr::Or,
Parenthesis: Parenthesis::Close,
CustomPlaceholder: 'name',
NoBind: true
);
$consult->Run();
//select * from users where active=:active and (name like :name or email like :name)
//select * from users where active=1 and (name like '%foo%' or email like '%foo%')
Select with like operator in many fields:
$consult = $Db->Select('users')
->WhereAdd(
['email', 'name', 'street'],
$_POST['value'],
Types::Str,
Operators::Like,
AndOr::Or,
CustomPlaceholder: 'value'
)
->Run();
//select * from users where email like :value or name like :value or street like :value
//select * from users where email like '%bob%' or name like '%bob%' or street like '%bob%'