title: Croogo/CakePHP: Plugin: OnActivation problem - cannot create tables from Schema and table-entry (via Model-instance) at the same time
body: <p>I am currently working on an existing plugin, mostly successful at 
   extending it according to my wishes. But I ran into one major problem: 
   When activating the plugin (onActivation-method) for the first time, 
   the required tables have to be created, in case they do not exist. In 
   fact, they ARE created, it works properly. But directly afterwards, I 
   am creating an Instance of the plugin's Model in order to save a first 
   Model entry in one of the recently created tables. However, this does 
   not work. The tables are created, but I am getting an SQL-Error: 
   Croogo/CakePHP claims, that those tables do not exist. But they 
   actually do. If i hit refresh several times, the plugin gets 
   deacivated again. The tables are then already existing, and the next 
   activation is successful: The sample-data is entered. </p>
  
  <p>This doe snot seem to be a caching issue, because I already playd 
   around with that. I also tried to use loadModel instead of 
   ClassRegistry. But that doesnt change anything at all. </p>
  
  <p>Here is my code:</p>
  
  <pre><code>class SitesActivation {
  public function beforeActivation(&amp;$controller) {
      return true;
  }
  
  public function onActivation(&amp;$controller) {
      App::import('Model', 'CakeSchema');
      App::import('Model', 'ConnectionManager');
      App::import('Libs', 'Sites.sites');
      include_once(APP.'plugins'.DS.'sites'.DS.'config'.DS.'schema'.DS.'schema.php');
      $db = ConnectionManager::getDataSource('default');
  
      //Get all available tables
      $tables = $db-&gt;listSources();
  
      $CakeSchema = new CakeSchema();
      $SiteSchema = new SitesSchema();
  
      if (!in_array('sites', $tables)) {
          $db-&gt;execute($db-&gt;createSchema($SiteSchema, 'sites'));
      }
      if (!in_array('site_domains', $tables)) {
          $db-&gt;execute($db-&gt;createSchema($SiteSchema, 'site_domains'));
      }
      if (!in_array('sites_nodes', $tables)) {
          $db-&gt;execute($db-&gt;createSchema($SiteSchema, 'sites_nodes'));
      }
  
      //Insert "ALL SITES"
  
      $Site = ClassRegistry::init('Sites.Site');
      $Site-&gt;create();
      $data = array(
          'Site' =&gt; array(
              'id' =&gt; Sites::ALL_SITES,
              'title' =&gt; 'All Sites',
              'tagline' =&gt; Configure::read('Site.tagline'),
              'email' =&gt; Configure::read('Site.email'),
              'locale' =&gt; Configure::read('Site.locale'),
              'status' =&gt; Configure::read('Site.status'),
              'timezone' =&gt; Configure::read('Site.timezone'),
              'theme' =&gt; Configure::read('Site.theme'),
              'default' =&gt; 1,
          ),
          'SiteDomain' =&gt; array(
              'site_id' =&gt; Sites::ALL_SITES,
              'domain' =&gt; env('HTTP_HOST'),
          ),
      );
      $Site-&gt;saveAll($data);
  
      /*$db-&gt;execute($db-&gt;rawQuery(
          'INSERT INTO sites ( id, title, tagline, email, locale, status, timezone, theme, default ) VALUES( 
          "1",
          "All Sites",
          "' . Configure::read('Site.tagline') . '",
          "' . Configure::read('Site.email') . '",
          "' . Configure::read('Site.locale') . '",
          "' . Configure::read('Site.status') . '",
          "' . Configure::read('Site.timezone') . '",
          "' . Configure::read('Site.theme') . '",
          "1"
          )'
      ));*/
  }
  
  public function beforeDeactivation(&amp;$controller) {
      return true;
  }
  
  public function onDeactivation(&amp;$controller) {
  }
  
  }
  </code></pre>
  
  <p>Thank you very much for your support!</p>