mirror of
				https://github.com/yiisoft/yii2.git
				synced 2025-11-04 06:37:55 +08:00 
			
		
		
		
	Fixes #15522: Fixed yii\db\ActiveRecord::refresh() method does not use an alias in the condition
				
					
				
			This commit is contained in:
		
				
					committed by
					
						
						Alexander Makarov
					
				
			
			
				
	
			
			
			
						parent
						
							b3130be7ba
						
					
				
				
					commit
					1a1fb49426
				
			@ -3,7 +3,7 @@ Yii Framework 2 Change Log
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
2.0.14 under development
 | 
					2.0.14 under development
 | 
				
			||||||
------------------------
 | 
					------------------------
 | 
				
			||||||
 | 
					- Bug #15522: Fixed `yii\db\ActiveRecord::refresh()` method does not use an alias in the condition (vladis84)
 | 
				
			||||||
- Enh #15476: Added `\yii\widgets\ActiveForm::$validationStateOn` to be able to specify where to add class for invalid fields (samdark)
 | 
					- Enh #15476: Added `\yii\widgets\ActiveForm::$validationStateOn` to be able to specify where to add class for invalid fields (samdark)
 | 
				
			||||||
- Enh #13996: Added `yii\web\View::registerJsVar()` method that allows registering JavaScript variables (Eseperio, samdark)
 | 
					- Enh #13996: Added `yii\web\View::registerJsVar()` method that allows registering JavaScript variables (Eseperio, samdark)
 | 
				
			||||||
- Enh #9771: Assign hidden input with its own set of HTML options via `$hiddenOptions` in activeFileInput `$options` (HanafiAhmat)
 | 
					- Enh #9771: Assign hidden input with its own set of HTML options via `$hiddenOptions` in activeFileInput `$options` (HanafiAhmat)
 | 
				
			||||||
 | 
				
			|||||||
@ -194,13 +194,17 @@ class ActiveRecord extends BaseActiveRecord
 | 
				
			|||||||
     */
 | 
					     */
 | 
				
			||||||
    public function refresh()
 | 
					    public function refresh()
 | 
				
			||||||
    {
 | 
					    {
 | 
				
			||||||
 | 
					        $query = static::find();
 | 
				
			||||||
 | 
					        $tableName = key($query->getTablesUsedInFrom());
 | 
				
			||||||
        $pk = [];
 | 
					        $pk = [];
 | 
				
			||||||
        // disambiguate column names in case ActiveQuery adds a JOIN
 | 
					        // disambiguate column names in case ActiveQuery adds a JOIN
 | 
				
			||||||
        foreach ($this->getPrimaryKey(true) as $key => $value) {
 | 
					        foreach ($this->getPrimaryKey(true) as $key => $value) {
 | 
				
			||||||
            $pk[static::tableName() . '.' . $key] = $value;
 | 
					            $pk[$tableName . '.' . $key] = $value;
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
 | 
					        $query->where($pk);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        /* @var $record BaseActiveRecord */
 | 
					        /* @var $record BaseActiveRecord */
 | 
				
			||||||
        $record = static::findOne($pk);
 | 
					        $record = $query->one();
 | 
				
			||||||
        return $this->refreshInternal($record);
 | 
					        return $this->refreshInternal($record);
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
				
			|||||||
							
								
								
									
										40
									
								
								tests/data/ar/CustomerWithAlias.php
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										40
									
								
								tests/data/ar/CustomerWithAlias.php
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,40 @@
 | 
				
			|||||||
 | 
					<?php
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					namespace yiiunit\data\ar;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/**
 | 
				
			||||||
 | 
					 * Class Customer.
 | 
				
			||||||
 | 
					 *
 | 
				
			||||||
 | 
					 * @property int $id
 | 
				
			||||||
 | 
					 * @property string $name
 | 
				
			||||||
 | 
					 * @property string $email
 | 
				
			||||||
 | 
					 * @property string $address
 | 
				
			||||||
 | 
					 * @property int $status
 | 
				
			||||||
 | 
					 *
 | 
				
			||||||
 | 
					 * @method CustomerQuery findBySql($sql, $params = []) static
 | 
				
			||||||
 | 
					 */
 | 
				
			||||||
 | 
					class CustomerWithAlias extends ActiveRecord
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
					    const STATUS_ACTIVE = 1;
 | 
				
			||||||
 | 
					    const STATUS_INACTIVE = 2;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    public $status2;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    public $sumTotal;
 | 
				
			||||||
 | 
					    
 | 
				
			||||||
 | 
					    public static function tableName()
 | 
				
			||||||
 | 
					    {
 | 
				
			||||||
 | 
					        return 'customer';
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					    
 | 
				
			||||||
 | 
					    /**
 | 
				
			||||||
 | 
					     * {@inheritdoc}
 | 
				
			||||||
 | 
					     * @return CustomerQuery
 | 
				
			||||||
 | 
					     */
 | 
				
			||||||
 | 
					    public static function find()
 | 
				
			||||||
 | 
					    {
 | 
				
			||||||
 | 
					        $activeQuery = new CustomerQuery(get_called_class());
 | 
				
			||||||
 | 
					        $activeQuery->alias('csr');
 | 
				
			||||||
 | 
					        return $activeQuery;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
@ -275,6 +275,16 @@ trait ActiveRecordTestTrait
 | 
				
			|||||||
        $this->assertEquals('user1', $customer->name);
 | 
					        $this->assertEquals('user1', $customer->name);
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    public function testRefresh_querySetAlias_findRecord()
 | 
				
			||||||
 | 
					    {
 | 
				
			||||||
 | 
					        $customer = new \yiiunit\data\ar\CustomerWithAlias();
 | 
				
			||||||
 | 
					        $customer->id = 1;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        $customer->refresh();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        $this->assertEquals(1, $customer->id);
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    public function testEquals()
 | 
					    public function testEquals()
 | 
				
			||||||
    {
 | 
					    {
 | 
				
			||||||
        /* @var $customerClass \yii\db\ActiveRecordInterface */
 | 
					        /* @var $customerClass \yii\db\ActiveRecordInterface */
 | 
				
			||||||
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user