示例#1
0
 function test_batch()
 {
     // Clear existing records
     $this->assertTrue(DB::execute('delete from foo'));
     // Test batch array of insertions
     $this->assertTrue(Foo::batch(array(array('id' => 1, 'name' => 'One'), array('id' => 2, 'name' => 'Two'), array('id' => 3, 'name' => 'Three'))));
     $this->assertEquals(3, Foo::query()->count());
     // Test closure batch
     $this->assertTrue(Foo::batch(function () {
         // Update an existing item
         $one = new Foo(1);
         $one->name = 'Joe';
         if (!$one->put()) {
             $f->error = $one->error;
             return false;
         }
         // Add a new one too
         $four = new Foo(array('name' => 'Four'));
         if (!$four->put()) {
             $f->error = $four->error;
             return false;
         }
     }));
     $this->assertEquals(4, Foo::query()->count());
     $one = new Foo(1);
     $this->assertEquals('Joe', $one->name);
     // Test rollback on false
     $this->assertFalse(Foo::batch(function () {
         $five = new Foo(array('name' => 'Five'));
         $five->put();
         return false;
     }));
     $this->assertEquals(4, Foo::query()->count());
 }