|
|
@ -92,16 +92,18 @@ class ActiveRecordTest extends ElasticSearchTestCase |
|
|
|
// $orderItem->setAttributes(array('order_id' => 3, 'item_id' => 2, 'quantity' => 1, 'subtotal' => 40.0), false); |
|
|
|
// $orderItem->setAttributes(array('order_id' => 3, 'item_id' => 2, 'quantity' => 1, 'subtotal' => 40.0), false); |
|
|
|
// $orderItem->save(false); |
|
|
|
// $orderItem->save(false); |
|
|
|
|
|
|
|
|
|
|
|
for($n = 0; $n < 20; $n++) { |
|
|
|
Customer::getDb()->createCommand()->flushIndex(); |
|
|
|
$r = $db->http()->post('_count')->send(); |
|
|
|
|
|
|
|
$c = Json::decode($r->getBody(true)); |
|
|
|
// for($n = 0; $n < 20; $n++) { |
|
|
|
if ($c['count'] != 11) { |
|
|
|
// $r = $db->http()->post('_count')->send(); |
|
|
|
usleep(100000); |
|
|
|
// $c = Json::decode($r->getBody(true)); |
|
|
|
} else { |
|
|
|
// if ($c['count'] != 11) { |
|
|
|
return; |
|
|
|
// usleep(100000); |
|
|
|
} |
|
|
|
// } else { |
|
|
|
} |
|
|
|
// return; |
|
|
|
throw new \Exception('Unable to initialize elasticsearch data.'); |
|
|
|
// } |
|
|
|
|
|
|
|
// } |
|
|
|
|
|
|
|
// throw new \Exception('Unable to initialize elasticsearch data.'); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public function testFind() |
|
|
|
public function testFind() |
|
|
@ -127,22 +129,24 @@ class ActiveRecordTest extends ElasticSearchTestCase |
|
|
|
$this->assertNull($customer); |
|
|
|
$this->assertNull($customer); |
|
|
|
|
|
|
|
|
|
|
|
// query scalar |
|
|
|
// query scalar |
|
|
|
$customerName = Customer::find()->where(array('id' => 2))->scalar('name'); |
|
|
|
$customerName = Customer::find()->where(array('status' => 2))->scalar('name'); |
|
|
|
$this->assertEquals('user2', $customerName); |
|
|
|
$this->assertEquals('user3', $customerName); |
|
|
|
|
|
|
|
|
|
|
|
// find by column values |
|
|
|
// find by column values |
|
|
|
$customer = Customer::find(array('id' => 2, 'name' => 'user2')); |
|
|
|
$customer = Customer::find(array('name' => 'user2')); |
|
|
|
$this->assertTrue($customer instanceof Customer); |
|
|
|
$this->assertTrue($customer instanceof Customer); |
|
|
|
$this->assertEquals('user2', $customer->name); |
|
|
|
$this->assertEquals('user2', $customer->name); |
|
|
|
$customer = Customer::find(array('id' => 2, 'name' => 'user1')); |
|
|
|
$customer = Customer::find(array('name' => 'user1', 'id' => 2)); |
|
|
|
|
|
|
|
$this->assertNull($customer); |
|
|
|
|
|
|
|
$customer = Customer::find(array('primaryKey' => 5)); |
|
|
|
$this->assertNull($customer); |
|
|
|
$this->assertNull($customer); |
|
|
|
$customer = Customer::find(array('id' => 5)); |
|
|
|
$customer = Customer::find(array('name' => 'user5')); |
|
|
|
$this->assertNull($customer); |
|
|
|
$this->assertNull($customer); |
|
|
|
|
|
|
|
|
|
|
|
// find by attributes |
|
|
|
// find by attributes |
|
|
|
$customer = Customer::find()->where(array('name' => 'user2'))->one(); |
|
|
|
$customer = Customer::find()->where(array('name' => 'user2'))->one(); |
|
|
|
$this->assertTrue($customer instanceof Customer); |
|
|
|
$this->assertTrue($customer instanceof Customer); |
|
|
|
$this->assertEquals(2, $customer->id); |
|
|
|
$this->assertEquals('user2', $customer->name); |
|
|
|
|
|
|
|
|
|
|
|
// find count, sum, average, min, max, scalar |
|
|
|
// find count, sum, average, min, max, scalar |
|
|
|
$this->assertEquals(3, Customer::find()->count()); |
|
|
|
$this->assertEquals(3, Customer::find()->count()); |
|
|
@ -156,13 +160,13 @@ class ActiveRecordTest extends ElasticSearchTestCase |
|
|
|
// $this->assertEquals(2, Customer::find()->active()->count()); |
|
|
|
// $this->assertEquals(2, Customer::find()->active()->count()); |
|
|
|
|
|
|
|
|
|
|
|
// asArray |
|
|
|
// asArray |
|
|
|
$customer = Customer::find()->where(array('id' => 2))->asArray()->one(); |
|
|
|
$customer = Customer::find()->where(array('name' => 'user2'))->asArray()->one(); |
|
|
|
$this->assertEquals(array( |
|
|
|
$this->assertEquals(array( |
|
|
|
'id' => '2', |
|
|
|
|
|
|
|
'email' => 'user2@example.com', |
|
|
|
'email' => 'user2@example.com', |
|
|
|
'name' => 'user2', |
|
|
|
'name' => 'user2', |
|
|
|
'address' => 'address2', |
|
|
|
'address' => 'address2', |
|
|
|
'status' => '1', |
|
|
|
'status' => '1', |
|
|
|
|
|
|
|
'primaryKey' => 2, |
|
|
|
), $customer); |
|
|
|
), $customer); |
|
|
|
|
|
|
|
|
|
|
|
// indexBy |
|
|
|
// indexBy |
|
|
@ -174,13 +178,13 @@ class ActiveRecordTest extends ElasticSearchTestCase |
|
|
|
|
|
|
|
|
|
|
|
// indexBy callable |
|
|
|
// indexBy callable |
|
|
|
$customers = Customer::find()->indexBy(function ($customer) { |
|
|
|
$customers = Customer::find()->indexBy(function ($customer) { |
|
|
|
return $customer->id . '-' . $customer->name; |
|
|
|
return $customer->status . '-' . $customer->name; |
|
|
|
// })->orderBy('id')->all(); |
|
|
|
// })->orderBy('id')->all(); |
|
|
|
})->all(); |
|
|
|
})->all(); |
|
|
|
$this->assertEquals(3, count($customers)); |
|
|
|
$this->assertEquals(3, count($customers)); |
|
|
|
$this->assertTrue($customers['1-user1'] instanceof Customer); |
|
|
|
$this->assertTrue($customers['1-user1'] instanceof Customer); |
|
|
|
$this->assertTrue($customers['2-user2'] instanceof Customer); |
|
|
|
$this->assertTrue($customers['1-user2'] instanceof Customer); |
|
|
|
$this->assertTrue($customers['3-user3'] instanceof Customer); |
|
|
|
$this->assertTrue($customers['2-user3'] instanceof Customer); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public function testFindCount() |
|
|
|
public function testFindCount() |
|
|
|