Flourish PHP Unframework
This is an archived copy of the forum for reference purposes

Error finding table when using multiple databases

posted by bret 9 years ago

I'm receiving the following error:

Uncaught fProgrammerException
-----------------------------
{doc_root}\\manage\\ads.php(10): fActiveRecord->__construct()
{doc_root}\\inc\\flourish\\fActiveRecord.php(1053): fSchema->getKeys('ad_blocks', 'primary')
{doc_root}\\inc\\flourish\\fSchema.php(1885) The table specified, ad_blocks, does not exist in the database

when trying to use multiple databases. In my config file I setup the databases like so:

//  INIT DB
$db = new fDatabase('mysql', 'database1', 'username', 'password', 'ip', '3306');
fORMDatabase::attach($db);

$db2 = new fDatabase('mysql', 'database2', 'username', 'password', 'ip', '3306');
fORMDatabase::attach($db2, 'adserver');

and declare the database relation in the class file:

class AdBlock extends fActiveRecord
{
  protected function configure()
  {
	fORM::mapClassToDatabase($this, 'adserver');
  }
}

Any ideas on what's going on? I've double-checked that the table does exist ;)

fORM::mapClassToDatabase() call needs to use the same name that you specified in your fORMDatabase::attach() call:

class AdBlock extends fActiveRecord
{
  protected function configure()
  {
    fORM::mapClassToDatabase($this, 'adserver');
  }
}

I noticed in your original post you had the db name as database2, but youve since changed it to adserver. Does it work set as adserver?

posted by wbond 9 years ago

no, that was just a mistake in the original post. i was renaming the database names and ip's and accidentally changed that.

posted by bret 9 years ago

Can you post the output of the following SQL statement when you run it in the mysql command line client (or PHPMyAdmin)?

SHOW CREATE TABLE ad_blocks;
posted by wbond 9 years ago
CREATE TABLE `ad_blocks` (
  `id` int(11) unsigned NOT NULL auto_increment,
  `image_id` int(11) unsigned default NULL,
  `title` varchar(25) default NULL,
  `text` varchar(70) NOT NULL,
  `link_text` varchar(15) default NULL,
  `link_href` varchar(100) NOT NULL,
  `advertiser_id` int(11) unsigned NOT NULL,
  PRIMARY KEY  (`id`),
  KEY `image_id` (`image_id`),
  KEY `advertiser_id` (`advertiser_id`),
  CONSTRAINT `ad_block_image_fk` FOREIGN KEY (`image_id`) REFERENCES `images` (`id`),
  CONSTRAINT `ad_block_advertiser_fk` FOREIGN KEY (`advertiser_id`) REFERENCES `advertisers` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1
posted by bret 9 years ago

What about the output from:

SHOW FULL TABLES WHERE table_type = 'BASE TABLE'
posted by wbond 9 years ago
ad_blocks, BASE TABLE
advertisers, BASE TABLE
images, BASE TABLE
images_sizes, BASE TABLE
posted by bret 9 years ago

Everything so far is looking fine. Let's try explicitly setting a schema object and see if that fixes it:

// After calling fORMDatabase::attach() for adserver
fORMSchema::attach(new fSchema($db2), 'adserver');

If so there may be a bug in creating the fSchema object for the non-default database.

posted by wbond 9 years ago

I'm receiving the same error message with the fORMSchema::attach() call added.

posted by bret 9 years ago

How about putting this code right where you attach the databases?

fORM::mapClassToDatabase('AdBlock', 'adserver');
posted by wbond 9 years ago

yup, that did the trick. any insight on what was happening? i know that the class' configure function was getting called...

posted by bret 9 years ago

Well, the configure method is called the first time an object of that type is created. I've tried to make it so that the ORM calls the configure method before any operations related to that record are performed, but it appears that whatever you were doing on ad.php was acting on the ad_blocks table, but had not yet configured AdBlock.

What operation were you performing on line 10 of ads.php?

posted by wbond 9 years ago
$adblock = new AdBlock();

I was just creating a new AdBlock instance to test the fORMDatabase configuration.

posted by bret 9 years ago

I wanted to let you guys know that all issues with mapped classes should be resolved as of r792.

I also updated the documentation about fORM::mapClassToTable() to recommend calling it in site-wide configuration and not in fActiveRecord::configure().

posted by wbond 9 years ago