mirror of
				https://github.com/yiisoft/yii2.git
				synced 2025-11-04 06:37:55 +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
 | 
					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 #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 #16648: Html::strtolower() was corrupting UTF-8 strings (Kolyunya)
 | 
				
			||||||
- Bug #13977: Skip validation if file input does not exist (RobinKamps, s1lver)
 | 
					- 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)
 | 
					    private function modelExists($targetClass, $conditions, $model)
 | 
				
			||||||
    {
 | 
					    {
 | 
				
			||||||
        /** @var ActiveRecordInterface $targetClass $query */
 | 
					        /** @var ActiveRecordInterface|\yii\base\BaseObject $targetClass $query */
 | 
				
			||||||
        $query = $this->prepareQuery($targetClass, $conditions);
 | 
					        $query = $this->prepareQuery($targetClass, $conditions);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        if (!$model instanceof ActiveRecordInterface || $model->getIsNewRecord() || $model->className() !== $targetClass::className()) {
 | 
					        if (!$model instanceof ActiveRecordInterface || $model->getIsNewRecord() || $model->className() !== $targetClass::className()) {
 | 
				
			||||||
@ -311,10 +311,12 @@ class UniqueValidator extends Validator
 | 
				
			|||||||
        $prefixedConditions = [];
 | 
					        $prefixedConditions = [];
 | 
				
			||||||
        foreach ($conditions as $columnName => $columnValue) {
 | 
					        foreach ($conditions as $columnName => $columnValue) {
 | 
				
			||||||
            if (strpos($columnName, '(') === false) {
 | 
					            if (strpos($columnName, '(') === false) {
 | 
				
			||||||
                $prefixedColumn = "{$alias}.[[" . preg_replace(
 | 
					                $columnName = preg_replace('/^' . preg_quote($alias) . '\.(.*)$/', '$1', $columnName);
 | 
				
			||||||
                    '/^' . preg_quote($alias) . '\.(.*)$/',
 | 
					                if (strpos($columnName, '[[') === 0) {
 | 
				
			||||||
                    '$1',
 | 
					                    $prefixedColumn = "{$alias}.{$columnName}";
 | 
				
			||||||
                    $columnName) . ']]';
 | 
					                } else {
 | 
				
			||||||
 | 
					                    $prefixedColumn = "{$alias}.[[{$columnName}]]";
 | 
				
			||||||
 | 
					                }
 | 
				
			||||||
            } else {
 | 
					            } else {
 | 
				
			||||||
                // there is an expression, can't prefix it reliably
 | 
					                // there is an expression, can't prefix it reliably
 | 
				
			||||||
                $prefixedColumn = $columnName;
 | 
					                $prefixedColumn = $columnName;
 | 
				
			||||||
 | 
				
			|||||||
@ -26,6 +26,8 @@ namespace yiiunit\data\ar;
 | 
				
			|||||||
 */
 | 
					 */
 | 
				
			||||||
class Type extends ActiveRecord
 | 
					class Type extends ActiveRecord
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
 | 
					    public $name;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    /**
 | 
					    /**
 | 
				
			||||||
     * {@inheritdoc}
 | 
					     * {@inheritdoc}
 | 
				
			||||||
     */
 | 
					     */
 | 
				
			||||||
 | 
				
			|||||||
@ -7,6 +7,9 @@
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
namespace yiiunit\framework\db\pgsql;
 | 
					namespace yiiunit\framework\db\pgsql;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					use yii\validators\UniqueValidator;
 | 
				
			||||||
 | 
					use yiiunit\data\ar\Type;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
/**
 | 
					/**
 | 
				
			||||||
 * @group db
 | 
					 * @group db
 | 
				
			||||||
 * @group pgsql
 | 
					 * @group pgsql
 | 
				
			||||||
@ -15,4 +18,19 @@ namespace yiiunit\framework\db\pgsql;
 | 
				
			|||||||
class UniqueValidatorTest extends \yiiunit\framework\validators\UniqueValidatorTest
 | 
					class UniqueValidatorTest extends \yiiunit\framework\validators\UniqueValidatorTest
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
    public $driverName = 'pgsql';
 | 
					    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