質問

I'm facing a situation where D2 Query builder returns, for 2 different requests, the results of the first one twice.

I am doing the 2 following queries one after another :

$friends['interests'] = $this->retrieveUsersByCat( $user, $minLat, $maxLat, $minLng, $maxLng, $lat, $lng, $cat ); //This should return Deena

$friends['available'] = $this->retrieveUsersAvail( $user, $minLat, $maxLat, $minLng, $maxLng, $lat, $lng ); // This should return Obiwan

The first one should return Deena - when performing ONLY this query it works fine

The second one should return Obiwan - when performing ONLY this query it works fine

But when perfoming the 2 queries one after another (literraly the same as here in my code) it returns Deena twice. So i was thinking it would come from my queries itself but here is the thing: if I invert the two queries to something like:

$friends['available'] = $this->retrieveUsersAvail( $user, $minLat, $maxLat, $minLng, $maxLng, $lat, $lng );
$friends['interests'] = $this->retrieveUsersByCat( $user, $minLat, $maxLat, $minLng, $maxLng, $lat, $lng, $cat );

Then it returns Obiwan twice, just like if my results where overwritten. So I'be looking directly at my queries to see what it returns. And the problem comes directly fro what D2 returns for each query.

Now another weird thing is that if I ask for an array in one of the query (say retrieveUsersByCat) then I'm getting the expected results. (Deena and Obiwan) But I want to keep my data consistent.

This is weird, I've been trying to play around with the queries itself qithout success.

Could anyone help me with that please ?

Here are the 2 queries:

public function retrieveUsersByCat( $user, $minLat, $maxLat, $minLng, $maxLng, $lat, $lng, $cat )
{
    //We only want to return users nearby who are available OR who
    $qb = $this->getEntityManager()->createQueryBuilder();

    $qb->select( 'USER', 'FRIENDS' )
        ->from( 'Entity\User',  'USER' )
        ->where( 'USER = :user' )
        ->leftJoin( 'USER.friends', 'FRIENDS' )
        ->andWhere( ':cat MEMBER OF FRIENDS.interests' )// THAT WAS IT !! MEMBER OF perform a where in a many to many !!
        ->andWhere( 
            $qb->expr()->andX(
                $qb->expr()->between( 'FRIENDS.latitude', ':minLat', ':maxLat' ),
                $qb->expr()->between( 'FRIENDS.longitude', ':minLng', ':maxLng' )
            )
        );

    $array = array(
         'cat' => $cat,
         'user'  => $user,
         'minLat' => $minLat,
         'maxLat' => $maxLat,
         'minLng' => $minLng,
         'maxLng' => $maxLng
    );

    $qb->setParameters( $array );

    $userReturned = $qb->getQuery()->getOneOrNullResult(); // NO USER RETURNED if no friends matches!

    if ( $userReturned )
    {
        return $userReturned->getFriends();
    }
    else
    {
        return false;
    }
}

And

public function retrieveUsersAvail( $user, $minLat, $maxLat, $minLng, $maxLng, $lat, $lng )
{
    //We only want to return users nearby who are available OR who
    $qb = $this->getEntityManager()->createQueryBuilder();

    $qb->select( 'USER', 'FRIENDS' )
        ->from( 'Entity\User',  'USER' )
        ->where( 'USER = :user' )
        ->leftJoin('USER.friends', 'FRIENDS')
        ->andWhere( 
            $qb->expr()->andX(
                $qb->expr()->eq( 'FRIENDS.available', 1 ),
                $qb->expr()->between('FRIENDS.latitude', ':minLat', ':maxLat'),
                $qb->expr()->between('FRIENDS.longitude', ':minLng', ':maxLng')
            )
        )
        ->add( 'orderBy', $qb->expr()->sum( 'FRIENDS.latitude - :lat', 'FRIENDS.longitude - :lng' ) );

    $array = array(
         'user'   => $user,
         'minLat' => $minLat,
         'maxLat' => $maxLat,
         'minLng' => $minLng,
         'maxLng' => $maxLng,
         'lat'    => $lat,
         'lng'    => $lng
    );

    $qb->setParameters( $array );
    // $qb->setFirstResult( $offset );
    // $qb->setMaxResults( $limit );
    $usersAvail = $qb->getQuery()->getOneOrNullResult();

    if ( $usersAvail )
    {
        return $usersAvail->getFriends();
    }
    else
    {
        return False;
    }
}
役に立ちましたか?

解決

2 answers:

$em->clear(); to detach all entities from the EntityManager (em)

$em->detach($entity) to detach only one entity.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top