public function testClassToDatabaseMapping() { $this->assertEquals('default', fORM::getDatabaseName('User')); $this->assertEquals('default', fORM::getDatabaseName('PhotoGallery')); fORM::mapClassToDatabase('User', 'second_db'); $this->assertEquals('second_db', fORM::getDatabaseName('User')); }
/** * Return the instance of the fSchema class * * @param string $class The class the object will be used with * @return fSchema The schema instance */ public static function retrieve($class = 'fActiveRecord') { if (substr($class, 0, 5) == 'name:') { $database_name = substr($class, 5); } else { $database_name = fORM::getDatabaseName($class); } if (!isset(self::$schema_objects[$database_name])) { self::$schema_objects[$database_name] = new fSchema(fORMDatabase::retrieve($class)); } return self::$schema_objects[$database_name]; }
/** * Return the instance of the fDatabase class * * @param string $class The class to retrieve the database for - if not specified, the default database will be returned * @param string $role If the database will be used for `'write'`, `'read'` or `'either'` operations * @return fDatabase The database instance */ public static function retrieve($class = 'fActiveRecord', $role = 'either') { if (substr($class, 0, 5) == 'name:') { $database_name = substr($class, 5); } else { $database_name = fORM::getDatabaseName($class); } if (!isset(self::$database_objects[$database_name])) { throw new fProgrammerException('The database object named "%1$s" has not been attached via %2$s yet', $database_name, __CLASS__ . '::attach()'); } if ($role == 'write' || $role == 'read') { // If the user wants a read database but we are in a transaction on the write database, return // the write database to allow for comparing data changed since the transaction started if ($role == 'read' && isset(self::$database_objects[$database_name]['write']) && self::$database_objects[$database_name]['write']->isInsideTransaction()) { $role = 'write'; } if (!isset(self::$database_objects[$database_name][$role])) { throw new fProgrammerException('The database object named "%1$s" for the %s$2 role has not been attached via %3$s yet', $database_name, $role, __CLASS__ . '::attach()'); } return self::$database_objects[$database_name][$role]; } if (isset(self::$database_objects[$database_name]['write'])) { return self::$database_objects[$database_name]['write']; } elseif (isset(self::$database_objects[$database_name]['read'])) { return self::$database_objects[$database_name]['read']; } }