Ejemplo n.º 1
0
	function testWriteSavesToHasOneRelations() {
		/* DataObject::write() should save to a has_one relationship if you set a field called (relname)ID */
		$team = new DataObjectTest_Team();
		$captainID = $this->idFromFixture('DataObjectTest_Player', 'player1');
		$team->CaptainID = $captainID;
		$team->write();
		$this->assertEquals($captainID, DB::query("SELECT CaptainID FROM DataObjectTest_Team WHERE ID = $team->ID")->value());
		
		/* After giving it a value, you should also be able to set it back to null */
		$team->CaptainID = '';
		$team->write();
		$this->assertEquals(0, DB::query("SELECT CaptainID FROM DataObjectTest_Team WHERE ID = $team->ID")->value());

		/* You should also be able to save a blank to it when it's first created */
		$team = new DataObjectTest_Team();
		$team->CaptainID = '';
		$team->write();
		$this->assertEquals(0, DB::query("SELECT CaptainID FROM DataObjectTest_Team WHERE ID = $team->ID")->value());
		
		/* Ditto for existing records without a value */
		$existingTeam = $this->objFromFixture('DataObjectTest_Team', 'team1');
		$existingTeam->CaptainID = '';
		$existingTeam->write();
		$this->assertEquals(0, DB::query("SELECT CaptainID FROM DataObjectTest_Team WHERE ID = $existingTeam->ID")->value());
	}
 function testManyManyExtraFields()
 {
     $player = $this->objFromFixture('DataObjectTest_Player', 'player1');
     $team = $this->objFromFixture('DataObjectTest_Team', 'team1');
     // Extra fields are immediately available on the Team class (defined in $many_many_extraFields)
     $teamExtraFields = $team->many_many_extraFields('Players');
     $this->assertEquals($teamExtraFields, array('Position' => 'Varchar(100)'));
     // We'll have to go through the relation to get the extra fields on Player
     $playerExtraFields = $player->many_many_extraFields('Teams');
     $this->assertEquals($playerExtraFields, array('Position' => 'Varchar(100)'));
     // Iterate through a many-many relationship and confirm that extra fields are included
     $newTeam = new DataObjectTest_Team();
     $newTeam->Title = "New team";
     $newTeam->write();
     $newTeamID = $newTeam->ID;
     $newPlayer = new DataObjectTest_Player();
     $newPlayer->FirstName = "Sam";
     $newPlayer->Surname = "Minnee";
     $newPlayer->write();
     // The idea of Sam as a prop is essentially humourous.
     $newTeam->Players()->add($newPlayer, array("Position" => "Prop"));
     // Requery and uncache everything
     $newTeam->flushCache();
     $newTeam = DataObject::get_by_id('DataObjectTest_Team', $newTeamID);
     // Check that the Position many_many_extraField is extracted.
     $player = $newTeam->Players()->First();
     $this->assertEquals('Sam', $player->FirstName);
     $this->assertEquals("Prop", $player->Position);
     // Check that ordering a many-many relation by an aggregate column doesn't fail
     $player = $this->objFromFixture('DataObjectTest_Player', 'player2');
     $player->Teams("", "count(DISTINCT \"DataObjectTest_Team_Players\".\"DataObjectTest_PlayerID\") DESC");
 }
Ejemplo n.º 3
0
 public function testRemoveAll()
 {
     $first = new DataObjectTest_Team();
     $first->write();
     $second = new DataObjectTest_Team();
     $second->write();
     $firstPlayers = $first->Players();
     $secondPlayers = $second->Players();
     $a = new DataObjectTest_Player();
     $a->ShirtNumber = 'a';
     $a->write();
     $b = new DataObjectTest_Player();
     $b->ShirtNumber = 'b';
     $b->write();
     $firstPlayers->add($a);
     $firstPlayers->add($b);
     $secondPlayers->add($a);
     $secondPlayers->add($b);
     $this->assertEquals(array('a', 'b'), $firstPlayers->sort('ShirtNumber')->column('ShirtNumber'));
     $this->assertEquals(array('a', 'b'), $secondPlayers->sort('ShirtNumber')->column('ShirtNumber'));
     $firstPlayers->removeAll();
     $this->assertEquals(0, count($firstPlayers));
     $this->assertEquals(2, count($secondPlayers));
     $firstPlayers->removeAll();
     $firstPlayers->add($a);
     $firstPlayers->add($b);
     $this->assertEquals(array('a', 'b'), $firstPlayers->sort('ShirtNumber')->column('ShirtNumber'));
     $firstPlayers->filter('ShirtNumber', 'b')->removeAll();
     $this->assertEquals(array('a'), $firstPlayers->column('ShirtNumber'));
     $this->assertEquals(array('a', 'b'), $secondPlayers->sort('ShirtNumber')->column('ShirtNumber'));
     $this->assertNotNull(DataObjectTest_Player::get()->byID($a->ID));
     $this->assertNotNull(DataObjectTest_Player::get()->byID($b->ID));
 }