php - CakePHP shell: Switch CAKE_ENV on the Go -
i have little problem in here. i'm new @ cakephp , have developing cakephp shell script saving data database. problem is, work on default environment , need save data environment. i'm using code switch environment:
connectionmanager::alias($env, 'default');
it seems since i've got right output when try database.
$this->out($datasource->config()['database']);
and load model:
$model = $this->model;
but it's load model data default
environment. approach wrong? or, there method switch environment on go cakephp?
that should work fine, , quick test shows does. you'd have show little more context, guess loading model (what you're showing there isn't loading accessing) before connection alias being created, hence model use original connection received when has been instantiated.
so either make sure load model afterwards, respectively create alias before model being loaded (that when tableregistry::get()
being invoked), or change connection of specific model on fly in case applicable:
$connection = connectionmanager::get($env); $model->setconnection($connection); // use connection($connection) in cakephp < 3.4
Comments
Post a Comment