proper fix for #9425, adjust changes from #11363

includes tests and fix that works in all cases.
This commit is contained in:
Carsten Brandt
2016-04-28 01:29:07 +02:00
parent 9ea5d46140
commit 4411a74ebd
6 changed files with 25 additions and 18 deletions

View File

@@ -235,6 +235,7 @@ class MigrateController extends BaseMigrateController
/**
* @inheritdoc
* @since 2.0.8
*/
protected function generateMigrationSourceCode($params)
{
@@ -312,6 +313,7 @@ class MigrateController extends BaseMigrateController
*
* @param string $tableName the table name to generate.
* @return string
* @since 2.0.8
*/
protected function generateTableName($tableName)
{

View File

@@ -789,17 +789,4 @@ class ActiveQuery extends Query implements ActiveQueryInterface
}
return $this;
}
/**
* @inheritdoc
*/
public function exists($db = null)
{
if ($db === null) {
/* @var $modelClass ActiveRecord */
$modelClass = $this->modelClass;
$db = $modelClass::getDb();
}
return parent::exists($db);
}
}

View File

@@ -360,10 +360,10 @@ class Query extends Component implements QueryInterface
*/
public function exists($db = null)
{
if ($db === null) {
$db = Yii::$app->getDb();
}
$command = $db->createCommand($db->getQueryBuilder()->selectExists($this->createCommand()->getRawSql()));
$command = $this->createCommand($db);
$params = $command->params;
$command->setSql($command->db->getQueryBuilder()->selectExists($command->getSql()));
$command->bindValues($params);
return (boolean)$command->queryScalar();
}

View File

@@ -1408,7 +1408,6 @@ class QueryBuilder extends \yii\base\Object
* Creates a SELECT EXISTS() SQL statement.
* @param string $rawSql the subquery in a raw form to select from.
* @return string the SELECT EXISTS() SQL statement.
*
* @since 2.0.8
*/
public function selectExists($rawSql)

View File

@@ -97,6 +97,14 @@ class ActiveRecordTest extends DatabaseTestCase
$this->assertEquals('user2', $customerName);
}
public function testFindExists()
{
$this->assertTrue(Customer::find()->where(['[[id]]' => 2])->exists());
$this->assertFalse(Customer::find()->where(['[[id]]' => 42])->exists());
$this->assertTrue(Customer::find()->where(['[[id]]' => 2])->select('[[name]]')->exists());
$this->assertFalse(Customer::find()->where(['[[id]]' => 42])->select('[[name]]')->exists());
}
public function testFindColumn()
{
/* @var $this TestCase|ActiveRecordTestTrait */

View File

@@ -214,6 +214,17 @@ class QueryTest extends DatabaseTestCase
$this->assertFalse($result);
}
public function testExists()
{
$db = $this->getConnection();
$result = (new Query)->from('customer')->where(['status' => 2])->exists($db);
$this->assertTrue($result);
$result = (new Query)->from('customer')->where(['status' => 3])->exists($db);
$this->assertFalse($result);
}
public function testColumn()
{
$db = $this->getConnection();