MainelyDesign.com Blog

View Layer - Getting count of Entities in View in Cakephp 3

Posted on 06/12/2016 at 01:27 pm by Kevin Wentworth
Viewed 6,453 times | 0 comments

In my views I'm usually executing code depending on if a variable is empty or not.  For example, I'll fetch all the images in a gallery and only want to output the containing divs when the gallery has images.  You can't do a simple !empty() or if($img) because it will always be "true" because it will be a non-empty object (like Cake\ORM\Query or similar). So, if you want to find out if a variable (at the view level) is empty or not, use the isEmpty() function.

Is that variable empty? Checking for empty variables in CakePHP 3 View

  1. // in the controller/cell/etc.
  2. $someVar = $this->find('threaded', ['order' => 'lft ASC'])->where($conditions);
  3.  
  4. // in the view.ctp file
  5. if($someVar.isEmpty()) {
  6. return null;
  7. } else {
  8. // ... do your thing ...
  9. }

Now, that was pretty easy.  If you want to find the count of records and you are at the query layer, you can use the $query->count() method, described here.

Cheers,

-Kevin Wentworth

Bookmark and Share

Tags for View Layer - Getting count of Entities in View in Cakephp 3

Cakephp | Cake3

Comments for this Posting

No comments. Be the first to post a reply.

Sorry, comments are closed for this posting.

Please Email Kevin if you have any questions. Thanks!

Meet Site Avenger - Hosted Content Management System

Powered By: Site Avenger | Site Production: Saco Design