-
Notifications
You must be signed in to change notification settings - Fork 645
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Set default element query
select
and orderBy
params will callbacks
- Loading branch information
1 parent
272f0a2
commit db3de0f
Showing
6 changed files
with
220 additions
and
81 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
<?php | ||
/** | ||
* @link https://craftcms.com/ | ||
* @copyright Copyright (c) Pixel & Tonic, Inc. | ||
* @license https://craftcms.github.io/license/ | ||
*/ | ||
|
||
namespace craft\db; | ||
|
||
use yii\base\BaseObject; | ||
use yii\db\ExpressionInterface; | ||
|
||
/** | ||
* CallbackExpression represents a DB expression where the SQL won’t get generated until execution time. | ||
* | ||
* Usage: | ||
* | ||
* ```php | ||
* $query->andWhere( | ||
* new CallbackExpression(function(array &$params): string { | ||
* $params['foo'] = 'bar'; | ||
* return '[[column_name]] = :foo'; | ||
* }) | ||
* ); | ||
* ``` | ||
* | ||
* @author Pixel & Tonic, Inc. <[email protected]> | ||
* @since 5.0.0 | ||
*/ | ||
class CallbackExpression extends BaseObject implements ExpressionInterface | ||
{ | ||
/** | ||
* Constructor | ||
* | ||
* @param callable $callback the DB expression callback | ||
* @param array $config name-value pairs that will be used to initialize the object properties | ||
*/ | ||
public function __construct( | ||
public $callback, | ||
array $config = [], | ||
) { | ||
parent::__construct($config); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
<?php | ||
/** | ||
* @link https://craftcms.com/ | ||
* @copyright Copyright (c) Pixel & Tonic, Inc. | ||
* @license https://craftcms.github.io/license/ | ||
*/ | ||
|
||
namespace craft\db; | ||
|
||
use yii\base\NotSupportedException; | ||
use yii\db\ExpressionBuilderInterface; | ||
use yii\db\ExpressionBuilderTrait; | ||
use yii\db\ExpressionInterface; | ||
|
||
/** | ||
* CallbackExpressionBuilder builds a callback expression | ||
* | ||
* @author Pixel & Tonic, Inc. <[email protected]> | ||
* @since 5.0.0 | ||
*/ | ||
class CallbackExpressionBuilder implements ExpressionBuilderInterface | ||
{ | ||
use ExpressionBuilderTrait; | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
public function build(ExpressionInterface $expression, array &$params = []) | ||
{ | ||
if (!$expression instanceof CallbackExpression) { | ||
throw new NotSupportedException('$expression must be an instance of CallbackExpression.'); | ||
} | ||
|
||
return call_user_func($expression->callback, $this->queryBuilder, $params); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.