function DatabaseFetchTestCase::testQueryFetchClasstype

Confirms that we can fetch a record into a new instance of a custom class. The name of the class is determined from a value of the first column.

See also

FakeRecord

File

modules/simpletest/tests/database_test.test, line 459

Class

DatabaseFetchTestCase
Test fetch actions, part 1.

Code

function testQueryFetchClasstype() {
  $records = array();
  $result = db_query('SELECT classname, name, job FROM {test_classtype} WHERE age = :age', array(
    ':age' => 26,
  ), array(
    'fetch' => PDO::FETCH_CLASS | PDO::FETCH_CLASSTYPE,
  ));
  foreach ($result as $record) {
    $records[] = $record;
    if ($this->assertTrue($record instanceof FakeRecord, 'Record is an object of class FakeRecord.')) {
      $this->assertIdentical($record->name, 'Kay', 'Kay is found.');
      $this->assertIdentical($record->job, 'Web Developer', 'A 26 year old Web Developer.');
    }
    $this->assertFalse(isset($record->classname), 'Classname field not found, as intended.');
  }
  $this->assertIdentical(count($records), 1, 'There is only one record.');
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.