public function testDeleteCompositeViaParent()
 {
     // At this moment, this should result 2 records
     $exists = Gas\Core::query("SELECT * FROM r_u WHERE r_u.u_id = 1")->result_array();
     $this->assertCount(2, $exists);
     // In this case, it should not delete any of the role entry
     // Instead, it just delete the intermediate table (if it was a composite table)
     $this->assertTrue(Model\User::with('role')->delete(1));
     // Now, this should contain empty array
     $exists = Gas\Core::query("SELECT * FROM r_u WHERE r_u.u_id = 1")->result_array();
     $this->assertEmpty($exists);
 }
Exemple #2
0
 public function testEagerLoadIntermixedRelationType()
 {
     // Find user with id `1` with his wife kid, job and role
     $user1 = Model\User::with('wife', 'kid', 'job', 'role')->find(1);
     // Consist
     $this->assertInstanceOf('Gas\\ORM', $user1);
     $this->assertInstanceOf('Gas\\Data', $user1->record);
     // Check result
     $this->assertEquals($user1->id, '1');
     $this->assertEquals($user1->name, 'John Doe');
     $this->assertEquals($user1->email, '*****@*****.**');
     $this->assertEquals($user1->username, 'johndoe');
     // Grab related wife
     $user1_wife = $user1->wife();
     // Consist
     $this->assertInstanceOf('Gas\\ORM', $user1_wife);
     $this->assertInstanceOf('Gas\\Data', $user1_wife->record);
     // Check results, this should be `Patricia Doe` with `2` as her id
     $this->assertEquals($user1_wife->id, '2');
     $this->assertEquals($user1_wife->name, 'Patricia Doe');
     // Since the third parameter for user <-> wife relationship
     // contain `select:id,name` for pre-process relation
     // Other fields should be null
     $this->assertNull($user1_wife->hair_color);
     // Grab related kid(s)
     $user1_kids = $user1->kid();
     // Should contain 2 kids
     $this->assertCount(2, $user1_kids);
     foreach ($user1_kids as $kid) {
         // Consist
         $this->assertInstanceOf('Gas\\ORM', $kid);
         $this->assertInstanceOf('Gas\\Data', $kid->record);
         // Check results
         // This should be kid with id `1` and `2`
         switch ($kid->id) {
             case '1':
                 $this->assertEquals($kid->name, 'Daria Doe');
                 break;
             case '2':
                 $this->assertEquals($kid->name, 'John Doe Jr');
                 break;
         }
     }
     // Grab related job(s)
     $user1_jobs = $user1->job();
     // Should contain 2 jobs
     $this->assertCount(2, $user1_jobs);
     foreach ($user1_jobs as $job) {
         // Consist
         $this->assertInstanceOf('Gas\\ORM', $job);
         $this->assertInstanceOf('Gas\\Data', $job->record);
         // Check results
         // This should be job with id `2` and `3`
         switch ($job->id) {
             case '2':
                 $this->assertEquals($job->name, 'Politician');
                 break;
             case '3':
                 $this->assertEquals($job->name, 'Accountant');
                 break;
         }
     }
     // Grab related role(s)
     $user1_roles = $user1->role();
     // Should contain 2 roles
     $this->assertCount(2, $user1_roles);
     foreach ($user1_roles as $role) {
         // Consist
         $this->assertInstanceOf('Gas\\ORM', $role);
         $this->assertInstanceOf('Gas\\Data', $role->record);
         // Check results
         // This should be role with id `2` and `3`
         switch ($role->id) {
             case '2':
                 $this->assertEquals($role->name, 'Moderator');
                 break;
             case '3':
                 $this->assertEquals($role->name, 'Member');
                 break;
         }
     }
 }