Generate dynamic query conditions with EasyStatement
masterUse EasyStatement to build complex, dynamic WHERE clauses. It supports grouping conditions and special placeholders for IN clauses.
// Basic dynamic conditions
$statement = EasyStatement::open()
->with('last_login IS NOT NULL');
if ($condition) {
$statement->orWith('username LIKE ?', '%' . $db->escapeLikeValue($val) . '%');
}
// Using the statement in a query
$user = $db->single("SELECT * FROM users WHERE $statement", $statement->values());
// Handling IN clauses with ?*
$roles = [1, 2];
$statement = EasyStatement::open()->in('role IN (?*)', $roles);
// Resulting SQL: role IN (?, ?)
// Grouping conditions
$statement = EasyStatement::open()
->group()
->with('subtotal > ?')
->andWith('taxes > ?')
->end()
->orGroup()
->with('cost > ?')
->andWith('cancelled = 1')
->end();
// Resulting SQL: (subtotal > ? AND taxes > ?) OR (cost > ? AND cancelled = 1)