hcarver
hcarver

Reputation: 7214

Table for model was not found in datasource

I'm converting a database from being managed by SQL dumps to being managed by schemas and migrations. Part of this is seeding data. I've based what I'm doing from the schema example on CakePhp's page about Schemas.

The weird thing is that the first table to be seeded with data works without problem, and the second fails with an error like Table users for model User was not found in datasource default. This happens even if I change which table will be seeded: the first one succeeds (and I've checked in the database that the data is there) and the next one to be seeded fails.

I've also checked the error message against the database, and every table it complains about not existing does actually exist.

My 'schema.php' looks like this:

class AppSchema extends CakeSchema {
public function before($event = array()) {
    return true;
}

private function create_many($class_name, $entries){  
  App::uses('ClassRegistry', 'Utility');
  $class = ClassRegistry::init($class_name);
  foreach($entries as $entry){
    $class->create();
    $class->save(array($class_name => $entry));
  }
}

private function create_many_kv($class_name, $keys, $values_matrix){
  $entries = array();
  foreach($values_matrix as $values){
    array_push($entries, array_combine($keys, $values));
  }
  $this->create_many($class_name, $entries);
}

public function after($event = array()) {
  if (isset($event['create'])) {
    switch ($event['create']) {
      case 'users':
        $this->create_many('User', array(
          array('emailaddress' => 'email',
                'password' => 'hash',
                'role_id' => 1
               ),
          array('emailaddress' => 'email2',
                'password' => 'hash',
                'role_id' => 3)
          ));
        break;
      case 'other_table':
        $this->create_many('OtherTable', array(
          array('id' => 1,
                'name' => 'datum'),
          array('id' => 2,
               'name' => 'datum2')
        ));
        break;

etc.

Upvotes: 1

Views: 1249

Answers (2)

bdereta
bdereta

Reputation: 913

When inserting data to more than one table you’ll need to flush the database cache after each table is created. Cache can be disabled by setting $db->cacheSources = false in the before action().

public $connection = 'default';

public function before($event = array()) {
    $db = ConnectionManager::getDataSource($this->connection);
    $db->cacheSources = false;
    return true;
}

Upvotes: 0

hcarver
hcarver

Reputation: 7214

The answer for me here was to populate all of the tables after the last table has been created.

My best hypothesis for why this didn't work as described in the question is that Cake is caching the database structure in memory, and this isn't updated when the new tables are added. I can't find any documentation about clearing that structure cache so a workaround is the closest thing to a solution for now.

Upvotes: 1

Related Questions