Unverified Commit 130e158a authored by Sergei Morozov's avatar Sergei Morozov Committed by GitHub

Merge pull request #3851 from BenMorel/andx

Rename andX() / orX() methods
parents c2b8e6e8 84328cd9
# Upgrade to 2.11
## Deprecated `ExpressionBuilder` methods
The usage of the `andX()` and `orX()` methods of the `ExpressionBuilder` class has been deprecated. Use `and()` and `or()` instead.
# Upgrade to 2.10 # Upgrade to 2.10
## Deprecated `Doctrine\DBAL\Event\ConnectionEventArgs` methods ## Deprecated `Doctrine\DBAL\Event\ConnectionEventArgs` methods
......
...@@ -332,13 +332,13 @@ Most notably you can use expressions to build nested And-/Or statements: ...@@ -332,13 +332,13 @@ Most notably you can use expressions to build nested And-/Or statements:
->select('id', 'name') ->select('id', 'name')
->from('users') ->from('users')
->where( ->where(
$queryBuilder->expr()->andX( $queryBuilder->expr()->and(
$queryBuilder->expr()->eq('username', '?'), $queryBuilder->expr()->eq('username', '?'),
$queryBuilder->expr()->eq('email', '?') $queryBuilder->expr()->eq('email', '?')
) )
); );
The ``andX()`` and ``orX()`` methods accept an arbitrary amount The ``and()`` and ``or()`` methods accept an arbitrary amount
of arguments and can be nested in each other. of arguments and can be nested in each other.
There is a bunch of methods to create comparisons and other SQL snippets There is a bunch of methods to create comparisons and other SQL snippets
......
...@@ -39,13 +39,27 @@ class ExpressionBuilder ...@@ -39,13 +39,27 @@ class ExpressionBuilder
} }
/** /**
* Creates a conjunction of the given boolean expressions. * Creates a conjunction of the given expressions.
* *
* Example: * @param string|CompositeExpression ...$expressions Requires at least one defined when converting to string.
*/
public function and(...$expressions) : CompositeExpression
{
return new CompositeExpression(CompositeExpression::TYPE_AND, $expressions);
}
/**
* Creates a disjunction of the given expressions.
* *
* [php] * @param string|CompositeExpression ...$expressions Requires at least one defined when converting to string.
* // (u.type = ?) AND (u.role = ?) */
* $expr->andX('u.type = ?', 'u.role = ?')); public function or(...$expressions) : CompositeExpression
{
return new CompositeExpression(CompositeExpression::TYPE_OR, $expressions);
}
/**
* @deprecated Use `and()` instead.
* *
* @param mixed $x Optional clause. Defaults = null, but requires * @param mixed $x Optional clause. Defaults = null, but requires
* at least one defined when converting to string. * at least one defined when converting to string.
...@@ -54,17 +68,11 @@ class ExpressionBuilder ...@@ -54,17 +68,11 @@ class ExpressionBuilder
*/ */
public function andX($x = null) public function andX($x = null)
{ {
return new CompositeExpression(CompositeExpression::TYPE_AND, func_get_args()); return $this->and(...func_get_args());
} }
/** /**
* Creates a disjunction of the given boolean expressions. * @deprecated Use `or()` instead.
*
* Example:
*
* [php]
* // (u.type = ?) OR (u.role = ?)
* $qb->where($qb->expr()->orX('u.type = ?', 'u.role = ?'));
* *
* @param mixed $x Optional clause. Defaults = null, but requires * @param mixed $x Optional clause. Defaults = null, but requires
* at least one defined when converting to string. * at least one defined when converting to string.
...@@ -73,7 +81,7 @@ class ExpressionBuilder ...@@ -73,7 +81,7 @@ class ExpressionBuilder
*/ */
public function orX($x = null) public function orX($x = null)
{ {
return new CompositeExpression(CompositeExpression::TYPE_OR, func_get_args()); return $this->or(...func_get_args());
} }
/** /**
......
...@@ -29,11 +29,11 @@ class ExpressionBuilderTest extends DbalTestCase ...@@ -29,11 +29,11 @@ class ExpressionBuilderTest extends DbalTestCase
/** /**
* @param string[]|CompositeExpression[] $parts * @param string[]|CompositeExpression[] $parts
* *
* @dataProvider provideDataForAndX * @dataProvider provideDataForAnd
*/ */
public function testAndX(array $parts, string $expected) : void public function testAnd(array $parts, string $expected) : void
{ {
$composite = $this->expr->andX(); $composite = $this->expr->and();
foreach ($parts as $part) { foreach ($parts as $part) {
$composite->add($part); $composite->add($part);
...@@ -45,7 +45,7 @@ class ExpressionBuilderTest extends DbalTestCase ...@@ -45,7 +45,7 @@ class ExpressionBuilderTest extends DbalTestCase
/** /**
* @return mixed[][] * @return mixed[][]
*/ */
public static function provideDataForAndX() : iterable public static function provideDataForAnd() : iterable
{ {
return [ return [
[ [
...@@ -90,11 +90,11 @@ class ExpressionBuilderTest extends DbalTestCase ...@@ -90,11 +90,11 @@ class ExpressionBuilderTest extends DbalTestCase
/** /**
* @param string[]|CompositeExpression[] $parts * @param string[]|CompositeExpression[] $parts
* *
* @dataProvider provideDataForOrX * @dataProvider provideDataForOr
*/ */
public function testOrX(array $parts, string $expected) : void public function testOr(array $parts, string $expected) : void
{ {
$composite = $this->expr->orX(); $composite = $this->expr->or();
foreach ($parts as $part) { foreach ($parts as $part) {
$composite->add($part); $composite->add($part);
...@@ -106,7 +106,7 @@ class ExpressionBuilderTest extends DbalTestCase ...@@ -106,7 +106,7 @@ class ExpressionBuilderTest extends DbalTestCase
/** /**
* @return mixed[][] * @return mixed[][]
*/ */
public static function provideDataForOrX() : iterable public static function provideDataForOr() : iterable
{ {
return [ return [
[ [
......
...@@ -68,7 +68,7 @@ class QueryBuilderTest extends DbalTestCase ...@@ -68,7 +68,7 @@ class QueryBuilderTest extends DbalTestCase
$qb->select('u.id') $qb->select('u.id')
->from('users', 'u') ->from('users', 'u')
->where($expr->andX($expr->eq('u.nickname', '?'))); ->where($expr->and($expr->eq('u.nickname', '?')));
self::assertEquals('SELECT u.id FROM users u WHERE u.nickname = ?', (string) $qb); self::assertEquals('SELECT u.id FROM users u WHERE u.nickname = ?', (string) $qb);
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment