add a common scope to all models in cakephp
-
22-06-2021 - |
Frage
I am building an online accounts application where each business has its own data and therefore each table has a field business_id.
Is there a way to automatically add to each Model the condition
Model.business_id => x
For example if a user did a search for all Transactions containing Project the conditions added would be:
Transaction.business_id => x
Project.business_id => x
I am guessing that this would be best placed in a behavior as it applies to all but two models
Thanks
Lösung
you can modify query data in beforeFind(), which can be within a behavior as well
in your models:
$actsAs = array('BusinessCondition');
The behavior would be something like:
<?php
App::uses('AuthComponent', 'Controller/Component');
class BusinessCondition extends ModelBehavior {
public function beforeFind(Model $Model, $query) {
$query['conditions'][$Model->alias . '.business_id'] = AuthComponent::user('business_id');
return $query;
}
}
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow