Have you ever wondering how to find MAX, MIN or AVG of a database table column in CakePHP way? Well I haven’t since yesterday. The solution is quite simple and obvious.
Lets say you want to show the average views per post (in the example we have the views field in every post).
The SQL should be something like this:
SELECT AVG(views) FROM posts
Instead of using query() function you can use the CakePHP way:
Hope this will help someone.
It is my first time @ this blog and I have really enjoyed your useful articles, great work 🙂
Ok, now how you display it in your view, I’m having the error, Undefined index:avg_views
It’s really usefull, but it doesn’t work with FIRST & cie, is it ?
@Teru: It worked with FIRST normally, I don’t see any errors.