2

I generated a schema file using the Cake console, and the schema file uses the after() method to create some default records in the database.

The 'schema create' command works fine for the default database but when I tried to execute the same command for my test database and used --connection parameter... something strange happened. It did create the tables under the test database but it attempted to insert the records on the default database.

I think it might have something to do with the after() method.

// Works. Creates the tables and inserts records successfully to the default

Console/cake schema create -s 1

// Breaks. Creates the tables under test but attempts to insert record in the default database

Console/cake schema create -s 1 --connection test

Here is my Schema file:

<?php 

// Use this Schema for all Stage_2.0 Releases
App::uses("ClassRegistry", "Utility");
App::uses("Shoe", 'Model');

class AppSchema extends CakeSchema {

    public function before($event = array()) {
            // the line below always outputs 'default'... even though --connection parameter is set to 'test'
        debug($this->connection);
        $db = ConnectionManager::getDataSource($this->connection);
        $db->cacheSources = false;
        return true;
    }

    public function after($event = array()) {
        if(isset($event['create'])){
            switch($event['create']){
                case "shoes":
                    $this->InsertSampleShoes();
                    break;
            }
        }
    }

    public function InsertSampleShoes(){
        $shoe = ClassRegistry::init("Shoe");
        $records = array(
            array(
                "Shoe" => array(
                    "name" => "Shoe 1"
                )
            ),
            array(
                "Shoe" => array(
                    "name" => "Shoe 2"
                )
            )
        );
        $shoe->saveAll($records);
    }

        // ... table name, column definitions etc ...

}
Álvaro González
  • 142,137
  • 41
  • 261
  • 360
roosevelt
  • 1,874
  • 4
  • 20
  • 27
  • 1
    Have you double checked that everything is ok in your Db config file? – MrSynAckSter Sep 11 '13 at 14:24
  • I have the exact same issue. Did you manage to resolve this? – lordg Jan 31 '14 at 14:46
  • 1
    @lordg I reported this bug to CakePHP and they said it was resolved in CakePHP 2.4.5 Stable. Make sure your CakePHP is up to date... if it doesn't then you can report the issue here: https://github.com/cakephp/cakephp/issues/2668#issuecomment-33801025 NVM, looks like you already beat me to it :P – roosevelt Jan 31 '14 at 15:58

1 Answers1

3

Ok, after Mark Story's response elsewhere, the answer to this is you need to set the database config for the model after you load it, otherwise it will also use the default.

The schema classes directly integrate with the db, whereas the models run through the normal cakephp config settings.

So for your example above, do the following:

public function InsertSampleShoes(){
    $shoe = ClassRegistry::init("Shoe");
    $shoe->useDbConfig = $this->connection;
    $records = array(
        array(
            "Shoe" => array(
                "name" => "Shoe 1"
            )
        ),
        array(
            "Shoe" => array(
                "name" => "Shoe 2"
            )
        )
    );
    $shoe->saveAll($records);
}
lordg
  • 520
  • 5
  • 25