I'm trying to retrieve a random row with CakePHP 3.0 RC-1, I've looked into the docs.
Using what I had from CakePHP 2.X and putting that as a starting point for getting a random row in CakePHP 3.0 RC-1. However, this is apparently not doing it for Miss Cake:
$result = $this->Game->find('all')
->order('rand()')
->limit(1);
The result yields nothing. There is data in the database, and I am able to retrieve single records. (I.e $this->Game->get(20) works like it should).
Just use "first" for getting the first result:
$result = $this->Game->find('all')
->order('rand()')
->first();
Alternatively, you can make it work like get()
in that it will return an exception if no results are found:
$result = $this->Game->find('all')
->order('rand()')
->firstOrFail();
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments