Ignore Callbacks (beforeFind, beforeSave etc) in CakePHP 3.x -
in cakephp 2.x have find('all','callbacks'=>false)
equivalent alternative in cakephp3?
i have situation in beforefind callback (in model's behavior) i'm appending site_id every query (for multi-tenant app).
90% of time want query appended via beforefind, %10 of time want finds ignore callbacks.
i've looked at: cakephp 3: how ignore beforefind specific queries? comes close, applying method won't 'chain' ignored beforefind() callback on associated models, need do.
updated code:
i've got 2 tables 'sites' , 'details' sites hasone details, details belongs sites. inner join.
in appcontroller's initialize() function i've got
$tbl = tableregistry::get( 'sites' ); $options = [ 'conditions' => ['sites.domain' => 'three.dev.mac', 'sites.is_current' => 1, 'sites.is_archive' => 0], 'contain' => 'details', // contain causes problem 'ignorecallbacks' => true ]; $tenant_details = $tbl->find('all',$options)->first();
my models beforefind() behavior callback
public function beforefind( event $event, query $query, arrayobject $options ) { debug($options); if(isset($options['ignorecallbacks']) && $options['ignorecallbacks'] === true) { // don't filter clause } else{ // filter clause default $query->where( [$this->_table->alias().'.'.'site_id'=> 7 ]); } return $query; }
if comment out 'contain' line of find call, query get's ignored should , debug call returns 'ignorecallbacks' => true' great.
if leave 'contain' line in find() call (which want) 2 debug outputs beforefind(), first has 'ignorecallbacks' => true', second empty. apparently second overrides first , query tries append site_id, don't want.
any thoughts?
http://book.cakephp.org/3.0/en/orm/retrieving-data-and-resultsets.html
any options not in list passed beforefind listeners can used modify query object. can use getoptions() method on query object retrieve options used.
so pass custom option queries find() call 2nd arg , read option in beforefind() described above.
if (isset($options['usesiteid']) && $options['usesiteid'] === true) { /*...*/ }
Comments
Post a Comment