Monday, February 3, 2014

Re: CakePHP Schema Connection is Resetting to Default

I found the solution to this as it appears Schema doesn't manage the Model's database config setting.

Add $shoe->useDbConfig = $this->connection;

On Tuesday, August 13, 2013 7:43:50 PM UTC+2, Roosevelt P wrote:
Hi,

I  generated a Schema file. And the schema file creates records for two tables under the function after(). If I run the create command on the default
table it works just fine. But if I specify the --connection argument 'test', it creates the tables under 'test' just fine but tries to insert the records to the default
connection regardless of the --connection parameter.

// Works fine. Creates the tables under the default database and inserts records
$ Console/cake schema create -s 1

// Breaks. Creates the tables under 'test' connection but tries to insert the record under the 'default' database connection
$ Console/cake schema create -s 1 --connection test

I believe it might has something to do with the after() method or it could be a bug within CakePHP console.

Here is the partial code of my Schema file:

<?php

App::uses("ClassRegistry", "Utility");
App::uses("Shoe", 'Model');

class AppSchema extends CakeSchema {

    public function before($event = array()) {
        debug($this->connection); // always prints default :/
        $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->InsertDefaultShoes();
                    break;
            }
        }
    }

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

    ......

}

--
Like Us on FaceBook https://www.facebook.com/CakePHP
Find us on Twitter http://twitter.com/CakePHP
 
---
You received this message because you are subscribed to the Google Groups "CakePHP" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cake-php+unsubscribe@googlegroups.com.
To post to this group, send email to cake-php@googlegroups.com.
Visit this group at http://groups.google.com/group/cake-php.
For more options, visit https://groups.google.com/groups/opt_out.

No comments: