I try to create custom finder in Repository class. And the where condition is find posts what created not by current user. But I still geting all posts. Here is the code:
public function selectRelatedTrips($assoc, $profileId)
{
$params = array();
$query = $this
->getEntityManager()
->createQueryBuilder()
->select('t')
->from('VputiTripBundle:Trip', 't')
->where('t.profile != :profile');
$params['profile']=$profileId;
foreach ($assoc as $k => $v) {
$query->orWhere('t.startCity = :param' . $k);
$query->orWhere('t.targetCity = :param' . $k);
$params['param' . $k] = $v;
}
return $query->setParameters($params)
->setMaxResults(20)
->orderBy('t.id', 'desc')
->getQuery()
->getResult();
}
What I am doing wrong?
If t.profile
is an entity, you should compare like this:
->where('IDENTITY(t.profile) != :profile');
You shouldn't compare to Id, you're mixing up id and entity this way. I prefer to compare the full entity, and delegate to the ORM the full comparison.
This is how I would correct your code, I would change just this line:
$params['profile'] = $profile;