Jump to:

22976 Posts in 11620 Topics by 2826 members

General Questions

SilverStripe Forums » General Questions » Count DataObject Records

General questions about getting started with SilverStripe that don't fit in any of the categories above.

Moderators: martimiz, Sean, biapar, Willr, Ingo, swaiba, simon_w

Page: 1 2
Go to End
Author Topic: 4268 Views
  • DeklinKelly
    Avatar
    Community Member
    196 Posts

    Count DataObject Records Link to this post

    What is the best way to count the total matching records?

    This works, but it is beneficent:

    $records = DataObject::get($obj, $filter, $sort, $join);
    $recordsCount = count($records);

    I don't need to run the full query. I only want to count the total available records.

    Is there a graceful way to do something like this?

    $recordsCount = DataObject::count($obj, $filter, $sort, $join);

  • Willr
    Avatar
    Forum Moderator
    5462 Posts

    Re: Count DataObject Records Link to this post

    Quickest way is to do a direct DB::query(), using DataObject::get to just get a count is a very bad way of doing things.

    $count = DB::query("SELECT COUNT(*) FROM Table WHERE Foo LIKE 'Bar'")->value();

  • DeklinKelly
    Avatar
    Community Member
    196 Posts

    Re: Count DataObject Records Link to this post

    Thanks! That is what I needed. It also seems to work if you use 'ID' instead of *

    $count = DB::query("SELECT COUNT('ID') FROM Table WHERE Foo LIKE 'Bar'")->value();

  • DeklinKelly
    Avatar
    Community Member
    196 Posts

    Re: Count DataObject Records Link to this post

    Ooops, there is a problem, Willr.

    Your idea counts unpublished items. Do we need a join? If so, what?

    Thanks!

  • Willr
    Avatar
    Forum Moderator
    5462 Posts

    Re: Count DataObject Records Link to this post

    Your idea counts unpublished items. Do we need a join? If so, what?

    Well it depends what your counting, if its SiteTree objects then use SiteTree_Live as the table then rather than SiteTree.

  • DeklinKelly
    Avatar
    Community Member
    196 Posts

    Re: Count DataObject Records Link to this post

    Perfect, thanks!

  • Stefdv
    Avatar
    Community Member
    110 Posts

    Re: Count DataObject Records Link to this post

    Hello,

    But what do i call in my template if i just want to show the total numbers of objects?

    function TotalDogs(){
    $count = DB::query("SELECT COUNT(*) FROM Dog")->value();
    return $count;
       }

    if i look with

    var_dump($count)

    i get a string(2)"175" . How do i show this in my template ?

    thanks

  • Willr
    Avatar
    Forum Moderator
    5462 Posts

    Re: Count DataObject Records Link to this post

    You can then use $TotalDogs in your template to get that string (provided the function is in scope).

    4268 Views
Page: 1 2
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.