mirror of
https://github.com/yiisoft/yii2.git
synced 2025-12-19 07:07:58 +08:00
includes tests and fix that works in all cases.
This commit is contained in:
@@ -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)
|
||||
{
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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();
|
||||
}
|
||||
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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 */
|
||||
|
||||
@@ -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();
|
||||
|
||||
Reference in New Issue
Block a user