Search code examples
mysqlsymfony1doctrinedql

Doctrine query for authentication


Consider this query:

$query = Doctrine::getTable('sfGuardUser')
      ->createQuery('u')
      ->innerJoin('u.Groups g')
      ->where('u.name = 'username')
      ->adnWhere('g.name <> 'groupname')

This return a user with 'username' regardless of his 'groupname'. I need to only return a user if he does NOT have a 'groupname' relation.


Solution

  • You should use the WITH keyword in your inner join. This basically add conditions to the implicit ON clause of the inner join.

    $query = Doctrine::getTable('sfGuardUser')
          ->createQuery('u')
          ->innerJoin("u.Groups g WITH g.name <> 'groupname'")
          ->where('u.name = 'username')
    

    More info here.