PHP code example of rentpost / doctrine-multi-tenancy
1. Go to this page and download the library: Download rentpost/doctrine-multi-tenancy library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
rentpost / doctrine-multi-tenancy example snippets
public function getIdentifier(): string;
public function isContextual(): bool;
use Rentpost\Doctrine\MultiTenancy\PrivilegedContextProviderInterface;
class TaskContextProvider implements PrivilegedContextProviderInterface
{
// ...
}
#[ORM\Entity]
#[MultiTenancy(filters: [
new MultiTenancy\Filter(where: '$this.company_id = {companyId}'),
new MultiTenancy\Filter(
context: ['task'],
where: '$this.company_id = {companyId}',
),
])]
class CreditReport
{
}
use Doctrine\ORM\Configuration;
use Doctrine\DBAL\Connection;
use App\Adapter\Doctrine\ORM\MultiTenancy\ContextProvider; // Your namespace for ContextProviders
use App\Adapter\Doctrine\ORM\MultiTenancy\ValueHolder; // Your namespace for ValueHolders
use Rentpost\Doctrine\MultiTenancy\Listener as MultiTenancyListener;
$connection = new Connection($dbalParams, new MySQLDriver());
$config = new Configuration();
$eventManager = $connection->getEventManager();
$multiTenancyListener = new MultiTenancyListener();
// The ValueHolders your filters interpolate values from
$multiTenancyListener->addValueHolder(new ValueHolder\Company());
$multiTenancyListener->addValueHolder(new ValueHolder\User());
// And the ContextProviders that say which kind of actor is acting
$multiTenancyListener->addContextProvider(new ContextProvider\Admin());
$multiTenancyListener->addContextProvider(new ContextProvider\Manager());
$multiTenancyListener->addContextProvider(new ContextProvider\Guest());
// Say what becomes of a context an entity has not declared
$multiTenancyListener->setPermissiveDefault(false);
$eventManager->addEventSubscriber($multiTenancyListener);
// Add the filter to the EntityManager config
$config->addFilter('multi-tenancy', 'Rentpost\Doctrine\MultiTenancy\Filter');
$entityManager = EntityManager::create($connection, $config, $eventManager);
// Lastly, you need to be sure you've enabled the filter
$entityManager->getFilters()->enable('multi-tenancy');
use Doctrine\ORM\Mapping as ORM;
use Rentpost\Doctrine\MultiTenancy\Attribute\MultiTenancy;
#[ORM\Entity]
#[MultiTenancy(filters: [
new MultiTenancy\Filter(where: '$this.company_id = {companyId}'),
])]
class Product
{
}
#[ORM\Entity]
#[MultiTenancy(filters: [
new MultiTenancy\Filter(
context: ['manager'],
where: '$this.company_id = {companyId}',
),
new MultiTenancy\Filter(
context: ['visitor'],
where: '$this.id IN(
SELECT product_id
FROM product_group
WHERE status = \'published\'
)',
),
new MultiTenancy\Filter(
context: ['admin'],
ignore: true,
),
])]
class Product
{
}
#[MultiTenancy(permissive: true, filters: [
new MultiTenancy\Filter(context: ['manager'], where: '$this.company_id = {companyId}'),
])]
class Product
{
}
#[ORM\Entity]
#[MultiTenancy(
strategy: MultiTenancy\FilterStrategy::FirstMatch,
filters: [
new MultiTenancy\Filter(
context: ['admin'],
ignore: true,
),
new MultiTenancy\Filter(
context: ['manager', 'staff'],
where: '$this.company_id = {companyId}',
),
],
)]
class Product
{
}
public function getIdentifier(): string;
public function getValue(): ?string;
use Rentpost\Doctrine\MultiTenancy\ConditionResolver;
// The Listener is the same one registered with the EventManager during setup
$resolver = new ConditionResolver($listener);
// Resolve conditions for a given entity class and table alias
$condition = $resolver->resolve(Product::class, 'p');
// Returns e.g.: "p.company_id = 42 AND p.id IN(SELECT product_id FROM ...)"
// Use in a raw SQL query
$sql = "SELECT p.* FROM product p WHERE {$condition} AND p.status = 'active'";
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.