Reputation: 552
Let's say I'm making a multi-company application and thus root of every entity starts with domain which is known by subdomain/currently logged user.
Is there a way to force Doctrine
to limit every query to specific company_id
? I'm also interested on a solution with forms to do that.
Upvotes: 1
Views: 385
Reputation: 526
I can give you a few ideas:
Options for Queries (Doctrine). I suppose that you have a "company_id" column in all your database tables => a company attribute in all you entities.
1) Create query global filters. Take a look at : http://docs.doctrine-project.org/en/latest/reference/filters.html
2) Create an abstract base repository where you could do sth like:
abstract class BaseRepository {
//...
protected function getQBDefault($company) {
$alias = $this->getClassAlias();
$query = $this->createQueryBuilder($alias)
->where($alias . '.company = :company')
->setParameter('company', $company);
return $query;
}
abstract function getClassAlias();
}
class MyRepository extends BaseRepository {
//....
public function findMyResults($company) {
$qb = $this->getQBDefault($company);
//DO STH WITH THIS QUERY BUILDER
return $qb->getQuery()->getResult();
}
}
3) Extend Doctrine EntityManager to apply your filter by company to every query. So you use your own-custom-service instead of the default one wherever you want.
References:
http://symfony.com/doc/current/cookbook/doctrine/multiple_entity_managers.html
How to inject non-default entity managers?
For the forms: could you tell me what you want to do?
If you are more specific, I can help you better ;).
Upvotes: 3