orm - How to remove an association on the fly in CakePHP v3 -


in previous versions of cakephp temporarily alter associations table::bindmodel('somemodel'); can't figure out how in v3.

i want temporarily disable hasmany association that's defined in table class because it's causing errors when run older migrations written before table existed. don't understand migration problem goes away when comment out association in table class.

class agenciestable extends table {     public function initialize(array $config)     {         parent::initialize($config);          $this->table('agencies');         $this->displayfield('full_name');         $this->primarykey('id');         $this->addbehavior('timestamp');          $this->hasmany('routes'); 

enter image description here

the issue here should not rely on actual model classes when using migrations. can cause kind of issue encountered.

instead use tableregistry or table object directly , create table object without dependencies. load associations need directly on object.

$agencies = new table(['table' => 'agencies', /*...*/]); $agencies->belongsto('whatever'); /* data manipulation */ 

this way migration work no matter other changes has been made agenciestable class. , imho proper way of doing in migrations.

i think if don't create association explicit calling $this->hasmany('routes'); you'll end same error because eager loader still try find matching table class , load dynamically. reason why there no "uncontain" method.

also you're not showing actual query code... assume you're calling custom find or method calls query::contain() somewhere. write new query without contain?

$agencies->find()->contain(['one', 'two'])->where([/*...*/])->all(); 

if have big query might idea break more custom finds because can combined:

$agencies->find('withoutmycontain')->all(); $agencies->find('withoutmycontain')->find('withmycontain')->all(); 

see custom finder methods.


Comments