No limit for db queries


i started developing a plugin for elgg. It all went well but now i got a problem with the get_* functions. For example i inserted a new object of type xyz. Now i want to get all objects of this type. So when i look at the get_entities() function there is always the $limit parameter which is set to 10 by default. I don't need such a parameter because i want to get all objects. I could easily set it to 99999999 or something but i think that is bad practice. Am i wrong or is this really the only way to get all objects?

Another problem that has something with the problem from above in common is how to get the friends count of a user? I could set the $count param to true but there is the $limit param as well.

Can you help me?


  • You can first get a total number of entities by setting the count parameter turned on.

    $count = elgg_get_entities(array('types' => 'entity_name', 'count' => TRUE));

    Now you can use this count value as $limit to get all the entities.

  • Thank you webgalli. I want to make a statitics page for my 3 colum design. You showed the light...

  • So do i always have to take the detour like you did in your example? Does Elgg ignore the limit parameter if the count parameter is true?



  • You can just pass an empty limit '' so that it bypasses the adding of the offset and limit clauses to the query.  This way you need not make a double call and write extra code to determine the count and passing that in.

  • Though passing an empty limit works, it's best to pass a limit of 0. limit => '' looks like it could be a typo, while limit => 0 is more explicit. Either way, that's how you do it!

    Either way, when you pass count it disables limits and offsets because they don't make sense in that situation. Be careful when passing not using a limit, though, because you can run out of memory if you pull too many objects in at once.