mirror of
https://github.com/yiisoft/yii2.git
synced 2025-11-02 21:41:19 +08:00
Fixes #16022: Fix UniqueValidator for PostgreSQL. Checks the uniqueness of keys in jsonb field
This commit is contained in:
@ -4,6 +4,7 @@ Yii Framework 2 Change Log
|
||||
2.0.16 under development
|
||||
------------------------
|
||||
|
||||
- Bug #16022: Fix UniqueValidator for PostgreSQL. Checks the uniqueness of keys in `jsonb` field (lav45)
|
||||
- Bug #16903: Fixed 'yii\validators\NumberValidator' method 'isNotNumber' returns false for true/false value (annechko)
|
||||
- Bug #16648: Html::strtolower() was corrupting UTF-8 strings (Kolyunya)
|
||||
- Bug #13977: Skip validation if file input does not exist (RobinKamps, s1lver)
|
||||
|
||||
@ -178,7 +178,7 @@ class UniqueValidator extends Validator
|
||||
*/
|
||||
private function modelExists($targetClass, $conditions, $model)
|
||||
{
|
||||
/** @var ActiveRecordInterface $targetClass $query */
|
||||
/** @var ActiveRecordInterface|\yii\base\BaseObject $targetClass $query */
|
||||
$query = $this->prepareQuery($targetClass, $conditions);
|
||||
|
||||
if (!$model instanceof ActiveRecordInterface || $model->getIsNewRecord() || $model->className() !== $targetClass::className()) {
|
||||
@ -311,10 +311,12 @@ class UniqueValidator extends Validator
|
||||
$prefixedConditions = [];
|
||||
foreach ($conditions as $columnName => $columnValue) {
|
||||
if (strpos($columnName, '(') === false) {
|
||||
$prefixedColumn = "{$alias}.[[" . preg_replace(
|
||||
'/^' . preg_quote($alias) . '\.(.*)$/',
|
||||
'$1',
|
||||
$columnName) . ']]';
|
||||
$columnName = preg_replace('/^' . preg_quote($alias) . '\.(.*)$/', '$1', $columnName);
|
||||
if (strpos($columnName, '[[') === 0) {
|
||||
$prefixedColumn = "{$alias}.{$columnName}";
|
||||
} else {
|
||||
$prefixedColumn = "{$alias}.[[{$columnName}]]";
|
||||
}
|
||||
} else {
|
||||
// there is an expression, can't prefix it reliably
|
||||
$prefixedColumn = $columnName;
|
||||
|
||||
@ -26,6 +26,8 @@ namespace yiiunit\data\ar;
|
||||
*/
|
||||
class Type extends ActiveRecord
|
||||
{
|
||||
public $name;
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
|
||||
@ -7,6 +7,9 @@
|
||||
|
||||
namespace yiiunit\framework\db\pgsql;
|
||||
|
||||
use yii\validators\UniqueValidator;
|
||||
use yiiunit\data\ar\Type;
|
||||
|
||||
/**
|
||||
* @group db
|
||||
* @group pgsql
|
||||
@ -15,4 +18,19 @@ namespace yiiunit\framework\db\pgsql;
|
||||
class UniqueValidatorTest extends \yiiunit\framework\validators\UniqueValidatorTest
|
||||
{
|
||||
public $driverName = 'pgsql';
|
||||
|
||||
public function testPrepareParams()
|
||||
{
|
||||
parent::testPrepareParams();
|
||||
|
||||
// Add table prefix for column name
|
||||
$model = new Type;
|
||||
$model->name = 'Angela';
|
||||
|
||||
$attribute = 'name';
|
||||
$targetAttribute = [$attribute => "[[jsonb_col]]->>'name'"];
|
||||
$result = $this->invokeMethod(new UniqueValidator(), 'prepareConditions', [$targetAttribute, $model, $attribute]);
|
||||
$expected = ['{{' . Type::tableName() . '}}.' . $targetAttribute[$attribute] => $model->name];
|
||||
$this->assertEquals($expected, $result);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user