public function testFindBySql() { // find one $customer = Customer::findBySql('SELECT * FROM customer ORDER BY id DESC')->one(); $this->assertTrue($customer instanceof Customer); $this->assertEquals('user3', $customer->name); // find all $customers = Customer::findBySql('SELECT * FROM customer')->all(); $this->assertEquals(3, count($customers)); // find with parameter binding $customer = Customer::findBySql('SELECT * FROM customer WHERE id=:id', [':id' => 2])->one(); $this->assertTrue($customer instanceof Customer); $this->assertEquals('user2', $customer->name); }
/** * @depends testFindBySql * * @see https://github.com/yiisoft/yii2/issues/8593 */ public function testCountWithFindBySql() { $query = Customer::findBySql('SELECT * FROM {{customer}}'); $this->assertEquals(3, $query->count()); $query = Customer::findBySql('SELECT * FROM {{customer}} WHERE [[id]]=:id', [':id' => 2]); $this->assertEquals(1, $query->count()); }