Sharing data in a Symfony Multi-tenant app using Doctrine

I am trying to implement an application with several tenants, that is - the data of all clients in one database - each common table has a field tenant_idfor sharing data

I want to achieve data separation by adding where('tenant_id = ', $user->getTenantID()){pseudo-code} to all SELECT queries. I could not find a solution at the front, but here are the possible approaches that I am considering. 1) an unreasonable approach: setting up all functions fetchAlland fetchOnein each class (go crazy) 2) with the audience: perhaps by encoding the event preDqlSelectand adding "where" to all requests 3) override buildQuery(): Could not find an example of this for the front- end 4) implement contentformfilter: need a pointer again

I would be grateful if someone could confirm them and comment on the effectiveness, suitability. In addition, if someone has achieved multi-dependency using a different strategy, share pl. Thanks

+3
source share
4 answers

I published sfMultiTenantPlugin, find it here: http://www.symfony-project.org/plugins/sfMultiTenantPlugin

+1
source

, Doctrine Record Listeners, preDqlSelect. , - , , , . - .

.

+2

, , , ... MyModel :

// lib/model/doctrine/MyModelTable.class.php
class MyModelTable extends Doctrine_Table
{
  public function createTenantAwareQuery($userId)
  {
    $q = $this->createQuery('m')
      ->where('tenant_id = ', $userId);
    return $q;
  }
}

:

$myVar = Doctrine_Core::getTable('MyModel')->createTenantAwareQuery()
  ->where('something = ', $value);

Thus, if necessary, you create an "Animated request for the tenant" ... you simply use this function when necessary ... Even in the admin generator in the configuration file, there is a way to override the default request method:

# apps/backend/modules/(module)/config/generator.yml
config:
  list:
    table_method: retrieveTenantAwareResult

It remains only to create this method.

Hope this answer will work for you =)

+1
source

The plugin sfMultiTenantPlugindoes not work for leftJoin. When A left join B and B is empty, the plugin will return an empty result set.

0
source

Source: https://habr.com/ru/post/1741400/


All Articles