1

Here is the content of my custom repository function for my entity Feed.

return $this->createQueryBuilder('f')
->select('f, COUNT(s)')
->leftJoin('f.subscriptions','s')
->groupBy('f')
->having('f.inCatalog = TRUE')
->orHaving('COUNT(s) > 0');
->getQuery()->getResult();

I want to get every feed with at least one subscription in my catalog, then browse it in a foreach loop but it returns this :

FatalErrorException: Error: Call to a member function getId() on a non-object in * line *

Any idea how to solve this problem ? I saw you could do, for example, $feed[0]->getId() but I want an object, not an array. Some topics are similar but I didn't find any good answer.

EDIT : Well, i think i found the answer to my own question...

To get what i want, i need to remove the COUNT() in the select. I don't want it and it is only needed in the HAVING.

This looks to be working :

return $this->createQueryBuilder('f')
->leftJoin('f.subscriptions','s')
->groupBy('f')
->having('f.inCatalog = TRUE')
->orHaving('COUNT(s) > 0');
->getQuery()->getResult();

Now the result returns an array of objects as i wanted. Thanks to the people who answered.

3
  • Did you check if the query returns any results by var_dump the result? Also it will be good help if you provide you entity mapping for us Commented Mar 17, 2014 at 15:38
  • If i use var_dump, the browser usually crashes (too big). I will edit the post to add the mapping. Commented Mar 17, 2014 at 15:41
  • Did you also try ->select('COUNT(s.id)') ? Commented Mar 17, 2014 at 16:09

2 Answers 2

0

You have to use at least a primary key in your select, f.id in this case I guess and group by this id

Sign up to request clarification or add additional context in comments.

Comments

0

Try it with partial...

$qb->select('partial f.{id, subscriptions}, count(s.id)');

the results might be given back in an array, so you can try access it

{{ feed[0][0]->getId() }}

see my similar question: How can I select count(field) and still get an object with createQueryBuilder

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.