The Power ORM


Counting records is very easy with RedBeanPHP. For instance, to count all beans of type book use:

    $numOfBooks R::count'book' );

You can use additional SQL here as well:

    $numOfBooks R::count'book'' pages > ? ', [ 250 ] );

Count related beans

Counting related beans is just as simple. To count all the pages of a 'book' bean:

    $numPages $book->countOwn'page' );

You can use the same technique for shared lists:

    $numProjects $member->countShared'project' );

You can also use withCondition() and alias():

    $numProj $member
->withCondition(' member_project.role ', ['lead'] ) )
countShared'project' );

$numPages $book
->withCondition' book_page.number > ? ', [100] )
countOwn'page' );

$andy->alias'coAuthor' )->countOwn'book' );

$shop->via'relation' )->countShared'customer' );

The first example counts all projects associated with the member in which the member has the 'lead' role. The second example counts all the pages of a book having a page number > 100. Finally the last example demonstrates the use of an aliased list. Here we count the number of books written by Andy where he has been the co-author. All count operations return a number.

Counting something (R::count) that does not exist will not trigger an error but just return the number 0.

back to main menu

Donate to RedBeanPHP using Monero:
47mmY3AVbRu 7zVVd4bxQnzD
2jR7PQtBJ cF93jWHQ
rP7yRED4qr fqu6G9Q8ZNu7
zqwnB28rz76 w7MaExf
mALVg69yFd 9sUmz
(remove spaces and new lines)

Performance monitor: this page has been generated in 0.022078990936279s. Is the performance lacking? Please drop me an e-mail to notify me!

Partners PapelDigital


RedBeanPHP Easy ORM for PHP © 2024 () and the RedBeanPHP community () - Licensed New BSD/GPLv2 - RedBeanPHP Archives
RedBeanPHP, the power ORM for PHP since 2009.

Privacy Statement