在多对多关系中应用doctrine sql过滤器

Art*_*nka 5 php doctrine-orm

我的项目中有很多关系(user_role,grade,user_role_grades).但我还要求不要从我的数据库中删除任何数据.所以,我在表中添加了一个状态列,连接2个表以创建多对多的关系.现在我想要

 $userRole->getGrades() 
Run Code Online (Sandbox Code Playgroud)

只获取那些记录,在unite table(user_role_grades)中没有状态"0".对于那些,我试图使用doctrine sql过滤器.

namespace Bis\MpBundle\Filter;
use \Doctrine\ORM\Mapping\ClassMetaData;

class UserRoleGradeFilter extends \Doctrine\ORM\Query\Filter\SQLFilter
{
    public function addFilterConstraint(ClassMetadata $targetEntity, $targetTableAlias)
    {
        if("Bis\DefaultBundle\Entity\UserRoleGrade" == $targetEntity->name){

            return $targetTableAlias . '.status != 0';
        }

        return '';
    }
}
Run Code Online (Sandbox Code Playgroud)

因此,它被称为Bis\DefaultBundle\Entity\UserRole,但不适用于Bis\DefaultBundle\Entity\UserRoleGrade实体.有什么想法吗?

或者你可能有其他想法,我怎么能这样做?

Tom*_*uba 1

我认为这是不可能的,因为它直接附加 SQL。即使你尝试 SQL 注入之类的东西:

return $targetTableAlias . '.status != 0)) LEFT join the_other_table ON  '
. $targetTableAlias . '.grades HAVING the_other_table.status = 0 ((';
Run Code Online (Sandbox Code Playgroud)

它可能会在像这样的声明中崩溃(())