简体   繁体   English

WHERE 子句中的 Doctrine Filter 而不是 LEFT JOIN

[英]Doctrine Filter in WHERE clause instead LEFT JOIN

I have a multi tenant application and i'm using the Doctrine Filters to filter my SQL by client.我有一个多租户应用程序,我正在使用Doctrine 过滤器按客户端过滤我的 SQL。

So, when i want a list of my client Projects i just need to do a "getAll" and the filter will automatically append the SQL on the WHERE clause, like this:因此,当我想要我的客户项目列表时,我只需要执行“getAll”,过滤器就会自动将 SQL 附加到 WHERE 子句上,如下所示:

SELECT * 
FROM projects p
WHERE p.client_id = 1 #(appended by the filter)

My problem is when i want for example the ProjectMembers .我的问题是当我想要ProjectMembers 时 The filter will add the SQL to the LEFT JOIN, not to the WHERE clause, making useless the filter because will return all the ProjectMembers , even they aren't from the client 1.过滤器会将 SQL 添加到 LEFT JOIN,而不是 WHERE 子句,使过滤器变得无用,因为将返回所有ProjectMembers ,即使它们不是来自客户端 1。

SELECT * 
FROM projects p
LEFT JOIN project_members pm ON pm.project_id = p.id 
AND p.client_id = 1 #(appended by the filter)

This is my addFilterConstrait这是我的 addFilterConstrait

public function addFilterConstraint(ClassMetaData $targetEntity, $targetTableAlias)
{
    $class = $targetEntity->getName();

    if (array_key_exists($class, $this->disabled) && $this->disabled[$class] === true) {
        return '';
    } elseif (array_key_exists($targetEntity->rootEntityName, $this->disabled) && $this->disabled[$targetEntity->rootEntityName] === true) {
            return '';
    }

    $config = $this->getFilterConfig($targetEntity->getReflectionClass());

    if (!isset($config['clientFilter']) || !$config['clientFilter']) {
        return '';
    }

    return $targetTableAlias. '.' . $config['columnName'] . ' = ' . $this->getParameter('client'); // getParameter applies quoting automatically
}

Any ideas how can i solve this, adding the filter to the WHERE instead of the LEFT JOIN?任何想法如何解决这个问题,将过滤器添加到 WHERE 而不是 LEFT JOIN?

The more SQL you do in Symfony you will soon see it is much easier to create a repository for your entity, use one for each or share a common repository with multiple entities.您在 Symfony 中执行的 SQL 越多,您很快就会发现为您的实体创建存储库、为每个实体使用一个或与多个实体共享一个公共存储库要容易得多。 Then write your DQL in the repository.然后在存储库中编写您的 DQL。 Note: DQL is a lot like SQL but it has its limitations.注意:DQL 很像 SQL,但它有其局限性。

https://symfony.com/doc/3.3/doctrine/repository.html https://symfony.com/doc/3.3/doctrine/repository.html

Then you can reference the function.然后就可以引用函数了。

$em = $this->getDoctrine()->getManager();
$data = $em->getRepository(EntityClassName::class)->repositoryFunctionName($paramifyouhaveany);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM