How to get random string in CakePHP 3.0?

I am trying to get a random string with CakePHP 3.0 RC-1, I looked through the docs .

Using what I had with CakePHP 2.X, and putting this as a starting point for getting a random string in CakePHP 3.0 RC-1. However, this does not seem to do this for Miss Cake:

$result = $this->Game->find('all') ->order('rand()') ->limit(1); 

The result gives nothing. There is data in the database, and I can get individual records. (Ie $ this-> Game-> get (20) works as it should).

+6
source share
1 answer

Just use "first" to get the first result:

 $result = $this->Game->find('all') ->order('rand()') ->first(); 

Alternatively, you can make it work like get() in that it will throw an exception if no results are found:

 $result = $this->Game->find('all') ->order('rand()') ->firstOrFail(); 
+8
source

Source: https://habr.com/ru/post/982052/


All Articles